Isomorphic translation engine. Mimics Rails' i18n interface.
const I18n = require('@fiverr/i18n');
const translations = require('./translations.json');
const i18n = new I18n({translations});
Option | Type | Description |
---|---|---|
translations |
Object | Representation of translation structure Must be JSON compliant otherwise will be treated like an empty object |
missing |
Function | Call this function when a key is missing. Function accepts the key as first argument |
$scope |
String | Omittable prefix. see Scope The base is translations key root |
const i18n = new I18n({
translations: {...},
missing: key => logMissingKeyEvent({key: `missing_translation.${key.replace(/\W/g, '_')}`}),
$scope: 'my_app.en'
});
i18n.t('my.key'); // I'm a sentence
i18n.t(['my.missing.key', 'my.key']); // I'm a sentence
i18n.add({yet: {another: {key: 'I\'m here, too!'}}});
Use:
i18n.translate('yet.another.key'); // I'm here, too!
Or:
i18n.t('yet.another.key'); // I'm here, too!
const i18n = new I18n({
translations: {
my: { string: 'a dynamic %{thing} in a static string' }
}
});
i18n.t('my.string', {thing: 'value'}); // a dynamic value in a static string
const i18n = new I18n({
translations: {
it_will_take_me_days: {
one: 'It\'ll take me one day',
other: 'It\'ll take me %{count} days'
}
}
});
i18n.t('it_will_take_me_days', {count: 1}); // It'll take me one day
i18n.t('it_will_take_me_days', {count: 3}); // It'll take me 3 days
i18n.t('it_will_take_me_days', {count: 'a lot of'}); // It'll take me a lot of days
Priority:
- Found result with passed in scope (when applicable)
- Found result with instance set scope (when applicable)
- Found result w/o scope
// Global scope setup
const i18n = new I18n({
translations: {
key: 'Top',
child: {
key: 'Child'
},
something: {
key: 'Something'
}
}
});
const child = i18n.spawn('child');
i18n.t('key'); // Top
child.t('key'); // Child
child.t('key', { $scope: 'something' }); // Something
This is a good option for shorthand in enclosed parts of the application.
The translation store is shared so the parent can find the keys if it prefixes the namespace, and the child doesn't need to.
The child can also find "global" translations (ones that are outside it's namespace)
const usersI18n = i18n.spawn('users.get');
// Add translations under the scope
usersI18n.add({introduction: 'Hi, my name is %{username}'});
// Use translations
usersI18n.t('introduction', {username: 'Martin'}); // Hi, my name is Martin
i18n.t('users.get.introduction', {username: 'Martin'}); // Hi, my name is Martin
Exposes an empty instance of i18n
const i18n = require('@fiverr/i18n/instance');
i18n.add({...});
Made especially for use as a webpack external
externals: {
'@fiverr/i18n/instance': 'i18n'
}
Name can alternate:
import phraser from '@fiverr/i18n/instance';externals: { '@fiverr/i18n/instance': 'phraser' }
Make sure you only have one instance of I18n in your global scope
const i18n = I18n.singleton;
// Optional:
i18n.$scope = 'my.scope';
i18n.onmiss((key, scope) => console.error(`Missing key "${key}" ${scope ? `In scope: "${scope}"`}`));
i18n.add({...});
Shortcut:
const i18n = require('@fiverr/i18n/singleton');
Or simply
require('@fiverr/i18n/singleton');
// i18n is defined globally