feat: add package i18n
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
a3f8451842
commit
0176cabd41
20
packages/i18n/languages/en.yml
Normal file
20
packages/i18n/languages/en.yml
Normal file
@ -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: "关闭"
|
20
packages/i18n/languages/zh_cn.yml
Normal file
20
packages/i18n/languages/zh_cn.yml
Normal file
@ -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: "关闭"
|
36
packages/i18n/package.json
Normal file
36
packages/i18n/package.json
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"name": "@ms/i18n",
|
||||||
|
"version": "0.2.1",
|
||||||
|
"description": "MiaoScript i18n package",
|
||||||
|
"keywords": [
|
||||||
|
"miaoscript",
|
||||||
|
"minecraft",
|
||||||
|
"bukkit",
|
||||||
|
"sponge"
|
||||||
|
],
|
||||||
|
"author": "MiaoWoo <admin@yumc.pw>",
|
||||||
|
"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"
|
||||||
|
}
|
42
packages/i18n/src/index.ts
Normal file
42
packages/i18n/src/index.ts
Normal file
@ -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
|
||||||
|
};
|
7
packages/i18n/tsconfig.json
Normal file
7
packages/i18n/tsconfig.json
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"baseUrl": "src",
|
||||||
|
"outDir": "dist"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user