12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- var _langCode = cc.sys.languageCode;
- var _lang = _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;
- }
- 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];
-
-
- if(!str){
- str = key;
- }
-
- 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
- }
-
- 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);
- }
- })
- }
- }
|