feat: improve i18n scope

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-02-27 12:11:18 +08:00
parent 08ca3db67b
commit 8a0fcfae8b
7 changed files with 77 additions and 43 deletions

View File

@ -1,3 +1,4 @@
import i18n from '@ms/i18n'
import { injectable } from "@ms/container";
export namespace command {
@ -5,18 +6,18 @@ export namespace command {
export abstract class Command {
on(plugin: any, name: string, exec: { cmd: Function, tab?: Function }) {
var cmd = this.create(plugin, name);
console.debug(`[${plugin.description.name}] register command ${name}(${cmd})...`)
console.debug(i18n.translate("ms.api.command.register", { plugin: plugin.description.name, name, cmd }))
if (exec.cmd && typeof exec.cmd === "function") {
this.onCommand(plugin, cmd, exec.cmd);
} else {
throw Error("CommandExec Must be a function... Input: " + exec.cmd)
throw Error(i18n.translate("ms.api.command.register.input.error", { exec: exec.cmd }))
}
if (exec.tab && typeof exec.tab === "function") {
this.onTabComplete(plugin, cmd, exec.tab);
}
}
off(plugin: any, name: string) {
console.debug(`[${plugin.description.name}] unregister command ${name}...`)
console.debug(i18n.translate("ms.api.command.unregister", { plugin: plugin.description.name, name }))
this.remove(plugin, name);
}
/**

View File

@ -80,7 +80,7 @@ export class MiaoScriptConsole implements Console {
this.info(args)
}
i18n(name: string, param?: { [key: string]: any }) {
this.log(i18m.translate(name, param))
this.console(i18m.translate(name, param))
}
object(obj) {
for (var i in obj) {
@ -103,7 +103,7 @@ export class MiaoScriptConsole implements Console {
}
}
if (this.sourceMaps[fileName]) {
var sourceMapping = this.sourceMaps[fileName].getSource(lineNumber, 0);
var sourceMapping = this.sourceMaps[fileName].getSource(lineNumber, lineNumber);
if (sourceMapping) {
if (lineNumber != sourceMapping.mapping.sourceLine) {
fileName = fileName.replace(".js", ".ts");

View File

@ -1,9 +1,9 @@
'use strict';
/// <reference types='@ms/nashorn' />
/**
* MiaoScript Event处理类
*/
import '@ms/core'
import '@ms/nashorn'
import i18n from '@ms/i18n'
import { injectable, unmanaged } from '@ms/container'
const Thread = Java.type('java.lang.Thread');
@ -35,9 +35,7 @@ export namespace event {
* org.spongepowered.api.event.game.GameRegistryEvent.Register => gameregistryevent$register
*/
mapEventName() {
if (this.baseEventDir === "") {
throw new Error("base event dir is empty, can't map event name !");
}
if (this.baseEventDir === "") { throw new Error(i18n.translate('ms.api.event.empty.event.dir')); }
let count = 0;
let jar = this.getJarFile(this.baseEventDir);
let entries = jar.entries();
@ -51,7 +49,7 @@ export namespace event {
let clazz = base.getClass(qualifiedName.substring(0, qualifiedName.length - 6));
if (this.isValidEvent(clazz)) {
let simpleName = this.class2Name(clazz).toLowerCase();
console.trace(`Mapping Event [${clazz.canonicalName}] => ${simpleName}`);
console.trace(i18n.translate("ms.api.event.mapping", { canonicalName: clazz.canonicalName, simpleName }));
this.mapEvent[simpleName] = clazz;
count++;
}
@ -69,7 +67,7 @@ export namespace event {
let url = dirs.nextElement();
if (url.protocol === "jar") { return url.openConnection().jarFile; }
}
throw new Error(`Can't Mapping Event Because not found Resources ${resource}!`)
throw new Error(i18n.translate("ms.api.event.resource.not.found", { resource }))
}
class2Name(clazz: any) {
@ -83,8 +81,7 @@ export namespace event {
eventCls = base.getClass(eventCls);
this.mapEvent[event] = eventCls;
} catch (ex) {
console.console(`§6插件 §b${name} §6注册事件 §c${event} §6失败 §4事件未找到!`);
console.ex(new Error(`Plugin ${name} register event error ${event} not found!`));
console.i18n("ms.api.event.not.found", { name, event })
return;
}
}
@ -100,11 +97,11 @@ export namespace event {
exec(event);
let cost = Date.now() - time;
if (cost > 20) {
console.console(`§c注意! §6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件 §c耗时 §4${cost}ms !`)
console.i18n("ms.api.event.execute.slow", { name, event: this.class2Name(eventCls), cost })
}
}
} catch (ex) {
console.console(`§6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件时发生异常 §4${ex}`);
console.i18n("ms.api.event.execute.error", { name, event: this.class2Name(eventCls), ex })
console.ex(ex);
}
}
@ -119,7 +116,7 @@ export namespace event {
* @param ignoreCancel
*/
listen(plugin: any, event: string, exec: (event: any) => void, priority: EventPriority = EventPriority.NORMAL, ignoreCancel = false) {
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError('插件名称为空 请检查传入参数!');
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError(i18n.translate("ms.api.event.listen.plugin.name.empty"));
var name = plugin.description.name;
var eventCls = this.name2Class(name, event);
if (!eventCls) { return; }
@ -136,7 +133,7 @@ export namespace event {
if (!listenerMap[name]) listenerMap[name] = [];
var offExec = () => {
this.unregister(eventCls, listener);
console.debug(`[${name}] unregister event ${this.class2Name(eventCls)}`);
console.debug(i18n.translate("ms.api.event.unregister", { name, event: this.class2Name(eventCls) }));
};
var off = {
event: eventCls,
@ -145,7 +142,7 @@ export namespace event {
};
listenerMap[name].push(off);
// noinspection JSUnresolvedVariable
console.debug(`[${name}] register event ${this.class2Name(eventCls)} => ${exec.name || '[anonymous]'}`);
console.debug(i18n.translate("ms.api.event.register", { name, event: this.class2Name(eventCls) }));
return off;
}

View File

@ -1,5 +1,5 @@
import '@ms/ployfill'
import i18n from '@ms/i18n'
/// <reference types="@ms/nashorn" />
import '@ms/i18n'
let containerStartTime = Date.now();
console.i18n("ms.core.ioc.initialize");
@ -92,7 +92,7 @@ function initialize() {
container.load(buildProviderModule());
console.i18n("ms.core.package.completed", { type, time: (Date.now() - corePackageStartTime) / 1000 });
let disable = container.get<MiaoScriptCore>(MiaoScriptCore).enable()
console.i18n("ms.core.engine.completed", { time: (Date.now() - corePackageStartTime) / 1000 });
console.i18n("ms.core.engine.completed", { time: (Date.now() - global.NashornEngineStartTime) / 1000 });
return disable;
}

View File

@ -11,10 +11,28 @@ 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.api.event.resource.not.found: "Can't Mapping Event Because not found Resources {resource}!"
ms.api.event.empty.event.dir: "base event dir is empty, can't map event name !"
ms.api.event.mapping: "Mapping Event [{canonicalName}] => {simpleName}"
ms.api.event.not.found: "§6Plugin §b{name} §6register {event} error. event not found!"
ms.api.event.execute.slow: "§cWARN! §6Plugin §b{name} §6execute §d{event} §6evnet §ccost §4{cost}ms !"
ms.api.event.execute.error: "§6Plugin §b{name} §6execute §d{event} §6event error §4{ex}"
ms.api.event.listen.plugin.name.empty: "Plugin name can't be empty!"
ms.api.event.register: "[{name}] register event {event}"
ms.api.event.unregister: "[{name}] unregister event {event}"
ms.api.command.register.input.error: "CommandExec Must be a function... Input: {exec}"
ms.api.command.register: "[{plugin}] register command {name}({cmd})..."
ms.api.command.unregister: "[{plugin}] unregister command {name}..."
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: "关闭"
ms.plugin.manager.initialize.error: "§6Plugin §b{name} §6initialize error §4{ex}"
ms.plugin.manager.stage: "{stage} {plugin} version {version} by {author}"
ms.plugin.manager.stage.load: "Loading"
ms.plugin.manager.stage.enable: "Enabling"
ms.plugin.manager.stage.disable: "Disabling"
ms.plugin.manager.build.update: "Auto Update Plugin {name} ..."
ms.plugin.manager.build.not.extends: "§4Found error plugin §b{source} §4it's not extends interfaces.Plugin, the plugin will be ignore!"
ms.plugin.manager.build.exists: "§4Found duplicate plugin §b{exists} §4and §b{source}§4. the first plugin will be ignore!"
ms.plugin.manager.config.save.default: "[{plugin}] config {name}.{format} not exists. auto create from default variable..."

View File

@ -11,10 +11,28 @@ ms.core.plugin.completed: "MiaoScript 插件加载完毕 耗时({time}s)!"
ms.core.engine.completed: "MiaoScript 脚本引擎 加载完毕... 耗时({time}s)!"
ms.core.engine.disable: "关闭 MiaoScript 引擎..."
ms.api.event.resource.not.found: "无法映射事件 未找到资源文件 {resource}!"
ms.api.event.empty.event.dir: "事件基础目录为空, 无法映射事件!"
ms.api.event.mapping: "映射事件 [{canonicalName}] => {simpleName}"
ms.api.event.not.found: "§6插件 §b{name} §6注册事件 §c{event} §6失败. §4事件未找到!"
ms.api.event.execute.slow: "§c注意! §6插件 §b{name} §6处理 §d{event} §6事件 §c耗时 §4{cost}ms !"
ms.api.event.execute.error: "§6插件 §b{name} §6处理 §d{event} §6事件时发生异常 §4{ex}"
ms.api.event.listen.plugin.name.empty: "插件名称为空 请检查传入参数!"
ms.api.event.register: "[{name}] 注册事件 {event}"
ms.api.event.unregister: "[{name}] 注销事件 {event}"
ms.api.command.register.input.error: "CommandExec 必须为一个函数... 输入: {exec}"
ms.api.command.register: "[{plugin}] 注册命令 {name}({cmd})..."
ms.api.command.unregister: "[{plugin}] 注销命令 {name}..."
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: "关闭"
ms.plugin.manager.scan: "在 {folder} 文件夹中扫描插件..."
ms.plugin.manager.initialize.error: "§6插件 §b{name} §6初始化错误 §4{ex}"
ms.plugin.manager.stage: "{stage} {plugin} 版本 {version} 作者 {author}"
ms.plugin.manager.stage.load: "加载"
ms.plugin.manager.stage.enable: "启用"
ms.plugin.manager.stage.disable: "关闭"
ms.plugin.manager.build.update: "自动更新插件 {name} ..."
ms.plugin.manager.build.not.extends: "§4发现错误的插件 §b{source} §4未继承接口 interfaces.Plugin, 将不会被载入到服务器!"
ms.plugin.manager.build.duplicate: "§4发现已存在插件 §b{exists} §4和 §b{source}§4 存在冲突. 已存在插件将会被替换!"
ms.plugin.manager.config.save.default: "[{plugin}] 配置 {name}.{format} 不存在. 从默认值自动创建保存..."

View File

@ -49,12 +49,12 @@ export class PluginManagerImpl implements plugin.PluginManager {
}
private logStage(plugin: interfaces.Plugin, stage: string) {
console.log(`[${plugin.description.name}] ${stage} ${plugin.description.name} version ${plugin.description.version} by ${plugin.description.author || 'Unknow'}`)
console.i18n("ms.plugin.manager.stage", { stage, plugin: plugin.description.name, version: plugin.description.version, author: plugin.description.author })
}
load(...args: any[]): void {
this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => {
this.logStage(plugin, i18n.translate("ms.plugin.stage.load"))
this.logStage(plugin, i18n.translate("ms.plugin.manager.stage.load"))
this.loadConfig(plugin)
this.runCatch(plugin, 'load')
this.runCatch(plugin, `${this.serverType}load`)
@ -63,7 +63,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
enable(...args: any[]): void {
this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => {
this.logStage(plugin, i18n.translate("ms.plugin.stage.enable"))
this.logStage(plugin, i18n.translate("ms.plugin.manager.stage.enable"))
this.runCatch(plugin, 'enable')
this.runCatch(plugin, `${this.serverType}enable`)
this.registryCommand(plugin)
@ -73,7 +73,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
disable(...args: any[]): void {
this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => {
this.logStage(plugin, i18n.translate("ms.plugin.stage.disable"))
this.logStage(plugin, i18n.translate("ms.plugin.manager.stage.disable"))
this.runCatch(plugin, 'disable')
this.runCatch(plugin, `${this.serverType}disable`)
this.unregistryCommand(plugin)
@ -147,7 +147,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
this.updatePlugin(file)
this.createPlugin(file)
} catch (ex) {
console.console(`§6Plugin §b${file.name} §6initialize error §4${ex.message}`)
console.i18n("ms.plugin.manager.initialize.error", { name: file.name, ex })
console.ex(ex)
}
}
@ -155,14 +155,15 @@ export class PluginManagerImpl implements plugin.PluginManager {
private updatePlugin(file: any) {
var update = fs.file(fs.file(file.parentFile, 'update'), file.name)
if (update.exists()) {
console.info(`Auto Update Plugin ${file.name} ...`)
console.i18n("ms.plugin.manager.build.update", { name: file.name })
fs.move(update, file, true)
}
}
private checkServers(servers: string[]) {
if (!servers) { return true }
return servers?.indexOf(this.serverType) != -1 && servers?.indexOf(`!${this.serverType}`) == -1
if (servers.indexOf(`!${this.serverType}`) != -1) { return false }
return servers?.indexOf(this.serverType) != -1
}
private createPlugin(file: string) {
@ -182,7 +183,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
this.bindPlugin(metadata)
let pluginInstance = this.container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
if (!(pluginInstance instanceof interfaces.Plugin)) {
console.console(`§4found error plugin §b${metadata.source} §4it's not extends interfaces.Plugin, the plugin will be ignore!`)
console.i18n('ms.plugin.manager.build.not.extends', { source: metadata.source })
return
}
this.pluginMap.set(metadata.name, pluginInstance)
@ -193,7 +194,7 @@ export class PluginManagerImpl implements plugin.PluginManager {
try {
let pluginInstance = this.container.getNamed<interfaces.Plugin>(plugin.Plugin, metadata.name)
if (pluginInstance.description.source + '' !== metadata.source + '') {
console.console(`§4found duplicate plugin §b${pluginInstance.description.source} §4and §b${metadata.source}§4. the first plugin will be ignore!`)
console.i18n('ms.plugin.manager.build.duplicate', { exists: pluginInstance.description.source, source: metadata.source })
}
this.container.rebind(plugin.Plugin).to(metadata.target).inSingletonScope().whenTargetNamed(metadata.name)
} catch{
@ -205,11 +206,10 @@ export class PluginManagerImpl implements plugin.PluginManager {
let configs = getPluginConfigMetadata(plugin);
for (let [_, config] of configs) {
let configFile = fs.concat(root, this.pluginFolder, plugin.description.name, config.name + '.' + config.format)
console.log(configFile)
let configFactory = getConfigLoader(config.format);
if (!fs.exists(configFile)) {
base.save(configFile, configFactory.dump(plugin[config.variable]))
console.log(`[${plugin.description.name}] config ${config.name}.${config.format} don't exists auto create from default variable...`)
console.i18n("ms.plugin.manager.config.save.default", { plugin: plugin.description.name, name: config.name, format: config.format })
} else {
plugin[config.variable] = configFactory.load(base.read(configFile));
}