feat: instance i18n object

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-03-30 18:49:08 +08:00
parent d9a2ea9c78
commit 12743bbbe0

View File

@ -3,49 +3,67 @@ import * as yaml from 'js-yaml'
const File = Java.type("java.io.File");
const separatorChar = File.separatorChar;
let langMap = {};
let fallbackMap = {};
type TranslateParam = { [key: string]: any }
function translate(name: string, param?: TranslateParam) {
let langText: string = langMap[name] || fallbackMap[name];
if (!langText) { return '[WARN] @ms/i18n miss lang translate: ' + name }
for (const key in param) {
langText = langText.replace("{" + key + "}", param[key])
}
return langText;
}
function initialize(lang: string = 'zh_cn', fallback: string = 'zh_cn') {
langMap = readYamlFile(root, lang) || readYamlFile(concat(__dirname, '..'), lang)
fallbackMap = readYamlFile(root, fallback) || readYamlFile(concat(__dirname, '..'), fallback)
console.i18n = function i18n(name: string, param?: TranslateParam) {
console.log(translate(name, param))
}
}
function readYamlFile(dir: string, name: string) {
let langFile = concat(dir, 'languages', name + '.yml');
return exists(langFile) && yaml.safeLoad(base.read(langFile))
}
function concat(...args: string[]) {
return args.join(separatorChar)
}
function exists(path: string) {
return new File(path).exists()
}
declare global {
interface Console {
i18n(name: string, param?: TranslateParam);
}
}
type TranslateContent = {
langMap: TranslateParam,
fallbackMap?: TranslateParam
}
export class Translate {
private root = '';
private langMap = {};
private fallbackMap = {};
constructor(root: string | TranslateContent) {
if (typeof root == 'string') {
this.root = root;
} else {
this.langMap = root.langMap;
this.fallbackMap = root.fallbackMap || {};
}
}
translate(name: string, param?: TranslateParam) {
let langText: string = this.langMap[name] || this.fallbackMap[name];
if (!langText) { return '[WARN] @ms/i18n miss lang translate: ' + name }
for (const key in param) {
langText = langText.replace("{" + key + "}", param[key])
}
return langText;
}
initialize(lang: string = 'zh_cn', fallback: string = 'zh_cn') {
this.langMap = this.readYamlFile(this.root, lang) || this.readYamlFile(this.concat(__dirname, '..'), lang)
this.fallbackMap = this.readYamlFile(this.root, fallback) || this.readYamlFile(this.concat(__dirname, '..'), fallback)
console.i18n = (name: string, param?: TranslateParam) => {
console.log(this.translate(name, param))
}
}
readYamlFile(dir: string, name: string) {
let langFile = this.concat(dir, 'languages', name + '.yml');
return this.exists(langFile) && yaml.safeLoad(base.read(langFile))
}
concat(...args: string[]) {
return args.join(separatorChar)
}
exists(path: string) {
return new File(path).exists()
}
}
let systemTranslate = new Translate(root)
export default {
initialize,
translate
};
initialize: systemTranslate.initialize.bind(systemTranslate),
translate: systemTranslate.translate.bind(systemTranslate)
}