WIP translation library enabling dictionary look ups, localized numbers and dates.
t(x, options)
import { bifrost } from 'bifrost-i18n';
import { en, nb } from './dictionaries.js';
const t = bifrost({
language: 'nb-NO',
dictionaries: new Map([
['nb-NO', nb],
['en-GB', en],
]),
});
const en = new Map([
['key', 'value'],
// Simple replace
['get the x', 'get the _'],
// Replace in specific order
['from a to b', 'from _2 to _1'],
// Simple plural
['fetch updates', new Map([
['one |', 'fetch update'],
['other |', 'fetch updates'],
])],
// Multiple plurals
['tomatoes and cucumbers', new Map([
['one & one |', 'tomato and cucumber'],
['one & other |', 'tomato and cucumbers'],
['other & one |', 'tomatoes and cucumber'],
['other & other |', 'tomatoes and cucumbers'],
])],
// Plural, but it's for ordinals only
['the nth update', new Map([
['| one', 'the first update'],
['| two', 'the second update'],
['| few', 'the third update'],
['| other', 'the _th update'],
])],
// Combined cardinal and ordinal plurals
['a good mix', new Map([
['one & other | few & one', 'value'],
['other & other | one & other', 'value'],
])],
]);
export { en };
- When specifying languages, use a BCP 47 language tag.
- Spaces between dividers in the plural key is optional.
language
is only used to translate numbers and other non dictionary translations.dictionaries
determines the order in which Bifrost should look for strings. In the example above, it would first look in the Norwegian dictionary, then the English one. If none are found, it'll return an empty string.
If key is string, the options are (in order):
- Array of replacement words
- Array of cardinal numbers matching string in dictionary
- Array of ordinal numbers matching string in dictionary
t('key', [[/*1.*/][/*2.*/][/*3.*/]]);
In object form, the options are:
t('key', {
replacements: [/*1.*/],
cardinals: [/*2.*/],
ordinals: [/*3.*/],
});
t('key');
//=> "value"
t('get the x', [['gin']]);
//=> "get the gin"
t('from a to b', [['London', 'Oslo']]);
//=> "from Oslo to London"
t('fetch updates', [,[1]]);
//=> "fetch update"
t('fetch updates', [,[9]]);
//=> "fetch updates"
t('tomatoes and cucumbers', [,[1, 3]]);
//=> "tomato and cucumbers"
t('the nth update', [,,[2]]);
//=> "the second update"
t('the nth update', [[7],,[7]]);
//=> "the 7th update"
Optionally, in object form:
t('the nth update', {
replacements: [7],
ordinals: [7],
});
//=> "the 7th update"
t(500000);
//=> "500,000"
t(3.14);
//=> "3.14"
t(500000);
//=> "500 000"
t(3.14);
//=> "3,14"
Coming