littera
v1.1.0
Published
🌐 Lightweight library making multilingualism easier.
Downloads
12
Maintainers
Readme
Littera
🌐 Lightweight library making multilingualism easier.
Install
Use npm install littera
or clone/download the repository.
Examples
The basic concept is that you load an object with translations for each language and then just get a string with the right translation returned adequate to the active language.
Most basic example
import Littera from "littera";
// Object containing translations for each language and key.
const translations = {
en_US: {
"unique.example": "Example"
},
pl_PL: {
"unique.example": "Przykład"
},
de_DE: {
"unique.example": "Beispiel"
}
};
// Create an instance of Littera and load the translations.
const _littera = new Littera(translations);
// Set the active language to German (de_DE)
_littera.setLanguage("de_DE");
// Get the right translation for the active language using a key.
const translation = _littera.translate("unique.example");
console.log(translation); // Returns => Beispiel
Stacked languages example
import Littera from 'littera';
// Object containing translations for each language and key with stacked languages.
const translations = {
"unique.example": {
en_US: "Example",
pl_PL: "Przykład",
de_DE: "Beispiel"
}
}
// Create an instance of Littera and load the translations.
const _littera = new Littera(translations, { stackLanguages: true }); // !** { stackLanguages: true } **!
// Set the active language to German (de_DE)
_littera.setLanguage("de_DE");
// Get the right translation for the active language using a key.
const translation = _littera.translate("unique.example");
console.log(translation); // Returns => Beispiel
Give it a try on codesandbox
Instance
new Littera(translations, options)
You can pass two object arguments translations
and options
.
Options default
{
stackLanguages: false // Use the stacked type of translations.
}
Translations example
{
en_US: {
"unique.example": "Example"
},
pl_PL: {
"unique.example": "Przykład"
},
de_DE: {
"unique.example": "Beispiel"
}
}
or for stacked languages
{
"unique.example": {
en_US: "Example",
pl_PL: "Przykład",
de_DE: "Beispiel"
}
}
Note: You have to set stackLanguages
to true
in options.
Build instructions
After cloning the repo, install all dependencies using npm install
.
Build the docs:
npm run docs
Build the coverage:
npm run coverage
Test the library:
npm test
API
Here comes the geeky part - documentation.
License
Copyright (c) 2018 Mike Eling