I18nManager.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /** @format */
  2. import * as i18nLabel from '../uiutils/i18nLabel'
  3. import * as i18nSprite from '../uiutils/i18nSprite'
  4. import {Mgr} from '../GameControl'
  5. import {ccUtils} from '../utils/ccUtils'
  6. import {I18nConfig} from '../config/I18nConfig'
  7. import {Language, LANGUAGE_TYPE, LOCAL} from '../enums/Enum'
  8. import {EntryConfig} from '../config/EntryConfig'
  9. import {RechargeConfig} from '../config/RechargeConfig'
  10. export class I18nManager {
  11. private language: Language = Language.zh // 当前语言 默认中文
  12. private labelArr: i18nLabel.i18nLabel[] = [] // i18nLabel 列表
  13. private spriteArr: i18nSprite.i18nSprite[] = [] // i18nSprite 列表
  14. private i18nBundle: cc.AssetManager.Bundle
  15. public async init(language: number) {
  16. let curLanguage = Mgr.storage.getString(LOCAL.selectLanguage, Object.keys(Language)[language - 1])
  17. this.i18nBundle = await ccUtils.getBundleAsync('i18n')
  18. if (this.i18nBundle) this.setLanguage(curLanguage)
  19. return this.i18nBundle != null
  20. }
  21. /**
  22. * 设置语言
  23. */
  24. public setLanguage(language: Language) {
  25. if (this.language == language) {
  26. return
  27. }
  28. this.language = language
  29. this.reloadLabel()
  30. this.reloadSprite()
  31. }
  32. public getLanguage(): Language {
  33. return this.language
  34. }
  35. /**
  36. * 添加或移除 i18nLabel
  37. */
  38. public addOrDelLabel(label: i18nLabel.i18nLabel, isAdd: boolean) {
  39. if (isAdd) {
  40. this.labelArr.push(label)
  41. } else {
  42. let index = this.labelArr.indexOf(label)
  43. if (index !== -1) {
  44. this.labelArr.splice(index, 1)
  45. }
  46. }
  47. }
  48. public getLabel(opt: string, params?: string[] | number[]): string {
  49. if (CC_EDITOR) {
  50. this.language = Language.en
  51. if (!I18nConfig[opt]) return ''
  52. }
  53. if (!params) {
  54. return (I18nConfig[opt] ? I18nConfig[opt][this.language] : opt) || opt
  55. }
  56. let str = (I18nConfig[opt] ? I18nConfig[opt][this.language] : opt) || opt
  57. for (let i = 0; i < params.length; i++) {
  58. let reg = new RegExp('#(?!color=)' + i, 'g')
  59. str = str.replace(reg, params[i].toString())
  60. }
  61. return str
  62. }
  63. /**
  64. * 添加或移除 i18nSprite
  65. */
  66. public addOrDelSprite(sprite: i18nSprite.i18nSprite, isAdd: boolean) {
  67. if (isAdd) {
  68. this.spriteArr.push(sprite)
  69. } else {
  70. let index = this.spriteArr.indexOf(sprite)
  71. if (index !== -1) {
  72. this.spriteArr.splice(index, 1)
  73. }
  74. }
  75. }
  76. public getSprite(path: string, cb: (spriteFrame: cc.SpriteFrame) => void) {
  77. if (!this.i18nBundle) return
  78. this.i18nBundle.load(
  79. 'sprite/' + this.language + '/' + path,
  80. cc.SpriteFrame,
  81. (err, spriteFrame: cc.SpriteFrame) => {
  82. if (err) {
  83. return cb(null)
  84. }
  85. cb(spriteFrame)
  86. },
  87. )
  88. }
  89. public getEntryLabel(entry: number): string {
  90. let cfg = EntryConfig[entry]
  91. if (!cfg) return ''
  92. return this.getLabel(
  93. cfg.describe,
  94. cfg.desParm.map(v => v.toString()),
  95. )
  96. }
  97. public getTimeLabel(
  98. timeStamp: number,
  99. maxType: LANGUAGE_TYPE = LANGUAGE_TYPE.d,
  100. minType: LANGUAGE_TYPE = LANGUAGE_TYPE.m,
  101. ): string {
  102. let dhms = Date.date2DHMS(timeStamp)
  103. let dhmsArr = [dhms.day, dhms.hour, dhms.minute, dhms.second]
  104. let typeArr = [LANGUAGE_TYPE.d, LANGUAGE_TYPE.h, LANGUAGE_TYPE.m, LANGUAGE_TYPE.s]
  105. let str = ''
  106. for (let i = typeArr.indexOf(maxType); i <= typeArr.indexOf(minType); i++) {
  107. if (!str && !dhmsArr[i]) continue
  108. str += `${dhmsArr[i]}${this.getLabel(typeArr[i])}`
  109. }
  110. return str
  111. }
  112. //根据内置语言库更新金额显示模式
  113. public getMoneyLabel(id: number): string {
  114. let str = ''
  115. switch (Mgr.storage.getString(LOCAL.selectLanguage)) {
  116. case Language.en:
  117. str = `$${RechargeConfig[id].my / 100}`
  118. break
  119. case Language.zh:
  120. str = `${RechargeConfig[id].rmb / 100}元`
  121. break
  122. default:
  123. break
  124. }
  125. return str
  126. }
  127. private reloadLabel() {
  128. for (let one of this.labelArr) {
  129. one._resetValue()
  130. }
  131. }
  132. private reloadSprite() {
  133. for (let one of this.spriteArr) {
  134. one._resetValue()
  135. }
  136. }
  137. }