npm install lang.js@next
var lang = new Lang({
messages: source, // required
locale: 'fr', // optional
fallback: 'zn' // optional
})
The messages source format looks like this:
{
"locale-1.mesages-name": {
"key-1": "value-1",
"key-2": "value-2",
// ... and more key-value pairs.
},
"locale-2.mesages-name": {
"key-1": "value-1",
"key-2": "value-2",
// ... and more key-value pairs.
},
// ... and more locales.
}
See the sample used in tests located at: test/fixture/messages.json
.
Set messages source. Check messages source format.
lang.setMessages(source)
Get the current locale, if none set, the default locale will be returned (en
).
lang.getLocale()
// > "en"
Set the current locale.
lang.setLocale('ht')
Get the fallback locale.
lang.getFallback()
// > de
Set the fallback locale for messages not found using the default locale.
var lang = new Lang({
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.
lang.has('greetings.hi')
Get a translation message.
lang.get('greetings.hi')
lang.get('forum/thread.hello')
This method act as an alias of get()
.
Get the plural or singular form of the message specified based on an integer value.
lang.choice('fruits.apple', 1)
// > "apple"
lang.choice('fruits.apple', 4)
// > "apples"
You may even create more complex pluralization rules which specify translation strings for multiple number ranges:
var lang = new Lang({
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 of choice()
.
- Fork this repository and clone it.
- Create a branch from
next
branch. - Submit a PR to be merge into
next
branch.
To run the tests use the following commands:
- Single run:
npm run test
We do deployment using np
:
npm run release:next
npm run release:stable