From 12743bbbe07e010db93849e672b2de1bed18cfb2 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Mon, 30 Mar 2020 18:49:08 +0800 Subject: [PATCH] feat: instance i18n object Signed-off-by: MiaoWoo --- packages/i18n/src/index.ts | 92 +++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 37 deletions(-) diff --git a/packages/i18n/src/index.ts b/packages/i18n/src/index.ts index 5b44ede5..18b65b3a 100644 --- a/packages/i18n/src/index.ts +++ b/packages/i18n/src/index.ts @@ -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) +}