localized-string
Version:
Fetches and formats translated strings.
54 lines (38 loc) • 1.3 kB
Markdown
# localized-string
Fetches and formats translated strings.
If you're using .properties files, [grunt-concat-i18n](https://www.npmjs.com/package/grunt-concat-i18n) is recommended.
## Installation
```
$ npm i localized-string --save
```
## Usage
```js
var localizedString = require('localized-string')(translations);
var myDefaultLanguageString = localizedString.getText('', 'some.key', 'parameter');
var myGermanString = localizedString.getText('de_DE', 'some.key', ['more', 'params']);
```
`translations` is expected to be a hash of translations containing the translated strings, for example:
```js
var translations = {
'' : { // Default language
'some.key': 'This is the parameter: {1}'
},
'de_DE': {
'some.key': 'Zwei parameters: {1} {2}'
}
}
```
It can also be a string pointing to a file which exports the default language. If files are named according to the pattern `filename_de_DE.js` then it will automatically pick up the languages. For example:
Directory contains
1. `translations.js`
1. `translations_de_DE.js`
And the files contain
```js
module.exports = {
'some.key': 'This is the parameter: {1}'
}
```
This could then be used with `localized-string`
```js
var localizedString = require('localized-string')('path/to/file/translations);
```