Added Localization
This commit is contained in:
parent
153265b0c0
commit
31880a2823
@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
const cl = global.botanLoader;
|
const cl = global.botanLoader;
|
||||||
|
|
||||||
const events = require( "events" );
|
const EventEmitter = require( "events" ).EventEmitter;
|
||||||
const util = require( "util" );
|
const util = require( "util" );
|
||||||
|
|
||||||
var Infrastructure = function()
|
class Infrastructure extends EventEmitter
|
||||||
{
|
{
|
||||||
events.EventEmitter.call( this );
|
constructor()
|
||||||
|
{
|
||||||
|
super();
|
||||||
var _self = this;
|
var _self = this;
|
||||||
|
|
||||||
var __readyList = [];
|
var __readyList = [];
|
||||||
@ -35,14 +37,10 @@ var Infrastructure = function()
|
|||||||
|
|
||||||
this.APIs = {};
|
this.APIs = {};
|
||||||
this.isReady = true;
|
this.isReady = true;
|
||||||
};
|
}
|
||||||
|
|
||||||
util.inherits( Infrastructure, events.EventEmitter );
|
|
||||||
|
|
||||||
Infrastructure.prototype.callAPI = function( name )
|
|
||||||
{
|
|
||||||
var _self = this;
|
|
||||||
|
|
||||||
|
callAPI( name )
|
||||||
|
{
|
||||||
var propName = name.split( "." );
|
var propName = name.split( "." );
|
||||||
propName = name[ name.length - 1 ];
|
propName = name[ name.length - 1 ];
|
||||||
|
|
||||||
@ -72,13 +70,13 @@ Infrastructure.prototype.callAPI = function( name )
|
|||||||
}
|
}
|
||||||
|
|
||||||
return aclass;
|
return aclass;
|
||||||
};
|
}
|
||||||
|
|
||||||
Infrastructure.prototype.Ready = function( callback )
|
Ready( callback )
|
||||||
{
|
{
|
||||||
var _self = this;
|
|
||||||
if( this.isReady ) callback();
|
if( this.isReady ) callback();
|
||||||
else this.once( "apis_ready", () => callback() );
|
else this.once( "apis_ready", () => callback() );
|
||||||
};
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = Infrastructure;
|
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