I18n.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { error, sys } from "cc";
  2. import { EventName } from "../EventName";
  3. import Events from "../Events";
  4. import En from "./config/En";
  5. import Zh from "./config/Zh";
  6. /**
  7. * 语言类型
  8. */
  9. export enum LangType {
  10. NONE = "",
  11. ZH = "zh",
  12. EN = "en",
  13. PT = "pt",
  14. JA = "ja",
  15. KO = "ko",
  16. DE = "de",
  17. FR = "fr"
  18. }
  19. /**
  20. * 国家类型
  21. */
  22. export enum CountryType {
  23. NONE = "",
  24. ZH = "ZH",
  25. JP = "JP",
  26. BR = "BR",
  27. KR = "KR",
  28. DE = "DE",
  29. FR = "FR",
  30. GB = "GB",
  31. US = "US",
  32. CA = "CA",
  33. AU = "AU"
  34. }
  35. /**
  36. * 多语言控制类
  37. */
  38. export default class I18n {
  39. private static _init: boolean = false;
  40. /** 语言表 */
  41. private static _phrases: any = null;
  42. private static _curLang: LangType = LangType.NONE;
  43. private static _curCountry: CountryType = CountryType.NONE;
  44. /** 当前语言类型 */
  45. public static get curLang(): LangType { return this._curLang; };
  46. /** 当前国家类型 */
  47. public static get curCountry(): CountryType { return this._curCountry };
  48. /**
  49. * 初始化语言
  50. * @param language
  51. */
  52. public static init(language: LangType = LangType.NONE): void {
  53. if (this._init) {
  54. return;
  55. }
  56. this._init = true;
  57. let lang = language || sys.language;
  58. this.switch(lang as LangType);
  59. }
  60. /**
  61. * 切换语言
  62. * @param language
  63. */
  64. public static switch(language: LangType): void {
  65. if (this._curLang === language) {
  66. return;
  67. }
  68. this._curLang = language;
  69. switch (language) {
  70. case LangType.ZH:
  71. this._phrases = Zh;
  72. break;
  73. case LangType.EN:
  74. this._phrases = En;
  75. break;
  76. default:
  77. this._curLang = LangType.EN;
  78. this._phrases = En;
  79. break;
  80. }
  81. this.updateLocalizedCmpt();
  82. }
  83. /**
  84. * 更新所有多语言组件
  85. */
  86. public static updateLocalizedCmpt(): void {
  87. Events.emit(EventName.UPDATE_LOCALIZED_CMPT);
  88. }
  89. /**
  90. * 通过语言表value获取对应的key
  91. * @param value 语言表的value
  92. */
  93. public static getKeyByValue(value: string): string {
  94. if (!this._phrases) {
  95. error(`[I18n.getKeyByValue] 未正确初始化`);
  96. return "";
  97. }
  98. for (let key in this._phrases) {
  99. if (this._phrases[key] === value) {
  100. return key;
  101. }
  102. }
  103. return "";
  104. }
  105. /**
  106. * 通过key获取语言表中的字符串
  107. * @param key 语言表中的key
  108. * @param option 用于替换的数据,可以传键值对,也可以按顺序传参
  109. * @example
  110. * // 语言表 {"test": "test %{arg1} %{arg2} !!!"}
  111. * I18n.getText("test", {arg1: "somthing", arg2: 2}); => "test somthing 2 !!!"
  112. * I18n.getText("test", "somthing", 2); => "test somthing 2 !!!"
  113. */
  114. public static getText(key: string, ...option: [{ [k: string]: string | number }] | Array<string | number>): string {
  115. if (!this._phrases) {
  116. error(`[I18n.getText] 未正确初始化`);
  117. return "";
  118. }
  119. if (!key) {
  120. return "";
  121. }
  122. let text: string = this._phrases.hasOwnProperty(key) ? this._phrases[key] : key;
  123. if (option.length === 1 && Object.prototype.toString.call(option[0]) === "[object Object]") {
  124. // 参数为键值对
  125. for (let arg in (option[0] as { [k: string]: string | number })) {
  126. if (option[0].hasOwnProperty(arg)) {
  127. let reg = new RegExp(`%{${arg}}`, "g");
  128. text = text.replace(reg, `${option[0][arg]}`);
  129. }
  130. }
  131. } else {
  132. // 参数为数组
  133. option.forEach((value: any) => {
  134. text = text.replace(/%\{.*?\}/, `${value}`);
  135. });
  136. }
  137. return text;
  138. }
  139. }