/** @format */ import * as i18nLabel from '../uiutils/i18nLabel' import * as i18nSprite from '../uiutils/i18nSprite' import {Mgr} from '../GameControl' import {ccUtils} from '../utils/ccUtils' import {I18nConfig} from '../config/I18nConfig' import {Language, LANGUAGE_TYPE, LOCAL} from '../enums/Enum' import {EntryConfig} from '../config/EntryConfig' import {RechargeConfig} from '../config/RechargeConfig' export class I18nManager { private language: Language = Language.zh // 当前语言 默认中文 private labelArr: i18nLabel.i18nLabel[] = [] // i18nLabel 列表 private spriteArr: i18nSprite.i18nSprite[] = [] // i18nSprite 列表 private i18nBundle: cc.AssetManager.Bundle public async init(language: number) { let curLanguage = Mgr.storage.getString(LOCAL.selectLanguage, Object.keys(Language)[language - 1]) this.i18nBundle = await ccUtils.getBundleAsync('i18n') if (this.i18nBundle) this.setLanguage(curLanguage) return this.i18nBundle != null } /** * 设置语言 */ public setLanguage(language: Language) { if (this.language == language) { return } this.language = language this.reloadLabel() this.reloadSprite() } public getLanguage(): Language { return this.language } /** * 添加或移除 i18nLabel */ public addOrDelLabel(label: i18nLabel.i18nLabel, isAdd: boolean) { if (isAdd) { this.labelArr.push(label) } else { let index = this.labelArr.indexOf(label) if (index !== -1) { this.labelArr.splice(index, 1) } } } public getLabel(opt: string, params?: string[] | number[]): string { if (CC_EDITOR) { this.language = Language.en if (!I18nConfig[opt]) return '' } if (!params) { return (I18nConfig[opt] ? I18nConfig[opt][this.language] : opt) || opt } let str = (I18nConfig[opt] ? I18nConfig[opt][this.language] : opt) || opt for (let i = 0; i < params.length; i++) { let reg = new RegExp('#(?!color=)' + i, 'g') str = str.replace(reg, params[i].toString()) } return str } /** * 添加或移除 i18nSprite */ public addOrDelSprite(sprite: i18nSprite.i18nSprite, isAdd: boolean) { if (isAdd) { this.spriteArr.push(sprite) } else { let index = this.spriteArr.indexOf(sprite) if (index !== -1) { this.spriteArr.splice(index, 1) } } } public getSprite(path: string, cb: (spriteFrame: cc.SpriteFrame) => void) { if (!this.i18nBundle) return this.i18nBundle.load( 'sprite/' + this.language + '/' + path, cc.SpriteFrame, (err, spriteFrame: cc.SpriteFrame) => { if (err) { return cb(null) } cb(spriteFrame) }, ) } public getEntryLabel(entry: number): string { let cfg = EntryConfig[entry] if (!cfg) return '' return this.getLabel( cfg.describe, cfg.desParm.map(v => v.toString()), ) } public getTimeLabel( timeStamp: number, maxType: LANGUAGE_TYPE = LANGUAGE_TYPE.d, minType: LANGUAGE_TYPE = LANGUAGE_TYPE.m, ): string { let dhms = Date.date2DHMS(timeStamp) let dhmsArr = [dhms.day, dhms.hour, dhms.minute, dhms.second] let typeArr = [LANGUAGE_TYPE.d, LANGUAGE_TYPE.h, LANGUAGE_TYPE.m, LANGUAGE_TYPE.s] let str = '' for (let i = typeArr.indexOf(maxType); i <= typeArr.indexOf(minType); i++) { if (!str && !dhmsArr[i]) continue str += `${dhmsArr[i]}${this.getLabel(typeArr[i])}` } return str } //根据内置语言库更新金额显示模式 public getMoneyLabel(id: number): string { let str = '' switch (Mgr.storage.getString(LOCAL.selectLanguage)) { case Language.en: str = `$${RechargeConfig[id].my / 100}` break case Language.zh: str = `${RechargeConfig[id].rmb / 100}元` break default: break } return str } private reloadLabel() { for (let one of this.labelArr) { one._resetValue() } } private reloadSprite() { for (let one of this.spriteArr) { one._resetValue() } } }