From 2693a22bf0c9c967945e5ce0706e39a708153582 Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Thu, 27 Feb 2020 01:58:23 +0800 Subject: [PATCH] feat: support i18n translate Signed-off-by: MiaoWoo --- packages/api/src/console.ts | 10 +++++++--- packages/core/src/index.ts | 22 ++++++++++++---------- packages/ployfill/src/global.ts | 1 + packages/ployfill/src/index.ts | 6 ++++-- packages/plugin/src/manager.ts | 23 ++++++++++++----------- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/packages/api/src/console.ts b/packages/api/src/console.ts index 2fa2e833..a2137d10 100644 --- a/packages/api/src/console.ts +++ b/packages/api/src/console.ts @@ -1,3 +1,4 @@ +import i18m from '@ms/i18n' import { SourceMapBuilder } from 'source-map-builder' const Arrays = Java.type('java.util.Arrays'); @@ -78,6 +79,9 @@ export class MiaoScriptConsole implements Console { console(...args) { this.info(args) } + i18n(name: string, param?: { [key: string]: any }) { + this.log(i18m.translate(name, param)) + } object(obj) { for (var i in obj) { this.info(i, '=>', obj[i]) @@ -101,9 +105,9 @@ export class MiaoScriptConsole implements Console { if (this.sourceMaps[fileName]) { var sourceMapping = this.sourceMaps[fileName].getSource(lineNumber, 0); if (sourceMapping) { - if(lineNumber != sourceMapping.mapping.sourceLine){ - fileName = fileName.replace(".js", ".ts"); - lineNumber = sourceMapping.mapping.sourceLine; + if (lineNumber != sourceMapping.mapping.sourceLine) { + fileName = fileName.replace(".js", ".ts"); + lineNumber = sourceMapping.mapping.sourceLine; } } } diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index af3f93ee..68c24d63 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,9 +1,11 @@ import '@ms/ployfill' -let containerStartTime = new Date().getTime() -console.log(`Initialization MiaoScript IOC Container @ms/container. Please wait...`) +import i18n from '@ms/i18n' + +let containerStartTime = Date.now(); +console.i18n("ms.core.ioc.initialize"); import { plugin, server, task } from '@ms/api' import { DefaultContainer as container, inject, provideSingleton, ContainerInstance, buildProviderModule } from '@ms/container' -console.log('MiaoScript IOC Container @ms/container loading completed(' + (new Date().getTime() - containerStartTime) / 1000 + 's)!'); +console.i18n("ms.core.ioc.completed", { time: (Date.now() - containerStartTime) / 1000 }) @provideSingleton(MiaoScriptCore) class MiaoScriptCore { @@ -38,16 +40,16 @@ class MiaoScriptCore { loadPlugins() { let loadPluginStartTime = new Date().getTime() - console.log(`Initialization MiaoScript Plugin System. Please wait...`) + console.i18n("ms.core.plugin.initialize") this.pluginManager.scan(this.pluginFolder); this.pluginManager.build(); this.pluginManager.load(); this.pluginManager.enable(); - console.log('MiaoScript Plugin System loading completed(' + (new Date().getTime() - loadPluginStartTime) / 1000 + 's)!'); + console.i18n("ms.core.plugin.completed", { time: (new Date().getTime() - loadPluginStartTime) / 1000 }) } disable() { - console.log("Disable MiaoScript Engine...") + console.i18n("ms.core.engine.disable") this.pluginManager.disable(); } } @@ -82,15 +84,15 @@ function initialize() { container.bind(plugin.PluginInstance).toConstantValue(base.getInstance()); container.bind(plugin.PluginFolder).toConstantValue('plugins'); let type = detectServer(); - console.info(`Detect Compatible Server set ServerType to ${type} ...`) + console.i18n("ms.core.initialize.detect", { type }); container.bind(server.ServerType).toConstantValue(type); - console.log(`Initialization MiaoScript Package @ms/core @ms/${type} @ms/plugin. Please wait...`) + console.i18n("ms.core.package.initialize", { type }); require(`@ms/${type}`).default(container); require('@ms/plugin') container.load(buildProviderModule()); - console.log(`MiaoScript Package @ms/core @ms/${type} @ms/plugin loading completed(` + (new Date().getTime() - corePackageStartTime) / 1000 + 's)!'); + console.i18n("ms.core.package.completed", { type, time: (Date.now() - corePackageStartTime) / 1000 }); let disable = container.get(MiaoScriptCore).enable() - console.log('MiaoScript ScriptEngine loading completed... Done (' + (new Date().getTime() - global.NashornEngineStartTime) / 1000 + 's)!'); + console.i18n("ms.core.engine.completed", { time: (Date.now() - corePackageStartTime) / 1000 }); return disable; } diff --git a/packages/ployfill/src/global.ts b/packages/ployfill/src/global.ts index 6b316128..baf41009 100644 --- a/packages/ployfill/src/global.ts +++ b/packages/ployfill/src/global.ts @@ -27,6 +27,7 @@ declare global { stack(err: Error): string[]; sender(...args: any): void; console(...args: any): void; + i18n(name: string, ...params: any[]); } } export { } \ No newline at end of file diff --git a/packages/ployfill/src/index.ts b/packages/ployfill/src/index.ts index 4b7d07e7..276dea78 100644 --- a/packages/ployfill/src/index.ts +++ b/packages/ployfill/src/index.ts @@ -1,10 +1,12 @@ /// /// +import i18n from '@ms/i18n' let ployfillStartTime = new Date().getTime(); -console.info('Initialization Java Nashorn ployfill. Please wait...'); +i18n.initialize(); +console.i18n("ms.ployfill.initialize"); require('./es5-ext'); require('core-js'); global.setGlobal('Proxy', require('./proxy').Proxy) global.setGlobal('XMLHttpRequest', require('./xml-http-request').XMLHttpRequest) global.setGlobal('Blob', require('blob-polyfill').Blob) -console.info('Java Nashorn ployfill loading completed... Cost (' + (new Date().getTime() - ployfillStartTime) / 1000 + 's)!'); +console.i18n("ms.ployfill.completed", { time: (new Date().getTime() - ployfillStartTime) / 1000 }); diff --git a/packages/plugin/src/manager.ts b/packages/plugin/src/manager.ts index 607d98e7..fc35af62 100644 --- a/packages/plugin/src/manager.ts +++ b/packages/plugin/src/manager.ts @@ -1,3 +1,4 @@ +import i18n from '@ms/i18n' import { plugin, server, command, event } from '@ms/api' import { inject, provideSingleton, postConstruct, Container, ContainerInstance } from '@ms/container' import * as fs from '@ms/common/dist/fs' @@ -27,9 +28,9 @@ export class PluginManagerImpl implements plugin.PluginManager { initialize() { if (this.pluginInstance !== null) { // 如果plugin不等于null 则代表是正式环境 - console.info(`Initialization MiaoScript Plugin System: ${this.pluginInstance} ...`) + console.i18n('ms.plugin.initialize', { plugin: this.pluginInstance }) this.pluginMap = new Map() - console.info(`${this.EventManager.mapEventName().toFixed(0)} ${this.serverType} Event Mapping Complate...`) + console.i18n('ms.plugin.event.map', { count: this.EventManager.mapEventName().toFixed(0), type: this.serverType }); } } @@ -37,9 +38,9 @@ export class PluginManagerImpl implements plugin.PluginManager { var plugin = fs.file(root, folder) var files = [] // load common plugin - .concat(this.scanFloder(plugin)) + .concat(this.scanFolder(plugin)) // load space plugin - .concat(this.scanFloder(fs.file(plugin, this.serverType))) + .concat(this.scanFolder(fs.file(plugin, this.serverType))) this.loadPlugins(files) } @@ -53,7 +54,7 @@ export class PluginManagerImpl implements plugin.PluginManager { load(...args: any[]): void { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { - this.logStage(plugin, "Loading") + this.logStage(plugin, i18n.translate("ms.plugin.stage.load")) this.loadConfig(plugin) this.runCatch(plugin, 'load') this.runCatch(plugin, `${this.serverType}load`) @@ -62,7 +63,7 @@ export class PluginManagerImpl implements plugin.PluginManager { enable(...args: any[]): void { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { - this.logStage(plugin, "Enabling") + this.logStage(plugin, i18n.translate("ms.plugin.stage.enable")) this.runCatch(plugin, 'enable') this.runCatch(plugin, `${this.serverType}enable`) this.registryCommand(plugin) @@ -72,7 +73,7 @@ export class PluginManagerImpl implements plugin.PluginManager { disable(...args: any[]): void { this.checkAndGet(args[0]).forEach((plugin: interfaces.Plugin) => { - this.logStage(plugin, "Disabling") + this.logStage(plugin, i18n.translate("ms.plugin.stage.disable")) this.runCatch(plugin, 'disable') this.runCatch(plugin, `${this.serverType}disable`) this.unregistryCommand(plugin) @@ -110,12 +111,12 @@ export class PluginManagerImpl implements plugin.PluginManager { throw new Error(`Plugin ${JSON.stringify(name)} not exist!`) } - private scanFloder(plugin: any): string[] { + private scanFolder(folder: any): string[] { var files = [] - console.info(`Scanning Plugins in ${plugin} ...`) - this.checkUpdateFolder(plugin) + console.i18n('ms.plugin.manager.scan', { folder }) + this.checkUpdateFolder(folder) // must check file is exist maybe is a illegal symbolic link file - fs.list(plugin).forEach((file: any) => file.toFile().exists() ? files.push(file.toFile()) : void 0) + fs.list(folder).forEach((file: any) => file.toFile().exists() ? files.push(file.toFile()) : void 0) return files }