Added Localization

This commit is contained in:
斟酌 鵬兄 2016-07-03 00:04:00 +08:00
parent 153265b0c0
commit 31880a2823
2 changed files with 133 additions and 59 deletions

View File

@ -2,12 +2,14 @@
const cl = global.botanLoader;
const events = require( "events" );
const EventEmitter = require( "events" ).EventEmitter;
const util = require( "util" );
var Infrastructure = function()
class Infrastructure extends EventEmitter
{
events.EventEmitter.call( this );
constructor()
{
super();
var _self = this;
var __readyList = [];
@ -35,14 +37,10 @@ var Infrastructure = function()
this.APIs = {};
this.isReady = true;
};
}
util.inherits( Infrastructure, events.EventEmitter );
Infrastructure.prototype.callAPI = function( name )
callAPI( name )
{
var _self = this;
var propName = name.split( "." );
propName = name[ name.length - 1 ];
@ -72,13 +70,13 @@ Infrastructure.prototype.callAPI = function( name )
}
return aclass;
};
}
Infrastructure.prototype.Ready = function( callback )
Ready( callback )
{
var _self = this;
if( this.isReady ) callback();
else this.once( "apis_ready", () => callback() );
};
}
}
module.exports = Infrastructure;

76
localization.js Normal file
View File

@ -0,0 +1,76 @@
"use strict";
const cl = global.botanLoader;
const Dragonfly = global.Dragonfly;
const ProxyLocale = function( name, _locale, _parent )
{
var _thisProxy = new Proxy( _locale, {
get: ( target, prop ) => {
switch( prop )
{
case Symbol.toPrimitive:
return () => _locale( global.lang || "en-US" );
case "inspect":
return _parent
? () => `LocaleString[ ${_parent}.${name} ]`
: () => `LocaleString[ ${name} ]`
;
}
if( target[ prop ] == undefined )
{
target[ prop ] = ProxyLocale(
prop
, function( lang, stack )
{
if( !stack )
{
stack = stack || [ prop, "" ];
}
else
{
stack[1] = stack[1] + "." + prop;
}
return _locale( lang, stack );
}
, _thisProxy );
}
return Reflect.get( target, prop );
}
} );
return _thisProxy;
};
const rLocale = function( lang, stack )
{
var Zone = "LocaleSX." + lang + stack[1];
try
{
var zoneFile = cl.load( Zone );
var translated = zoneFile[ stack[0] ];
if( translated == undefined )
{
throw new Error( `Translation does not exists: ${Zone}[ ${stack[0]} ]` );
}
return translated;
}
catch( e )
{
Dragonfly.Warning( e );
}
return Zone + "." + stack[0];
};
module.exports = ProxyLocale( "", rLocale );