123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2", "__unresolved_3", "__unresolved_4", "__unresolved_5"], function (_export, _context) {
- "use strict";
- var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, director, Polyglot, Singleton, LocalizedLabel, LocalizedSprite, Constants, i18nCls, _crd, ccclass, property, curLang, langCacheKey, instance, i18nFn, i18n;
- function _reportPossibleCrUseOfPolyglot(extras) {
- _reporterNs.report("Polyglot", "./polyglot.min", _context.meta, extras);
- }
- function _reportPossibleCrUseOfSingleton(extras) {
- _reporterNs.report("Singleton", "../../manager/Singleton", _context.meta, extras);
- }
- function _reportPossibleCrUseOfLocalizedLabel(extras) {
- _reporterNs.report("LocalizedLabel", "./LocalizedLabel", _context.meta, extras);
- }
- function _reportPossibleCrUseOfLocalizedSprite(extras) {
- _reporterNs.report("LocalizedSprite", "./LocalizedSprite", _context.meta, extras);
- }
- function _reportPossibleCrUseOfConstants(extras) {
- _reporterNs.report("Constants", "../../../data/Constants", _context.meta, extras);
- }
- return {
- setters: [function (_unresolved_) {
- _reporterNs = _unresolved_;
- }, function (_cc) {
- _cclegacy = _cc.cclegacy;
- __checkObsolete__ = _cc.__checkObsolete__;
- __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
- _decorator = _cc._decorator;
- director = _cc.director;
- }, function (_unresolved_2) {
- Polyglot = _unresolved_2.Polyglot;
- }, function (_unresolved_3) {
- Singleton = _unresolved_3.Singleton;
- }, function (_unresolved_4) {
- LocalizedLabel = _unresolved_4.default;
- }, function (_unresolved_5) {
- LocalizedSprite = _unresolved_5.default;
- }, function (_unresolved_6) {
- Constants = _unresolved_6.Constants;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "d6597NeRz5OuZ6kceJkHWc3", "LanguageData", undefined);
- __checkObsolete__(['_decorator']);
- __checkObsolete__(['director']);
- ({
- ccclass,
- property
- } = _decorator); //默认的多语言 必须是存在于数组i18n.languages中的
- curLang = 'en'; //缓存选择的多语言的key
- langCacheKey = (_crd && Constants === void 0 ? (_reportPossibleCrUseOfConstants({
- error: Error()
- }), Constants) : Constants).gameName + "CurLang";
- /**事例
- * 1、切换语言调用:
- * let languages = Object.keys(i18n.languages);
- let count: number = (languages.indexOf(i18n.curLang) + 1) % languages.length;
- i18n.init(languages[count]);
-
- 2、切换了语言整个页面刷新语言 调用:
- i18n.updateSceneRenderers();
-
- 3、lable多语言 直接挂载LocalizedLabel类 到lable节点上 然后填入id,
- 这个id是多语言中i18n.languages.en 例如
- "lottery": {
- "title": "Wheel of fortune",
- "free": "Free"
- },就是 填入“lottery.title”
-
- 4、打当lable文本是动态的时候,比如”你完成了多少个个订单“ 这儿就需要你在当前所在的类的,
- 就是这个空间的所在的页面调用 this.lbTakeCount.string = i18n("balance.你完成了%{value}个订单", { value: take});
- 传入动态的实际参数,然后在这个lable上就不需要在去挂载 LocalizedLabel类
-
- 5、图片多语言 直接挂载LocalizedSprite类 到对应的sprite节点上去
- 然后根据languages.length的列表填入如 “en” 然后拖入英文的图片,然后“zh” 拖入中文的图片
- */
- i18nCls = class i18nCls extends (_crd && Singleton === void 0 ? (_reportPossibleCrUseOfSingleton({
- error: Error()
- }), Singleton) : Singleton) {
- //当前语言 默认语言中文
- get curLang() {
- var _localStorage$getItem;
- return (_localStorage$getItem = localStorage.getItem(langCacheKey)) != null ? _localStorage$getItem : curLang;
- } //设置当前语言并更新到 localStorage
- set curLang(language) {
- var cur = this.curLang;
- if (cur !== language) {
- //将语言存储到localStorage
- localStorage.setItem(langCacheKey, language); //初始化语言
- this.init(language);
- }
- }
- constructor() {
- super();
- this.inst = null;
- //多语言数组
- this.languages = {
- en: {},
- zh: {}
- };
- this.init();
- }
- initPolyglot(data) {
- if (data) {
- if (this.inst) {
- this.inst.replace(data);
- } else {
- this.inst = new (_crd && Polyglot === void 0 ? (_reportPossibleCrUseOfPolyglot({
- error: Error()
- }), Polyglot) : Polyglot)({
- phrases: data,
- allowMissing: true
- });
- }
- }
- }
- /**
- * 当前语言是否是中文
- */
- get isZh() {
- return this.curLang === "zh";
- }
- /**
- * This method allow you to switch language during runtime, language argument should be the same as your data file name
- * such as when language is 'zh', it will load your 'zh.js' data source.
- * @method init
- * @param language - the language specific data file name, such as 'zh' to load 'zh.js'
- */
- init(language) {
- if (!language || language === this.curLang) {
- return;
- }
- var data = this.languages[language] || {};
- this.curLang = language;
- this.initPolyglot(data);
- }
- /**
- * this method takes a text key as input, and return the localized string
- * Please read https://github.com/airbnb/polyglot.js for details
- * @method t
- * @return {String} localized string
- * @example
- *
- * var myText = i18n.t('MY_TEXT_KEY');
- *
- * // if your data source is defined as
- * // {"hello_name": "Hello, %{name}"}
- * // you can use the following to interpolate the text
- * var greetingText = i18n.t('hello_name', {name: 'nantas'}); // Hello, nantas
- */
- t(key, opt) {
- var _this$inst;
- //检查数据是否加载
- if (!this.inst || Object.keys(this.inst.phrases).length === 0) {
- var data = this.languages[this.curLang] || {};
- this.initPolyglot(data);
- } //调用 Polyglot 的翻译方法
- return ((_this$inst = this.inst) == null ? void 0 : _this$inst.t(key, opt)) || key;
- }
- /**
- * inst: polyInst
- * 是遍历场景中所有的 LocalizedLabel 和 LocalizedSprite 组件,并更新它们的显示内容
- */
- updateSceneRenderers() {
- // very costly iterations
- var rootNodes = director.getScene().children; // walk all nodes with localize label and update
- var allLocalizedLabels = [];
- for (var i = 0; i < rootNodes.length; ++i) {
- var labels = rootNodes[i].getComponentsInChildren(_crd && LocalizedLabel === void 0 ? (_reportPossibleCrUseOfLocalizedLabel({
- error: Error()
- }), LocalizedLabel) : LocalizedLabel);
- Array.prototype.push.apply(allLocalizedLabels, labels);
- }
- for (var _i = 0; _i < allLocalizedLabels.length; ++_i) {
- var label = allLocalizedLabels[_i];
- if (!label.node.active) continue;
- label.updateLabel();
- } //walk all nodes with localize sprite and update
- var allLocalizedSprites = [];
- for (var _i2 = 0; _i2 < rootNodes.length; ++_i2) {
- var sprites = rootNodes[_i2].getComponentsInChildren(_crd && LocalizedSprite === void 0 ? (_reportPossibleCrUseOfLocalizedSprite({
- error: Error()
- }), LocalizedSprite) : LocalizedSprite);
- Array.prototype.push.apply(allLocalizedSprites, sprites);
- }
- for (var _i3 = 0; _i3 < allLocalizedSprites.length; ++_i3) {
- var sprite = allLocalizedSprites[_i3];
- if (!sprite.node.active) continue;
- sprite.updateSprite(this.curLang);
- }
- }
- };
- ;
- instance = i18nCls.ins(); //使用函数保持this 绑定
- i18nFn = (key, opt) => instance.t(key, opt); //拷贝所有方法到函数对象
- i18n = Object.setPrototypeOf(i18nFn, instance); //导出类型声明保持不变
- _export("default", i18n);
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=5f3a600c0c9c8fc3914f60945ed41a9cd735411b.js.map
|