Skip to content

Releases: fiverr/i18n.js

[FIX] Critical fix - spawn should inherit onmiss functionality (#40)

22 Jan 07:30
214472f
Compare
Choose a tag to compare
1.8.1

[FIX] Critical fix - spawn should inherit onmiss functionality (#40)

Add "has" method for checking if keys exist

02 Jan 15:07
bd0da6c
Compare
Choose a tag to compare

Example usage

i18n.has('key');
i18n.has(['key', 'other_key']);
i18n.has('namespace.key');
i18n.has('key', { $scope: 'namespace' });

Support a list of keys

09 Dec 13:12
a479841
Compare
Choose a tag to compare

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

04 Aug 08:42
5196d15
Compare
Choose a tag to compare

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

08 Nov 09:47
Compare
Choose a tag to compare

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)

25 Sep 10:33
Compare
Choose a tag to compare

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

24 Sep 10:40
Compare
Choose a tag to compare
  • 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

18 Sep 05:38
Compare
Choose a tag to compare

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

13 Sep 18:21
Compare
Choose a tag to compare

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

10 Sep 07:54
Compare
Choose a tag to compare

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;

Add alias t to 'translate' function


Maintenance work