Added Localization
This commit is contained in:
parent
153265b0c0
commit
31880a2823
@ -2,83 +2,81 @@
|
||||
|
||||
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 );
|
||||
var _self = this;
|
||||
constructor()
|
||||
{
|
||||
super();
|
||||
var _self = this;
|
||||
|
||||
var __readyList = [];
|
||||
this.readyList = new Proxy( __readyList, {
|
||||
var __readyList = [];
|
||||
this.readyList = new Proxy( __readyList, {
|
||||
|
||||
get: ( target, prop ) => Reflect.get( target, prop )
|
||||
get: ( target, prop ) => Reflect.get( target, prop )
|
||||
|
||||
, set: ( target, prop, value ) => {
|
||||
, set: ( target, prop, value ) => {
|
||||
|
||||
// Have to emit the event at next tick
|
||||
// due to error thrown from the handlers
|
||||
// hangs the process
|
||||
process.nextTick( () => {
|
||||
if( __readyList.every( ( v ) => v === true ) )
|
||||
{
|
||||
_self.isReady = true;
|
||||
_self.emit( "apis_ready", _self )
|
||||
}
|
||||
} );
|
||||
// Have to emit the event at next tick
|
||||
// due to error thrown from the handlers
|
||||
// hangs the process
|
||||
process.nextTick( () => {
|
||||
if( __readyList.every( ( v ) => v === true ) )
|
||||
{
|
||||
_self.isReady = true;
|
||||
_self.emit( "apis_ready", _self )
|
||||
}
|
||||
} );
|
||||
|
||||
return Reflect.set( target, prop, value );
|
||||
return Reflect.set( target, prop, value );
|
||||
}
|
||||
|
||||
} );
|
||||
|
||||
this.APIs = {};
|
||||
this.isReady = true;
|
||||
}
|
||||
|
||||
callAPI( name )
|
||||
{
|
||||
var propName = name.split( "." );
|
||||
propName = name[ name.length - 1 ];
|
||||
|
||||
if( this.APIs[ propName ] ) return this.APIs[ propName ];
|
||||
|
||||
var path = name.replace( ".", "/" );
|
||||
|
||||
var API = cl.load( name );
|
||||
|
||||
if( !API.create )
|
||||
{
|
||||
throw new Error( "API does not have support calling" );
|
||||
}
|
||||
|
||||
} );
|
||||
var aclass = API.create( Array.prototype.slice.call( arguments, 1 ) );
|
||||
|
||||
this.APIs = {};
|
||||
this.isReady = true;
|
||||
};
|
||||
this.APIs[ propName ] = aclass;
|
||||
|
||||
util.inherits( Infrastructure, events.EventEmitter );
|
||||
if( aclass.ready !== undefined )
|
||||
{
|
||||
this.isReady = false;
|
||||
|
||||
Infrastructure.prototype.callAPI = function( name )
|
||||
{
|
||||
var _self = this;
|
||||
var rList = this.readyList;
|
||||
rList.push( aclass.ready || aclass );
|
||||
|
||||
var propName = name.split( "." );
|
||||
propName = name[ name.length - 1 ];
|
||||
aclass.once( "ready", ( e ) => rList[ rList.indexOf( e ) ] = true );
|
||||
}
|
||||
|
||||
if( this.APIs[ propName ] ) return this.APIs[ propName ];
|
||||
|
||||
var path = name.replace( ".", "/" );
|
||||
|
||||
var API = cl.load( name );
|
||||
|
||||
if( !API.create )
|
||||
{
|
||||
throw new Error( "API does not have support calling" );
|
||||
return aclass;
|
||||
}
|
||||
|
||||
var aclass = API.create( Array.prototype.slice.call( arguments, 1 ) );
|
||||
|
||||
this.APIs[ propName ] = aclass;
|
||||
|
||||
if( aclass.ready !== undefined )
|
||||
Ready( callback )
|
||||
{
|
||||
this.isReady = false;
|
||||
|
||||
var rList = this.readyList;
|
||||
rList.push( aclass.ready || aclass );
|
||||
|
||||
aclass.once( "ready", ( e ) => rList[ rList.indexOf( e ) ] = true );
|
||||
if( this.isReady ) callback();
|
||||
else this.once( "apis_ready", () => callback() );
|
||||
}
|
||||
|
||||
return aclass;
|
||||
};
|
||||
|
||||
Infrastructure.prototype.Ready = function( callback )
|
||||
{
|
||||
var _self = this;
|
||||
if( this.isReady ) callback();
|
||||
else this.once( "apis_ready", () => callback() );
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = Infrastructure;
|
||||
|
76
localization.js
Normal file
76
localization.js
Normal 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 );
|
Loading…
Reference in New Issue
Block a user