2020-05-02 16:14:47 +00:00
|
|
|
/// <reference types="@ccms/nashorn" />
|
2020-02-26 17:57:44 +00:00
|
|
|
import * as yaml from 'js-yaml'
|
|
|
|
|
2020-12-07 03:12:49 +00:00
|
|
|
const File = Java.type("java.io.File")
|
|
|
|
const separatorChar = File.separatorChar
|
2020-02-26 17:57:44 +00:00
|
|
|
type TranslateParam = { [key: string]: any }
|
|
|
|
|
2020-03-30 10:49:08 +00:00
|
|
|
declare global {
|
|
|
|
interface Console {
|
2020-12-07 03:12:49 +00:00
|
|
|
i18n(name: string, param?: TranslateParam)
|
2020-02-26 17:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 10:49:08 +00:00
|
|
|
type TranslateContent = {
|
|
|
|
langMap: TranslateParam,
|
|
|
|
fallbackMap?: TranslateParam
|
2020-02-26 17:57:44 +00:00
|
|
|
}
|
|
|
|
|
2020-03-30 10:49:08 +00:00
|
|
|
export class Translate {
|
|
|
|
private root = '';
|
|
|
|
private langMap = {};
|
|
|
|
private fallbackMap = {};
|
2020-03-01 11:37:34 +00:00
|
|
|
|
2020-03-30 10:49:08 +00:00
|
|
|
constructor(root: string | TranslateContent) {
|
|
|
|
if (typeof root == 'string') {
|
2020-12-07 03:12:49 +00:00
|
|
|
this.root = root
|
2020-03-30 10:49:08 +00:00
|
|
|
} else {
|
2020-12-07 03:12:49 +00:00
|
|
|
this.langMap = root.langMap
|
|
|
|
this.fallbackMap = root.fallbackMap || {}
|
2020-03-30 10:49:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-01 11:37:34 +00:00
|
|
|
|
2020-03-30 10:49:08 +00:00
|
|
|
translate(name: string, param?: TranslateParam) {
|
2020-12-07 03:12:49 +00:00
|
|
|
let langText: string = this.langMap[name] || this.fallbackMap[name]
|
2020-05-02 16:14:47 +00:00
|
|
|
if (!langText) { return '[WARN] @ccms/i18n miss lang translate: ' + name }
|
2020-03-30 10:49:08 +00:00
|
|
|
for (const key in param) {
|
2020-05-02 16:14:47 +00:00
|
|
|
langText = langText.replace(new RegExp("{" + key + "}", 'gm'), param[key])
|
2020-03-30 10:49:08 +00:00
|
|
|
}
|
2020-12-07 03:12:49 +00:00
|
|
|
return langText
|
2020-03-30 10:49:08 +00:00
|
|
|
}
|
2020-02-26 17:57:44 +00:00
|
|
|
|
2020-03-30 10:49:08 +00:00
|
|
|
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))
|
|
|
|
}
|
2020-05-11 09:19:44 +00:00
|
|
|
console.i18n('ms.i18n.completed')
|
2020-03-30 10:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
readYamlFile(dir: string, name: string) {
|
2020-12-07 03:12:49 +00:00
|
|
|
let langFile = this.concat(dir, 'languages', name + '.yml')
|
2021-03-27 03:35:54 +00:00
|
|
|
return this.exists(langFile) && yaml.load(base.read(langFile))
|
2020-03-30 10:49:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
concat(...args: string[]) {
|
|
|
|
return args.join(separatorChar)
|
|
|
|
}
|
|
|
|
|
|
|
|
exists(path: string) {
|
|
|
|
return new File(path).exists()
|
2020-02-26 17:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 10:49:08 +00:00
|
|
|
let systemTranslate = new Translate(root)
|
|
|
|
|
2020-02-26 17:57:44 +00:00
|
|
|
export default {
|
2020-03-30 10:49:08 +00:00
|
|
|
initialize: systemTranslate.initialize.bind(systemTranslate),
|
|
|
|
translate: systemTranslate.translate.bind(systemTranslate)
|
|
|
|
}
|