var _langCode = cc.sys.languageCode; // _langCode = 'en' //多国语言版本测试 var _lang = _langCode; // cc.log('cc.sys.languageCode : ',_langCode); var _langHead = _langCode.substring(0,2); if(_langHead == 'zh'){ if(_langCode == 'zh' || _langCode == 'zh-cn' || _langCode == 'zh-CN' ){ _lang = 'zh-CN'; }else{ _lang = 'zh-HK'; } }else{ _lang = _langHead; } /** * 5国语言 */ const LANG_TYPE = cc.Enum({ zh_CN : 'zh-CN', zh_HK : 'zh-HK', en : 'en', jo : 'jo', ko : 'ko' }); const {ccclass, property} = cc._decorator; @ccclass export default class i18n extends cc.Component{ public static data:any; onLoad(){ cc.log('当前语言:'+_lang); } __init(callback:()=>void){ //读取存档 //读取语言文件包 cc.resources.load('i18n/'+_lang, cc.JsonAsset, (err, jsonAsset:cc.JsonAsset) =>{ if(err){ cc.error(err); }else{ i18n.data = jsonAsset.json; callback(); } }) } static t (key: string, opt?: any) { if(key.length > 50){ return key } let str:string = i18n.data[key]; // cc.log('key:',key) // cc.log('opt:',opt) if(!str){ str = key; } // cc.log('str:',str) if(opt){//文字替换 let keys = Object.keys(opt); for(let id in keys){ let K = keys[id]; let V = opt[K]; str = str.replace('{'+K+'}',V); } } return str; } static lang(){ return _lang } /* * 载入多国语言图片资源 * @param path * @param callback */ static load(path,callback:(sf:cc.SpriteFrame)=>void){ cc.resources.load(path+'_'+_lang, cc.SpriteFrame, (err, sprintFrame:cc.SpriteFrame) =>{ if(err){ cc.error(err); }else{ callback(sprintFrame); } }) } }