To use Lang.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 the setMessages() method.
Messages source format
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 the fallback locale. When retrieving a message (using get() or has()) which is not defined in the specified locale, then it will try to find a message with the fallback locale (if set).
Indicate if a given key is defined on the messages source. Return true if the key is defined on the messages source, otherwise false. 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 false will be returned.
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.
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
请发表评论