akili-localization
v1.2.9
Published
Localiztion service for Akili framework
Downloads
54
Maintainers
Readme
akili-localization
Localization service for Akili framework.
Example
import Akili from 'akili';
import localization from 'akili-localization';
const dictionaryEN = {
hello: "world",
languages: {
en: "english",
ru: "russian"
},
how: "from %% to %%",
when "at {{ time }} o'clock"
};
const dictionaryRU = {
hello: "привет"
};
localization.define({
defaultLocale: "en",
currentLocale: "ru"
});
localization.addDict("en-US", dictionaryEN);
localization.addDict("ru", dictionaryRU);
document.addEventListener('DOMContentLoaded', () => {
Akili.init().catch((err) => console.error(err));
});
<div>${ translate('hello') }</div>
<div>${ translate('languages.en') }</div>
<div>${ translate('how', [1, 10]) }</div>
<div>${ translate('when', { time: 5 }) }</div>
<div>${ date(new Date('1995-12-17T03:24:00')) }</div>
<div>${ currency(1250000.99, 'USD') }</div>
<div>${ number(1250000.99) }</div>
After the compilation:
<div>привет</div>
<div>english</div>
<div>from 1 to 10<div>
<div>at 5 o'clock</div>
<div>17.12.1995</div>
<div>1 250 000,99 $</div>
<div>1 250 000,99</div>
You can change the default or current locale any time you want and all expressions in templates will be updated.
The service based on localizationjs.
.define() method take localizationjs options, create an instance of the library class and put it on locale property in the service object. In addition, the service has all the same methods as localizationjs.
localization.setDefaultLocale('es');
localization.setCurrentLocale('fr');
localization.hasTranslation('hello');
// ...
If you load the component as a script, you will have Akili.services.localization to get it.