A simple and easy to use lib for localizing your application. Size 1.1 kb. Writen on pure JavaScript.
<script src="easy-locale.min.js"></script>
npm install easy-locale
(function(){
var translations = {
en:{
string: 'test string',
section:{
substring: 'test substring'
}
string_with_values: 'string with value1: %{value1} and value2: %{value2}'
string_with_count: {
elements: {
zero: '%{count} elements',
one: '%{count} element',
other: '%{count} elements'
}
},
},
...
}
locale.init('en', translations);
window.t = locale.t;
})();
console.log(t('string'));
// => test string
console.log(t('section.substring'));
// => test substring
console.log(t('string_with_values', {value1: 'first value', value2: 'second value'}));
// => string with value1: first value and value2: second value
console.log(t('string_with_count.elements', {count: 0}));
// => 0 elements
console.log(t('string_with_count.elements', {count: 1}));
// => 1 element
Sometimes you may need only one locale.
For example if you build on server side dynamicly your translation based on his current locale zone and then send it to client.
var translations = {
first: 'First string',
second: 'Second string',
};
var options = {
singleLocale: true
}
// We still need set first parameter for determining pluralization logic
locale.init('en', translations, options);
console.log(locale.t('first'));
// => First string
Shows object with current translation
var translations = {
en: {
first: 'First string',
second: 'Second string',
},
ru: {
first: 'Первая строка',
second: 'Вторая строка',
}
};
locale.init('en', translations);
console.log(locale.getCurrent());
// =>
// {
// first: 'First string',
// second: 'Second string'
// }