JavaScript gettext-style translation output library with sprintf-js parameter replace.
$ npm install translation-dictionary
$ bower install translation-dictionary
<!-- with EventEmitter ~9 kb -->
<script src="bower_components/translation-dictionary/build/translation-dictionary.min.js"></script>
<!-- or without EventEmitter ~6.4 kb -->
<script src="bower_components/translation-dictionary/build/translation-dictionary.woe.min.js"></script>
var dict = new TranslationDictionary();
dict.registerTranslation('de', {
'I have a car' : 'Ich habe ein Auto',
'I have %d cars' : 'Ich habe %d Autos',
'Hello %s %s' : 'Hallo %s %s',
'Name: %2$s, Firstname: %1$s': 'Name: %2$s, Vorname: %1$s',
'Named arguments %(arg1)s' : 'Benannte Argumente %(arg1)s'
});
dict.setLocale('de');
dict.__('I have a car'); // 'Ich habe ein Auto'
dict._p('I have a car', 'I have %d cars', 2); // 'Ich habe 2 Autos'
dict.__('Hello %s %s', 'John', 'Doe'); // 'Hallo John Doe'
dict.__('Name: %2$s, Firstname: %1$s', 'John', 'Doe'); // 'Name: Doe, Vorname: John'
dict.__('Named arguments %(arg1)s', { arg1: 'inserted'}); // 'Named arguments inserted'
View example/ for detailed examples.
Return the current locale (default: 'en').
Set a pluralizer for a locale with the number of plurals. Visit localization-guide for pluralforms and unicode.org for details and examples.
registerPluralizer('cs', function(n) {
return (n === 1) ? 0 : ( n >= 2 && n <= 4) ? 1 : 2;
}, 3);
Merge translations to a locale.
// nPlurals=2 in german
registerTranslation('de', {
'wolve' : 'wolf',
'wolves': 'wölfe'
});
// nPlurals=3 in czech
dict.registerTranslation('cs', {
'wolve' : 'vlk',
'wolves': [
'vlci',
'vlků'
]
});
Set the applications base language.
Set the current locale.
Translate a singular text.
arguments[0] The text to translate. [arguments[n]] Optional parameters for sprintf to replace markers in the text.
Depending on the markers a list of parameters, an array, or an object can be used.
translate('a text');
translate('a text %s %s', 'str1', 'str2');
translate('a text %s %s', [ 'str1', 'str2' ]);
translate('a text %(name)s', { name: 'John Doe' });
Alias for translate ().
Translate a text as singular or plural, depending on the count.
arguments[0] The singular text to translate. arguments[1..(nPlurals-1)] The plural text to translate. arguments[nPlurals..n] The count and parameters for sprintf to replace markers in the text.
Depending on the markers a list of parameters, an array, or an object can be used.
translatePlural('a text', 'some text', 47);
translatePlural('a text %d %s', 'some text %d %s', 47, 'str1');
translatePlural('a text %d %s', 'some text %d %s', [ 47, 'str1' ]);
translatePlural('a text %(name)s', 'some text %(name)s', { count: 47, name: 'John Doe' });
Alias for translatePlural ().