From 813f761e475a8213c08f006b56e1a0e65c0fbf17 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Thu, 27 Feb 2020 01:57:44 +0800 Subject: [PATCH] feat: add package i18n Signed-off-by: MiaoWoo --- packages/i18n/languages/en.yml | 20 +++++++++++++++ packages/i18n/languages/zh_cn.yml | 20 +++++++++++++++ packages/i18n/package.json | 36 ++++++++++++++++++++++++++ packages/i18n/src/index.ts | 42 +++++++++++++++++++++++++++++++ packages/i18n/tsconfig.json | 7 ++++++ 5 files changed, 125 insertions(+) create mode 100644 packages/i18n/languages/en.yml create mode 100644 packages/i18n/languages/zh_cn.yml create mode 100644 packages/i18n/package.json create mode 100644 packages/i18n/src/index.ts create mode 100644 packages/i18n/tsconfig.json diff --git a/packages/i18n/languages/en.yml b/packages/i18n/languages/en.yml new file mode 100644 index 00000000..3eebe3ae --- /dev/null +++ b/packages/i18n/languages/en.yml @@ -0,0 +1,20 @@ +ms.ployfill.initialize: "Initialization Java Nashorn ployfill. Please wait..." +ms.ployfill.completed: "Java Nashorn ployfill loading completed... Cost ({time}s)!" + +ms.core.ioc.initialize: "Initialization MiaoScript IOC Container @ms/container. Please wait..." +ms.core.ioc.completed: "MiaoScript IOC Container @ms/container loading completed({time}s)!" +ms.core.initialize.detect: "Detect Compatible Server set ServerType to {type} ..." +ms.core.package.initialize: "Initialization MiaoScript Package @ms/core @ms/{type} @ms/plugin. Please wait..." +ms.core.package.completed: "MiaoScript Package @ms/core @ms/{type} @ms/plugin loading completed({time}s)!" +ms.core.plugin.initialize: "Initialization MiaoScript Plugin System. Please wait..." +ms.core.plugin.completed: "MiaoScript Plugin System loading completed({time}s)!" +ms.core.engine.completed: "MiaoScript ScriptEngine loading completed... Done({time}s)!" +ms.core.engine.disable: "Disable MiaoScript Engine..." + +ms.plugin.initialize: "Initialization MiaoScript Plugin System: {plugin} ..." +ms.plugin.event.map: "Total {count} {type} Event Mapping Complate..." +ms.plugin.manager.scan: "Scanning Plugins in {folder} ..." +ms.plugin.manager.stage: + load: "加载" + enable: "启用" + disable: "关闭" diff --git a/packages/i18n/languages/zh_cn.yml b/packages/i18n/languages/zh_cn.yml new file mode 100644 index 00000000..ca8a3e76 --- /dev/null +++ b/packages/i18n/languages/zh_cn.yml @@ -0,0 +1,20 @@ +ms.ployfill.initialize: "初始化 Java Nashorn 补丁. 请稍候..." +ms.ployfill.completed: "Java Nashorn 补丁 加载完成... 耗时 ({time}s)!" + +ms.core.ioc.initialize: "初始化 MiaoScript IOC 容器 @ms/container. 请稍候..." +ms.core.ioc.completed: "MiaoScript IOC 容器 @ms/container 加载完成 耗时({time}s)" +ms.core.initialize.detect: "检测到兼容的服务器类型. 设置 ServerType 值 {type} ..." +ms.core.package.initialize: "初始化 MiaoScript 扩展 @ms/core @ms/{type} @ms/plugin. 请稍候..." +ms.core.package.completed: "MiaoScript 扩展 @ms/core @ms/{type} @ms/plugin 加载完成 耗时({time}s)" +ms.core.plugin.initialize: "MiaoScript 开始引导插件系统. 请稍候..." +ms.core.plugin.completed: "MiaoScript 插件加载完毕 耗时({time}s)!" +ms.core.engine.completed: "MiaoScript 脚本引擎 加载完毕... 耗时({time}s)!" +ms.core.engine.disable: "关闭 MiaoScript 引擎..." + +ms.plugin.initialize: "初始化 MiaoScript 插件系统: {plugin} ..." +ms.plugin.event.map: "总计 {count} 个 {type} 事件 映射完成..." +ms.plugin.manager.scan: "Scanning Plugins in {folder} ..." +ms.plugin.manager.stage: + load: "加载" + enable: "启用" + disable: "关闭" \ No newline at end of file diff --git a/packages/i18n/package.json b/packages/i18n/package.json new file mode 100644 index 00000000..25618341 --- /dev/null +++ b/packages/i18n/package.json @@ -0,0 +1,36 @@ +{ + "name": "@ms/i18n", + "version": "0.2.1", + "description": "MiaoScript i18n package", + "keywords": [ + "miaoscript", + "minecraft", + "bukkit", + "sponge" + ], + "author": "MiaoWoo ", + "homepage": "https://github.com/circlecloud/ms.git", + "license": "ISC", + "main": "dist/index.js", + "publishConfig": { + "registry": "https://repo.yumc.pw/repository/npm-hosted/" + }, + "scripts": { + "clean": "rimraf dist", + "watch": "tsc --watch", + "build": "yarn clean && tsc", + "test": "echo \"Error: run tests from root\" && exit 1" + }, + "devDependencies": { + "@types/js-yaml": "^3.12.2", + "reflect-metadata": "^0.1.13", + "rimraf": "^3.0.2", + "typescript": "^3.8.2" + }, + "dependencies": { + "@ms/container": "^0.2.1", + "@ms/ployfill": "^0.2.1", + "js-yaml": "^3.13.1" + }, + "gitHead": "781524f83e52cad26d7c480513e3c525df867121" +} diff --git a/packages/i18n/src/index.ts b/packages/i18n/src/index.ts new file mode 100644 index 00000000..c9cb2cb7 --- /dev/null +++ b/packages/i18n/src/index.ts @@ -0,0 +1,42 @@ +import '@ms/ployfill' +import * as yaml from 'js-yaml' + +import * as fs from '@ms/common/dist/fs' + +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(fs.concat(__dirname, '..'), lang) + fallbackMap = readYamlFile(root, fallback) || readYamlFile(fs.concat(__dirname, '..'), fallback) + console.i18n = function i18n(name: string, param?: TranslateParam) { + console.log(translate(name, param)) + } +} + +function readYamlFile(dir: string, name: string) { + let langFile = fs.concat(dir, 'languages', name + '.yml'); + return fs.exists(langFile) && yaml.safeLoad(base.read(langFile)) +} + +declare global { + interface Console { + i18n(name: string, param?: TranslateParam); + } +} + +export default { + initialize, + translate +}; diff --git a/packages/i18n/tsconfig.json b/packages/i18n/tsconfig.json new file mode 100644 index 00000000..7aae5d2b --- /dev/null +++ b/packages/i18n/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": "src", + "outDir": "dist" + } +} \ No newline at end of file