i18nLabel.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /** @format */
  2. import {Mgr} from '../GameControl'
  3. import {Language} from '../enums/Enum'
  4. const {ccclass, property, executeInEditMode, disallowMultiple, requireComponent, menu} = cc._decorator
  5. // 定义英文大小写类型
  6. enum EnglishCase {
  7. 默认,
  8. 首字母大写,
  9. 全部大写,
  10. 所有首字母大写,
  11. }
  12. @ccclass
  13. @executeInEditMode
  14. @disallowMultiple
  15. @menu('多语言/i18nLabel')
  16. export class i18nLabel extends cc.Component {
  17. @property({visible: false})
  18. private _englishCase: EnglishCase = 0 // 设置默认值
  19. @property({visible: false})
  20. private i18n_string: string = ''
  21. @property({visible: false})
  22. private i18n_params: string[] = []
  23. @property({type: cc.String})
  24. get string() {
  25. return this.i18n_string
  26. }
  27. set string(value: string) {
  28. this.i18n_string = value
  29. this.setEndValue()
  30. }
  31. @property({type: [cc.String]})
  32. get params() {
  33. return this.i18n_params
  34. }
  35. set params(value: string[]) {
  36. this.i18n_params = value
  37. this.setEndValue()
  38. }
  39. @property({
  40. displayName: '英文格式',
  41. type: cc.Enum(EnglishCase), // 设置属性类型为枚举类型
  42. })
  43. get englishCase(): EnglishCase {
  44. return this._englishCase
  45. }
  46. set englishCase(value: EnglishCase) {
  47. this._englishCase = value
  48. this.setEndValue()
  49. }
  50. @property({
  51. displayName: '结尾增加冒号',
  52. type: cc.Boolean,
  53. })
  54. get addColon(): boolean {
  55. return this._addColon
  56. }
  57. set addColon(value: boolean) {
  58. this._addColon = value
  59. this.setEndValue()
  60. }
  61. @property({visible: false})
  62. private _addColon: boolean = false // 设置默认值
  63. start() {
  64. Mgr.i18n.addOrDelLabel(this, true)
  65. this._resetValue()
  66. }
  67. init(string: string, params: string[]) {
  68. this.i18n_string = string
  69. this.i18n_params = params
  70. this.setEndValue()
  71. }
  72. setParamByIndex(str: string, index: number) {
  73. let i18nStr = Mgr.i18n.getLabel(str, [])
  74. this.i18n_params[index] = i18nStr ? i18nStr : str
  75. this.setEndValue()
  76. }
  77. private setEndValue() {
  78. let label: any = this.getComponent(cc.Label)
  79. if (!label) label = this.getComponent(cc.RichText)
  80. if (cc.isValid(label)) {
  81. let i18nStr = Mgr.i18n.getLabel(this.i18n_string, this.i18n_params)
  82. if (!i18nStr && CC_EDITOR) return
  83. label.string = i18nStr
  84. if (Mgr.i18n.getLanguage() == Language.en) {
  85. switch (this._englishCase) {
  86. case 1:
  87. label.string = this._capitalizeRichText(label.string, true)
  88. break
  89. case 2:
  90. label.string = label.string.toUpperCase()
  91. break
  92. case 3:
  93. label.string = this._capitalizeRichText(label.string)
  94. break
  95. }
  96. }
  97. if (this.addColon) label.string += ':'
  98. }
  99. }
  100. _resetValue() {
  101. this.string = this.i18n_string
  102. }
  103. _capitalizeRichText(text: string, onlyStart?: boolean): string {
  104. // 匹配富文本中的单词
  105. let wordRegex = /\b\w/g
  106. if (onlyStart) wordRegex = /\b\w/
  107. // 匹配 HTML 标签
  108. const tagRegex = /<[^>]+>/g
  109. // 替换匹配到的单词,并将其首字母大写
  110. const replacedText = text.replace(wordRegex, letter => letter.toUpperCase())
  111. // 过滤 HTML 标签内的单词
  112. const filteredText = replacedText.replace(tagRegex, match => match.toLowerCase())
  113. return filteredText
  114. }
  115. onDestroy() {
  116. Mgr.i18n.addOrDelLabel(this, false)
  117. }
  118. }