123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /** @format */
- import {Mgr} from '../GameControl'
- import {Language} from '../enums/Enum'
- const {ccclass, property, executeInEditMode, disallowMultiple, requireComponent, menu} = cc._decorator
- // 定义英文大小写类型
- enum EnglishCase {
- 默认,
- 首字母大写,
- 全部大写,
- 所有首字母大写,
- }
- @ccclass
- @executeInEditMode
- @disallowMultiple
- @menu('多语言/i18nLabel')
- export class i18nLabel extends cc.Component {
- @property({visible: false})
- private _englishCase: EnglishCase = 0 // 设置默认值
- @property({visible: false})
- private i18n_string: string = ''
- @property({visible: false})
- private i18n_params: string[] = []
- @property({type: cc.String})
- get string() {
- return this.i18n_string
- }
- set string(value: string) {
- this.i18n_string = value
- this.setEndValue()
- }
- @property({type: [cc.String]})
- get params() {
- return this.i18n_params
- }
- set params(value: string[]) {
- this.i18n_params = value
- this.setEndValue()
- }
- @property({
- displayName: '英文格式',
- type: cc.Enum(EnglishCase), // 设置属性类型为枚举类型
- })
- get englishCase(): EnglishCase {
- return this._englishCase
- }
- set englishCase(value: EnglishCase) {
- this._englishCase = value
- this.setEndValue()
- }
- @property({
- displayName: '结尾增加冒号',
- type: cc.Boolean,
- })
- get addColon(): boolean {
- return this._addColon
- }
- set addColon(value: boolean) {
- this._addColon = value
- this.setEndValue()
- }
- @property({visible: false})
- private _addColon: boolean = false // 设置默认值
- start() {
- Mgr.i18n.addOrDelLabel(this, true)
- this._resetValue()
- }
- init(string: string, params: string[]) {
- this.i18n_string = string
- this.i18n_params = params
- this.setEndValue()
- }
- setParamByIndex(str: string, index: number) {
- let i18nStr = Mgr.i18n.getLabel(str, [])
- this.i18n_params[index] = i18nStr ? i18nStr : str
- this.setEndValue()
- }
- private setEndValue() {
- let label: any = this.getComponent(cc.Label)
- if (!label) label = this.getComponent(cc.RichText)
- if (cc.isValid(label)) {
- let i18nStr = Mgr.i18n.getLabel(this.i18n_string, this.i18n_params)
- if (!i18nStr && CC_EDITOR) return
- label.string = i18nStr
- if (Mgr.i18n.getLanguage() == Language.en) {
- switch (this._englishCase) {
- case 1:
- label.string = this._capitalizeRichText(label.string, true)
- break
- case 2:
- label.string = label.string.toUpperCase()
- break
- case 3:
- label.string = this._capitalizeRichText(label.string)
- break
- }
- }
- if (this.addColon) label.string += ':'
- }
- }
- _resetValue() {
- this.string = this.i18n_string
- }
- _capitalizeRichText(text: string, onlyStart?: boolean): string {
- // 匹配富文本中的单词
- let wordRegex = /\b\w/g
- if (onlyStart) wordRegex = /\b\w/
- // 匹配 HTML 标签
- const tagRegex = /<[^>]+>/g
- // 替换匹配到的单词,并将其首字母大写
- const replacedText = text.replace(wordRegex, letter => letter.toUpperCase())
- // 过滤 HTML 标签内的单词
- const filteredText = replacedText.replace(tagRegex, match => match.toLowerCase())
- return filteredText
- }
- onDestroy() {
- Mgr.i18n.addOrDelLabel(this, false)
- }
- }
|