Releases: fiverr/i18n.js
[FIX] Critical fix - spawn should inherit onmiss functionality (#40)
Add "has" method for checking if keys exist
Example usage
i18n.has('key');
i18n.has(['key', 'other_key']);
i18n.has('namespace.key');
i18n.has('key', { $scope: 'namespace' });
Support a list of keys
Existing behaviour (still supported)
i18n.t('my.key'); // I'm a sentence
Added functionality
i18n.t(['my.missing.key', 'my.key']); // I'm a sentence
Change the priority of scope lookup
Scoped results are preferred to un-scoped when a scope is supplied
const i18n = new I18n({translations: {
key: 'Top',
child: { key: 'Child' },
something: { key: 'Something' }
}});
const child = i18n.spawn('child');
i18n.t('key'); // Top
i18n.t('key', {$scope: 'something'}); // Something
child.t('key'); // Child
child.t('key', {$scope: 'something'}); // Something (explicit scope is strongest)
Memoise - improve instance subsequent key lookups
Memoisation of resolve
operation may improve subsequent lookup significantly. Older node environments experience a more significant improvements
This feature is significant in server environments, where the same translation keys are being used over and over again by the same instance.
Expose instance - (made for Webpack external)
Made especially for use as a webpack external
import i18n from '@fiverr/i18n/instance';
externals: {
'@fiverr/i18n/instance': 'i18n'
}
Lock singleton to global, add onmiss entry point
- Lock singleton to global
- Add onmiss entry point, so singletons can also register for missing translation event
Example
i18n.onmiss((key, scope) => console.error(`Missing key "${key}" ${scope ? `In scope: "${scope}"`}`));
Introduce a child process - namespaced instance
Scoped child instance
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
Add singleton to interface
Singleton
Make sure you only have one instance of I18n in your global scope
const i18n = I18n.singleton;
i18n.$scope = 'my.scope'; // Optional
i18n.add({...});
Shortcut:
const i18n = require('@fiverr/i18n/singleton');
Interface shortcuts and maintenance
Add default options
I18n instance can be created with no options
Bind translate
Binding the "translate" function allows for simpler shortcutting:
Consumer code
Before
const i18n = new I18n({...}); const t = i18n.translate.bind(i18n);After
const i18n = new I18n({...}); const t = i18n.translate; // OR const t = new I18n({...}).translate;