5f3a600c0c9c8fc3914f60945ed41a9cd735411b.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2", "__unresolved_3", "__unresolved_4", "__unresolved_5"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, director, Polyglot, Singleton, LocalizedLabel, LocalizedSprite, Constants, i18nCls, _crd, ccclass, property, curLang, langCacheKey, instance, i18nFn, i18n;
  4. function _reportPossibleCrUseOfPolyglot(extras) {
  5. _reporterNs.report("Polyglot", "./polyglot.min", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfSingleton(extras) {
  8. _reporterNs.report("Singleton", "../../manager/Singleton", _context.meta, extras);
  9. }
  10. function _reportPossibleCrUseOfLocalizedLabel(extras) {
  11. _reporterNs.report("LocalizedLabel", "./LocalizedLabel", _context.meta, extras);
  12. }
  13. function _reportPossibleCrUseOfLocalizedSprite(extras) {
  14. _reporterNs.report("LocalizedSprite", "./LocalizedSprite", _context.meta, extras);
  15. }
  16. function _reportPossibleCrUseOfConstants(extras) {
  17. _reporterNs.report("Constants", "../../../data/Constants", _context.meta, extras);
  18. }
  19. return {
  20. setters: [function (_unresolved_) {
  21. _reporterNs = _unresolved_;
  22. }, function (_cc) {
  23. _cclegacy = _cc.cclegacy;
  24. __checkObsolete__ = _cc.__checkObsolete__;
  25. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  26. _decorator = _cc._decorator;
  27. director = _cc.director;
  28. }, function (_unresolved_2) {
  29. Polyglot = _unresolved_2.Polyglot;
  30. }, function (_unresolved_3) {
  31. Singleton = _unresolved_3.Singleton;
  32. }, function (_unresolved_4) {
  33. LocalizedLabel = _unresolved_4.default;
  34. }, function (_unresolved_5) {
  35. LocalizedSprite = _unresolved_5.default;
  36. }, function (_unresolved_6) {
  37. Constants = _unresolved_6.Constants;
  38. }],
  39. execute: function () {
  40. _crd = true;
  41. _cclegacy._RF.push({}, "d6597NeRz5OuZ6kceJkHWc3", "LanguageData", undefined);
  42. __checkObsolete__(['_decorator']);
  43. __checkObsolete__(['director']);
  44. ({
  45. ccclass,
  46. property
  47. } = _decorator); //默认的多语言 必须是存在于数组i18n.languages中的
  48. curLang = 'en'; //缓存选择的多语言的key
  49. langCacheKey = (_crd && Constants === void 0 ? (_reportPossibleCrUseOfConstants({
  50. error: Error()
  51. }), Constants) : Constants).gameName + "CurLang";
  52. /**事例
  53. * 1、切换语言调用:
  54. * let languages = Object.keys(i18n.languages);
  55. let count: number = (languages.indexOf(i18n.curLang) + 1) % languages.length;
  56. i18n.init(languages[count]);
  57. 2、切换了语言整个页面刷新语言 调用:
  58. i18n.updateSceneRenderers();
  59. 3、lable多语言 直接挂载LocalizedLabel类 到lable节点上 然后填入id,
  60. 这个id是多语言中i18n.languages.en 例如
  61. "lottery": {
  62. "title": "Wheel of fortune",
  63. "free": "Free"
  64. },就是 填入“lottery.title”
  65. 4、打当lable文本是动态的时候,比如”你完成了多少个个订单“ 这儿就需要你在当前所在的类的,
  66. 就是这个空间的所在的页面调用 this.lbTakeCount.string = i18n("balance.你完成了%{value}个订单", { value: take});
  67. 传入动态的实际参数,然后在这个lable上就不需要在去挂载 LocalizedLabel类
  68. 5、图片多语言 直接挂载LocalizedSprite类 到对应的sprite节点上去
  69. 然后根据languages.length的列表填入如 “en” 然后拖入英文的图片,然后“zh” 拖入中文的图片
  70. */
  71. i18nCls = class i18nCls extends (_crd && Singleton === void 0 ? (_reportPossibleCrUseOfSingleton({
  72. error: Error()
  73. }), Singleton) : Singleton) {
  74. //当前语言 默认语言中文
  75. get curLang() {
  76. var _localStorage$getItem;
  77. return (_localStorage$getItem = localStorage.getItem(langCacheKey)) != null ? _localStorage$getItem : curLang;
  78. } //设置当前语言并更新到 localStorage
  79. set curLang(language) {
  80. var cur = this.curLang;
  81. if (cur !== language) {
  82. //将语言存储到localStorage
  83. localStorage.setItem(langCacheKey, language); //初始化语言
  84. this.init(language);
  85. }
  86. }
  87. constructor() {
  88. super();
  89. this.inst = null;
  90. //多语言数组
  91. this.languages = {
  92. en: {},
  93. zh: {}
  94. };
  95. this.init();
  96. }
  97. initPolyglot(data) {
  98. if (data) {
  99. if (this.inst) {
  100. this.inst.replace(data);
  101. } else {
  102. this.inst = new (_crd && Polyglot === void 0 ? (_reportPossibleCrUseOfPolyglot({
  103. error: Error()
  104. }), Polyglot) : Polyglot)({
  105. phrases: data,
  106. allowMissing: true
  107. });
  108. }
  109. }
  110. }
  111. /**
  112. * 当前语言是否是中文
  113. */
  114. get isZh() {
  115. return this.curLang === "zh";
  116. }
  117. /**
  118. * This method allow you to switch language during runtime, language argument should be the same as your data file name
  119. * such as when language is 'zh', it will load your 'zh.js' data source.
  120. * @method init
  121. * @param language - the language specific data file name, such as 'zh' to load 'zh.js'
  122. */
  123. init(language) {
  124. if (!language || language === this.curLang) {
  125. return;
  126. }
  127. var data = this.languages[language] || {};
  128. this.curLang = language;
  129. this.initPolyglot(data);
  130. }
  131. /**
  132. * this method takes a text key as input, and return the localized string
  133. * Please read https://github.com/airbnb/polyglot.js for details
  134. * @method t
  135. * @return {String} localized string
  136. * @example
  137. *
  138. * var myText = i18n.t('MY_TEXT_KEY');
  139. *
  140. * // if your data source is defined as
  141. * // {"hello_name": "Hello, %{name}"}
  142. * // you can use the following to interpolate the text
  143. * var greetingText = i18n.t('hello_name', {name: 'nantas'}); // Hello, nantas
  144. */
  145. t(key, opt) {
  146. var _this$inst;
  147. //检查数据是否加载
  148. if (!this.inst || Object.keys(this.inst.phrases).length === 0) {
  149. var data = this.languages[this.curLang] || {};
  150. this.initPolyglot(data);
  151. } //调用 Polyglot 的翻译方法
  152. return ((_this$inst = this.inst) == null ? void 0 : _this$inst.t(key, opt)) || key;
  153. }
  154. /**
  155. * inst: polyInst
  156. * 是遍历场景中所有的 LocalizedLabel 和 LocalizedSprite 组件,并更新它们的显示内容
  157. */
  158. updateSceneRenderers() {
  159. // very costly iterations
  160. var rootNodes = director.getScene().children; // walk all nodes with localize label and update
  161. var allLocalizedLabels = [];
  162. for (var i = 0; i < rootNodes.length; ++i) {
  163. var labels = rootNodes[i].getComponentsInChildren(_crd && LocalizedLabel === void 0 ? (_reportPossibleCrUseOfLocalizedLabel({
  164. error: Error()
  165. }), LocalizedLabel) : LocalizedLabel);
  166. Array.prototype.push.apply(allLocalizedLabels, labels);
  167. }
  168. for (var _i = 0; _i < allLocalizedLabels.length; ++_i) {
  169. var label = allLocalizedLabels[_i];
  170. if (!label.node.active) continue;
  171. label.updateLabel();
  172. } //walk all nodes with localize sprite and update
  173. var allLocalizedSprites = [];
  174. for (var _i2 = 0; _i2 < rootNodes.length; ++_i2) {
  175. var sprites = rootNodes[_i2].getComponentsInChildren(_crd && LocalizedSprite === void 0 ? (_reportPossibleCrUseOfLocalizedSprite({
  176. error: Error()
  177. }), LocalizedSprite) : LocalizedSprite);
  178. Array.prototype.push.apply(allLocalizedSprites, sprites);
  179. }
  180. for (var _i3 = 0; _i3 < allLocalizedSprites.length; ++_i3) {
  181. var sprite = allLocalizedSprites[_i3];
  182. if (!sprite.node.active) continue;
  183. sprite.updateSprite(this.curLang);
  184. }
  185. }
  186. };
  187. ;
  188. instance = i18nCls.ins(); //使用函数保持this 绑定
  189. i18nFn = (key, opt) => instance.t(key, opt); //拷贝所有方法到函数对象
  190. i18n = Object.setPrototypeOf(i18nFn, instance); //导出类型声明保持不变
  191. _export("default", i18n);
  192. _cclegacy._RF.pop();
  193. _crd = false;
  194. }
  195. };
  196. });
  197. //# sourceMappingURL=5f3a600c0c9c8fc3914f60945ed41a9cd735411b.js.map