Minimal JavaScript translation library.
It's like i18n but worst!
Import index.js
script in your project.
Import your localized json object and assign it to currentLanguageDictionary
variable.
Use t()
function to get your translation by passing a string key and its optional variables.
Examples:
/* This is my current dictionary */
const currentLanguageDictionary = {
"simple-string": "Simple string",
"string-with-variable": "My name is ${name}",
"string-with-variables": "My full name is ${firstName} ${lastName}",
"parent-level": {
"child-level": "Second level",
"child-level-with-variable": "I was born in ${year}",
},
};
/* Pass a key string */
t("simple-string");
// Output: Simple string
/* Pass one variable */
t("string-with-variable", { name: "Luca" });
// Output: My name is Luca
/* Pass an array of variables */
t("string-with-variables", [{ firstName: "Mario" }, { lastName: "Rossi" }]);
// Output: My full name is Mario Rossi
/* Pass n variables */
t("string-with-variables", { firstName: "Mario" }, { lastName: "Rossi" });
// Output: My full name is Mario Rossi
/* Pass one object with multiple properties as variable */
t("string-with-variables", { firstName: "Mario", lastName: "Rossi" });
// Output: My full name is Mario Rossi
/* Pass a nested key string, dot character is the separator */
t("parent-level.child-level");
// Output: Second level
/* Pass a nested key string and one variable */
t("parent-level.child-level-with-variable", { year: "1990" });
// Output: I was born in 1990
Same examples are also provided in index.js
file
If you are cool include the index.min.js
file in your project and leave your coworkers astonished with the 185 bytes version of this library.
SIIIIIIUUUUUUUUM