"use strict";
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 "name":
return _parent ? `${_parent.name}.${name}` : name;
case "inspect":
return _parent
? () => `LocaleString[ Locale${_parent.name}.${name} ]`
: () => `LocaleString`
;
}
if( target[ prop ] == undefined )
{
target[ prop ] = ProxyLocale(
prop
, function( lang, stack )
{
if( !stack )
{
stack = stack || [ prop, name ];
}
else
{
stack[1] = name + "." + stack[1];
}
return _locale( lang, stack );
}
, _thisProxy );
}
return Reflect.get( target, prop );
}
} );
return _thisProxy;
};
const rLocale = function( lang, stack )
{
var Zone = "LocaleSX." + lang + stack[1];
try
{
// Resolve your string here, cache it if you want
console.log( "[DEBUG] " + Zone + ":" + stack[0] );
return "My awesome translated string";
}
catch( e )
{
console.log( e );
}
return Zone + "." + stack[0];
};
module.exports = ProxyLocale( "", rLocale );
"use strict";
const Locale = require( "./localization.js" );
console.log( Locale.Zone.Category.Penguin.MY_AWESOME_STRING );
console.log( Locale.Zone.Category.Penguin.MY_AWESOME_STRING + "" );
console.log( Locale.Zone.Category.Penguin.MY_AWESOME_STRING( "zh-TW" ) );
// Output
/*
LocaleString[ Locale.Zone.Category.Penguin.MY_AWESOME_STRING ]
[DBBUG] LocaleSX.en-US.Zone.Category.Penguin:MY_AWESOME_STRING
My awesome translated string
[DBBUG] LocaleSX.zh-TW.Zone.Category.Penguin:MY_AWESOME_STRING
My awesome translated string
*/