Easy I18n JS
Simplify translate and plural message. Library is small and easy.
Inspired by https://github.com/aissat/easy_localization for Flutter
Install
npm install easy-i18n-js
Usage
Import
import { installEasyI18n, setEasyI18nMessages, tr, plural } from 'easy-i18n-js';
Static installEasyI18n
Initialize EasyI18n and install tr
and plural
function in [String]
installEasyI18n();
installEasyI18n({ ... });
Options arguments
Name | type | example | description |
---|
logging | boolean | true | Show logging |
pluralRuleFn | 'zero' | 'one' | 'two' | 'few' | 'many' | 'other' | (value) => 'other' | Custom plural |
numberFormatterFn | (value: number) => string | (value) => value.Precision(3) | Global formatter |
modifiers | { [key: string]: (val: string | undefined) => string | undefined } } | | Custom modifier for link |
Static setEasyI18nMessages
Set messages for Easy 18n and set current locale
Arguments
Name | type | example | description |
---|
msg | { [key: string]: string | msg; } } | See below | Messages |
locale | string | 'fr_FR' | Current locale use for plural |
setEasyI18nMessages({
'welcome': 'Hello world'
});
setEasyI18nMessages({
'welcome': 'Hello world'
}, 'en_US');
Translate tr()
Main function for translate your language keys
You can use extension methods of [String], you can also use tr() as a static function.
installEasyI18n();
setEasyI18nMessages({
'welcome': 'Hello world'
});
'welcome'.tr()
tr('welcome')
Arguments
Name | type | example |
---|
args | string[] | ['Gabriel', '20'] |
namedArgs | { [key: string]: string; } } | { name : 'Gabriel', age : '20' } |
key | string | 'welcome' |
namespace | string | 'common' |
gender | 'male' | 'female' | 'other' | gender: 'other' |
Examples
installEasyI18n();
setEasyI18nMessages({
'welcome': 'Welcome',
'Complexe Key': 'Key is complexe',
'common': {
'welcome': 'Welcome everyone'
},
'msg_args': '{} are written in the {} language',
'msg_named': 'Easy I18n is written in the {lang} language',
'msg_mixed': 'Hi {name}, you are {} old and rename {name}',
'welcome_gender': {
'male': 'Hi, boy {name}',
'female': 'Hello girl {name}',
'other': 'Welcome {name}'
},
});
'welcome'.tr()
'Hello world'.tr()
'welcome'.tr({ namespace: 'common' })
'common.welcome'.tr()
'msg_args'.tr({ args: ['Easy I18n', 'french'] });
'msg_named'.tr({ namedArgs: { lang: 'french' } });
'msg_mixed'.tr({ args: ['30'], namedArgs: { name: 'Gabriel' } });
'Hi {}, do you want delete {nb} files?'.tr({
args: ['Gabriel'],
namedArgs: { nb: '30' }
});
'welcome_gender'.tr({ namedArgs: { name: 'Gabriel' }, gender: 'male' });
'welcome_gender'.tr({ namedArgs: { name: 'Sandra' }, gender: 'female' });
'welcome_gender'.tr({ namedArgs: { name: 'Sandra and Gabriel' }, gender: 'other' });
Translate plural()
You can translate with pluralization. To insert a number in the translated string, use {}.
You can use extension methods of [String], you can also use plural() as a static function.
Arguments
Name | type | example |
---|
args | string[] | ['Gabriel', '20'] |
namedArgs | { [key: string]: string; } } | { name : 'Gabriel', age : '20' } |
key | string | 'welcome' |
namespace | string | 'common' |
name | string | money |
numberFormatterFn | (value: number) => string | (value) => value.Precision(3) |
gender | 'male' | 'female' | 'other' | gender: 'other' |
installEasyI18n();
setEasyI18nMessages({
'money': {
'zero': 'You not have money',
'one': 'You have {} dollar',
'many': 'You have many {} dollars',
'other': 'You have other {} dollars'
},
'money_args': {
'zero': '{} has no money',
'one': '{} has {} dollar',
'many': '{} has {} dollars',
'other': '{} has {} dollars'
},
'money_named_args': {
'zero': '{name} has no money',
'one': '{name} has {money} dollar',
'many': '{name} has {money} dollars',
'other': '{name} has {money} dollars'
}
});
'money'.plural(10)
'money_args'.plural(0, { args: ['Gabriel'] })
'money_args'.plural(1.5, { args: ['Gabriel'] })
'money_named_args'.plural(1, { namedArgs: { name: 'Gabriel' }, name: 'money' })
'money_named_args'.plural(1.5, { namedArgs: { name: 'Gabriel' }, name: 'money' })
Gender
installEasyI18n();
setEasyI18nMessages({
'money': {
'zero': { male: 'He not have money', female: 'She not have money', other: 'You not have money' },
'one': { male: 'He have {} dollar', female: 'She have {} dollar', other: 'You have {} dollar' },
'two': 'You have few {} dollars',
'other': { male: 'He have other {} dollars', female: 'She have other {} dollars', other: 'You have other {} dollars' },
}
});
'money'.plural(0, { gender: 'female' })
'money'.plural(1, { gender: 'other' })
'money'.plural(2, { gender: 'male' })
'money'.plural(1.5, { gender: 'male' })
Formatter
With install for default
installEasyI18n({
numberFormatterFn: (value) => value.toString().replaceAll('.', ',')
});
or for particular
'money'.plural(10.24, { numberFormatterFn: (value) => value.toString().replaceAll('.', ',') })
Locale and PluralCase
With specific locale
installEasyI18n();
setEasyI18nMessages({
'money': {
'zero': 'You not have money',
'one': 'You have {} dollar',
'few': 'You have few {} dollars',
'many': 'You have many {} dollars',
'other': 'You have other {} dollars'
}
}, 'br');
'money'.plural(0)
'money'.plural(1.5)
'money'.plural(3)
'money'.plural(4000000)
'money'.plural(5)
Or custom PluralCase
installEasyI18n({
pluralRuleFn: (value) => {
if (value < 1) {
return 'zero';
}
if (value < 2) {
return 'one';
}
if (value < 10) {
return 'few';
}
if (value > 10000) {
return 'many';
}
return 'other';
}
});
setEasyI18nMessages({
'money': {
'zero': 'You not have money',
'one': 'You have {} dollar',
'few': 'You have few {} dollars',
'many': 'You have many {} dollars',
'other': 'You have other {} dollars'
}
});
'money'.plural(0)
'money'.plural(0.5)
'money'.plural(1)
'money'.plural(1.5)
'money'.plural(5)
'money'.plural(10.24)
'money'.plural(31024)
Link and modifiers
If there's a translation key that will always have the same concrete text as another one you can just link to it. To link to another
translation key, all you have to do is to prefix its contents with an @: sign followed by the full name of the translation key including the
namespace you want to link to.
You can also do nested anonymous and named arguments inside the linked messages.
Formatting linked locale messages If the language distinguishes cases of character, you may need to control the case of the linked locale
messages. Linked messages can be formatted with modifier @.modifier:key
The below modifiers are available currently.
modifier | description | example |
---|
lower | Lowercase all characters in the linked message. | @:.lower:key |
upper | Uppercase all characters in the linked message | @:.upper:key |
capitalize | Capitalize the first character in the linked message. | @:.capitalize:key |
installEasyI18n({
modifiers: {
camel: lodash.camelCase
}
});
setEasyI18nMessages({
'common': {
'yes': 'Yes',
'no': 'No',
'other': 'Other value',
'full_name': '{first} {last}'
},
'love': 'I love link @:common.yes or @:common.no',
'love2': 'I love link @.lower:common.yes or @.upper:common.no',
'love3': 'I love link @.camel:common.other',
'love4': 'I love link @:common.full_name'
});
'love'.tr()
'love2'.tr()
'love3'.tr()
'love4'.tr({ namedArgs: { first: 'Gabriel', last: 'Allaigre' } })