- Notifications
You must be signed in to change notification settings - Fork63
🎭 Laravel Translator class in JavaScript!
License
rmariuzzo/Lang.js
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Different installation methods:
- NPM:
npm install lang.js - Bower:
bower install lang.js - Manually:Download latest release
varlang=newLang({messages:source,locale:'fr',fallback:'zn'});
To useLang.js we need to specify at least the messages sources. This can be done during instantiation as shown in the previous code or later using thesetMessages() method.
The messages source format looks like:
{"locale1.name":{"key1":"value1","key2":"value2",// ... and more key-value pairs.},"locale2.name":{"key1":"value1","key2":"value2",// ... and more key-value pairs.},// ... and more locales.}
See the sample used in tests located at:test/fixture/messages.json.
Set messages source. Checkmessages source format.
varlang=newLang();lang.setMessages(source);
Get the current locale, if none set, the default locale will be returned (en).
varlang=newLang();lang.getLocale();// > "en"lang.setLocale('fr');lang.getLocale();// > "fr"
Set the current locale.
varlang=newLang();lang.setLocale('ht');lang.getLocale();// > "ht"
Get the fallback locale.
varlang=newLang();lang.getFallback();// > undefinedlang.setFallback('de');lang.getFallback();// > "de"
Set the fallback locale. When retrieving a message (usingget() orhas()) which is not defined in the specified locale, then it will try to find a message with the fallback locale (if set).
varlang=newLang({messages:{'en.greetings':{'hi':'Hi','hello':'Hello'},'it.greetings':{'hi':'Salve'}}});lang.setLocale('it');lang.get('greetings.hello');// > "greetings.hello"lang.setFallback('en');lang.get('greetings.hello');// > "Hello"
Indicate if a given key is defined on the messages source. Returntrue if the key is defined on the messages source, otherwisefalse. This method will try to get a message for the specified locale, if not found, then it will return a message for the fallback locale, if not found, thenfalse will be returned.
varlang=newLang({messages:{'en.greetings':{'hi':'Hi'},'es.greetings':{'hi':'Hola'}}});lang.has('greetings.hi');// > truelang.has('greetings.hi','es');// > truelang.has('greetings.hello');// > false
Get a translation message if found, otherwise return the given key. This method will try to get a message for the specified locale, if not found, then it will return a message for the fallback locale, if not found, then the given key will be returned.
varlang=newLang({messages:{'en.greetings':{'hi':'Hi'},'es.greetings':{'hi':'Hola'}}});lang.get('greetings.hi');// > "Hi"lang.get('greetings.hi',{},'es');// > "Hola"lang.get('greetings.hello');// > "greetings.hello"
Get a translation file from a nested directory
Lang.get('forum/thread.hello');// > "Hello"Lang.get('forum/thread.hello',{},'es');// > "Hola"
This method act as an alias ofget().
Get the plural or singular form of the message specified based on an integer value.
varlang=newLang({messages:{'en.fruits':{'apple':'apple|apples'},'es.fruits':{'apple':'manzana|manzanas'}}});lang.choice('fruits.apple',1);// > "apple"lang.choice('fruits.apple',4);// > "apples"lang.choice('fruits.apple',4,{},'es');// > "manzanas"
You may even create more complex pluralization rules which specify translation strings for multiple number ranges:
varlang=newLang({messages:{'en.fruits':{'apple':'{0} There are none|[1,19] There are some|[20,*] There are many'}}});lang.choice('fruits.apple',0);// > "There are none"lang.choice('fruits.apple',1);// > "There are some"lang.choice('fruits.apple',3);// > "There are some"lang.choice('fruits.apple',20);// > "There are many"lang.choice('fruits.apple',22);// > "There are many
This method act as an alias ofchoice().
- Fork this repository and clone it.
- Create a branch from develop:
git checkout -b feature/xxxxxxx - Submit a PR to be merge into develop branch.
To run the tests use the following commands:
- Single run:
npm run test - Run on changes:
npm run test:watch
About
🎭 Laravel Translator class in JavaScript!
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
