sg-i18n
v1.1.2
Published
Fork of https://github.com/ccqgithub/i18n-s
Downloads
3
Readme
sg-i18n
A simple tool to translate i18n.
use
install
npm install sg-i18n -S
use
import I18n from 'sg-i18n';
let locale = 'zh-cn';
let localeData = {
"test": "test string",
"b.test": "test string b",
"a is b's father": "${a} is ${b}'s father",
"c": {
"test": "test string c"
}
}
let i18n = new I18n({
locale: 'zh-CN', // init locale
i18nData: {}, // init data,
log: process.env.NODE_ENV !== 'production', // is log when no translate
// log: function(msg) { console.log(msg) }, // log can be a function
});
// set current locale and locale data
i18n.setLocaleData(locale, i18nData);
i18n.setLocale(locale);
// translate with vars
let str = i18n.__('test');
// translate with dot
let str = i18n.__('b.test');
// translate with context
let str = i18n.__('c.test');
// translate with vars
let str = i18n.__('a is b\'s father', {
a: 'Json',
b: 'Joy'
});
api
i18n.setLocaleData(locale, localeData)
i18n.setLocale(locale)
i18n.translate(key, vars={})
i18n.__(key, vars={})
: alias toi18n.translate