/******************************************************************************* * * Nx本地化文本服务 * * * * 2021.12.10 ******************************************************************************/ const ERR = require( "error" ); const NxObject = require( "nx.object" ); const REGEXP_NUM_OR_STR = /(%d)|(%s)/; const REGEXP_STR = /%s/; var NxText = cc.Class( { extends: NxObject, name: "NxText", // 初始化 initialize: function( _args ) { // USPER if( !this._super( _args ) ) { return false; } this.libs = []; this.keymap = {}; // 全局服务开启 nx.text = this; return true; }, // 销毁 uninitialize: function() { // 全局服务关闭 nx.text = null; // USPER return this._super(); }, // 初始化准备 ready: function( _cb ) { // 先清空 this.keymap = []; // 加载base this.batchLoad( ["base"], _cb ); }, // 重建本地化库 rebuild: function( _base, _cb, _lang ) { let queue = _base ? [ _base ] : []; let lst = this.getKey( "FileList" ); if( nx.dt.strEmpty( lst ) ) { nx.error( "[文本]base没有库信息FileList参数!" ); } else { queue = queue.concat( lst.split( "," ) ); } // 批量加载 this.batchLoad( queue, _cb, _lang ); }, // 批量加载 batchLoad: function( _queue, _cb, _lang ) { if( nx.dt.arrEmpty( _queue ) ) { nx.dt.fnInvoke( _cb, nx.genError( ERR.COM.EmptyQueue ), 0, 0 ); return; } let self = this; let cnt = 0; let max = _queue.length; let queue = nx.dt.objClone( _queue ); let next = function() { let key = queue.shift(); let path = cc.path.join( "locals", _lang || nx.getLocLanguage(), "texts", key ); cc.loader.loadRes( path, cc.JsonAsset, ( _error, _asset ) => { ++cnt; if( _error ) { nx.error( "[文本]添加文本库失败: %s", path ); nx.dt.fnInvoke( _cb, _error, cnt, max ); } else { nx.debug( "[文本]添加文本库成功: %s", path ); self.pushKeys( _asset.json ); nx.dt.fnInvoke( _cb, null, cnt, max ); } if( queue.length > 0 ) { next(); } } ); }; next(); }, // 文本添加 pushKeys: function( _map ) { for( let k in _map ) { // if( this.keymap[k] ) { // nx.warn( "[文本]文本键名重复(默认替换):", k ); // } this.keymap[k] = _map[k]; } }, // 获取 getKey: function( _key, _empty = false ) { if( nx.dt.strEmpty( _key ) ) { return ""; } let ret = this.keymap[_key]; // 数字直接返回 if( nx.dt.numGood( ret ) ) { return ret; } if( ret ) { ret = ret.replace( "\\n", "\n" ); return ret; } return _empty ? "" : _key; }, // 取值 format: function() { var argLen = arguments.length; if( 0 === argLen ) { return ""; } var t = arguments[ 0 ]; if( nx.dt.strEmpty( t ) ) { return t; } var msg = this.getKey( t ); if( typeof ( msg ) != "string" ) { cc.warn( "[文本]取文本失败,没有找到:" + t ); return t; } if( 1 === argLen ) { return "" + msg; } var hasSubstitution = "string" === typeof msg && REGEXP_NUM_OR_STR.test( msg ); if( hasSubstitution ) { for( var i = 1; i < argLen; ++i ) { var arg = arguments[ i ]; var regExpToTest = "number" === typeof arg ? REGEXP_NUM_OR_STR : REGEXP_STR; regExpToTest.test( msg ) ? msg = msg.replace( regExpToTest, arg ) : msg += " " + arg; } } // **未格式化参数不再追加在传出字符串后 // else { // for( var _i = 1; _i < argLen; ++_i ) { // msg += " " + arguments[ _i ]; // } // } return msg; }, // 直接格式化 formatS: function() { var argLen = arguments.length; if( 0 === argLen ) { return ""; } var msg = arguments[ 0 ]; if( typeof ( msg ) != "string" || msg == "" ) { // cc.warn( "取文本失败,参数无效:" + msg ); return ""; } if( 1 === argLen ) { return "" + msg; } var hasSubstitution = "string" === typeof msg && REGEXP_NUM_OR_STR.test( msg ); if( hasSubstitution ) { for( var i = 1; i < argLen; ++i ) { var arg = arguments[ i ]; var regExpToTest = "number" === typeof arg ? REGEXP_NUM_OR_STR : REGEXP_STR; regExpToTest.test( msg ) ? msg = msg.replace( regExpToTest, arg ) : msg += " " + arg; } } // **未格式化参数不再追加在传出字符串后 // else { // for( var _i = 1; _i < argLen; ++_i ) { // msg += " " + arguments[ _i ]; // } // } return msg; }, // 格式化错误码 formatError: function() { var argLen = arguments.length; if( 0 === argLen ) { return this.format( "ErrBadArguments" ); } var msg = this.getKey( "ErrServer" + arguments[ 0 ], true ); if( nx.dt.strEmpty( msg ) ) { return this.format( "ErrServerUnknown", arguments[ 0 ] ); } if( 1 === argLen ) { return "" + msg; } var hasSubstitution = "string" === typeof msg && REGEXP_NUM_OR_STR.test( msg ); if( hasSubstitution ) { for( var i = 1; i < argLen; ++i ) { var arg = arguments[ i ]; var regExpToTest = "number" === typeof arg ? REGEXP_NUM_OR_STR : REGEXP_STR; regExpToTest.test( msg ) ? msg = msg.replace( regExpToTest, arg ) : msg += " " + arg; } } else { for( var _i = 1; _i < argLen; ++_i ) { msg += " " + arguments[ _i ]; } } return msg; } } ); module.exports = NxText;