I18n.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Learn TypeScript:
  2. import Zh from './language/Zh';
  3. import En from './language/En';
  4. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
  5. // Learn Attribute:
  6. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html
  9. import executeInEditMode = cc._decorator.executeInEditMode;
  10. const {ccclass, property} = cc._decorator;
  11. @ccclass
  12. export default class I18n
  13. {
  14. public static language:string="en";
  15. private static obj:any;
  16. public static ready:boolean=false;
  17. public static init(l:string)
  18. {
  19. this.ready=true;
  20. this.language= l;
  21. if(this.obj==null)
  22. {
  23. this.obj={};
  24. this.obj.zh=Zh.zh;
  25. this.obj.en=En.en;
  26. }
  27. }
  28. public static t(key)
  29. {
  30. if(this.ready==false)
  31. {
  32. this.init(this.language);
  33. }
  34. let data=this.obj[this.language][key];
  35. if(data==null)
  36. {
  37. return "";
  38. }
  39. return data;
  40. }
  41. // 更新场景渲染器
  42. public static updateSceneRenderers()
  43. {
  44. let rootNodes = cc.director.getScene()!.children;
  45. // walk all nodes with localize label and update
  46. let allLocalizedLabels: any[] = [];
  47. // 遍历所有节点获取LocalizedLabel组件
  48. for (let i = 0; i < rootNodes.length; ++i) {
  49. let labels = rootNodes[i].getComponentsInChildren('I18nLable');
  50. allLocalizedLabels = allLocalizedLabels.concat(labels);
  51. }
  52. // 更新内容显示
  53. for (let i = 0; i < allLocalizedLabels.length; ++i) {
  54. let label = allLocalizedLabels[i];
  55. if(!label.node.active)continue;
  56. label.updateLabel();
  57. }
  58. }
  59. }