simple i18n
npm install --save simple-i18n
import i18n from 'simple-i18n'
orconst i18n = require('simple-i18n')
func(key): value
translate a key into value
func(locale, key): value
translate a key into value by locale
func(locale)
set the locale
func(locale)
set default locale
func(mapper)
set language definition mapper. a mapper is a function as func(locale): lang.
A language definition is a js object with leaves of type string or function. For example:
// define a language
const zh_cn = {
name: '名字',
account: {
username: '用户名',
password: '密码'
},
welcome: name=> `欢迎,${name}!`
}
// define a mapper
const mapper = locale=> {
switch (locale) {
case: 'zh-cn': return zh_cn;
...
}
}
// set mapper
i18n.setMapper(mapper)
which will result in
i18n('name') // '名字'
i18n.__('account.username') // '用户名'
i18n('welcome', 'Bob') // '欢迎,Bob!'
MIT