Tiny utility to implement localization in JavaScript (L10N), supporting only scoped translations; and simple templates.
- Supports scoped translations
- Supports simple templates with mustaches {{ }}
- Enables the implementation of text localization
- Supports NodeJs and amd
- Minified it weighs only 1.8 KB
I.js is meant to be as simple and short as possible, and to support only scoped translations. Users who need more complex solutions (e.g. with support for pluralization; currencies; dates management), should look instead at other libraries (internationalization frameworks):
For a comparison with i18n-js library, please refer to the dedicated wiki page;
Either install using npm
:
npm install ilocale
Or download one of these two files:
- https://raw.githubusercontent.com/RobertoPrevato/I.js/master/source/i.js
- https://raw.githubusercontent.com/RobertoPrevato/I.js/master/source/i.min.js
If I.js is installed using npm
package:
import I from "ilocale"
I.regional = {
"en": {
"voc": {
"Hi": "Hi"
}
}
}
I.setLocale("en");
I.t("voc.Hi"); // --> "Hi"
The following example shows how to work with scoped translations, using I.js:
(function () {
//set the locale information:
I.regional = {
"en": {
"voc": {
"Hi": "Hi"
}
},
"it": {
"voc": {
"Hi": "Ciao"
}
},
"pl": {
"voc": {
"Hi": "Cześć"
}
}
};
I.setLocale("en");
I.t("voc.Hi"); // --> "Hi"
I.setLocale("it");
I.t("voc.Hi"); // --> "Ciao"
I.setLocale("pl");
I.t("voc.Hi"); // --> "Cześć"
})();
The following example shows how to use simple templates:
(function () {
//set the locale information:
I.regional = {
"en": {
"voc": {
"HelloName": "Hello, {{name}}!"
}
},
"it": {
"voc": {
"HelloName": "Ciao, {{name}}!"
}
}
};
I.setLocale("en");
I.t("voc.HelloName"); // --> "Hello, {{name}}!"
I.t("voc.HelloName", {
name: "Edyta" // e.g. user.name
}); // --> "Hello, Edyta!"
I.setLocale("it");
I.t("voc.HelloName"); // --> "Ciao, {{name}}!"
I.t("voc.HelloName", {
name: "Edyta" // e.g. user.name
}); // --> "Ciao, Edyta!"
})();
Please refer to the dedicated wiki page.
The following repositories contain examples of internationalization implemented using I.js:
Live demos:
When setting a locale using the function setLocale; a class to the document.body is also added accordingly.