Skip to content
LanguageUtil

Utils / LanguageUtil

LanguageUtil Class ​

Multilingual tools

Table of contents ​

Methods ​

addKey(textkey: string): string other
Multilingual tagging
getDefaultLocale(): string other
Get the default language and region
getText(textkey: string): string other
Retrieve translation content based on key
getlanguage(): LanguageCodeType other
Get the current localized language
setLanguage(type: LanguageCodeType): boolean other
Game language settings

Methods ​

addKey ​

• Static addKey(textkey): string other

Multilingual tagging

Parameters ​

textkey stringUsage: Key range collected from user multilingual configuration table: No restrictions

Returns ​

stringReturns the Key. This function is only used to translate text and collect identifiers.

getDefaultLocale ​

• Static getDefaultLocale(): string other

Get the default language and region

Returns ​

stringOptional ISO 3166-1 country code (e.g. "CN")

Usage example: create a script named LocaleExample, place it in the object bar, open the script, modify the original content to the following, save and run the game, and the log will output the default language and region

ts
@Component
export default class LocaleExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        const locale = LocaleUtil.getDefaultLocale();
        console.log(`locale: ${locale}`);
// zh-CN
    }
}
@Component
export default class LocaleExample extends Script {

    protected onStart(): void {
        if (!SystemUtil.isClient()) return;
        this.test();
    }

    private async test(): Promise<void> {
        const locale = LocaleUtil.getDefaultLocale();
        console.log(`locale: ${locale}`);
// zh-CN
    }
}

getText ​

• Static getText(textkey): string other

Retrieve translation content based on key

Parameters ​

textkey stringUsage: The key range for the translation to be searched is not restricted

Returns ​

stringReturn the translation content of the current language environment corresponding to the Key

getlanguage ​

• Static getlanguage(): LanguageCodeType other

Get the current localized language

Returns ​

LanguageCodeTypeLanguage enumeration object

setLanguage ​

• Static setLanguage(type): boolean other

Game language settings

Parameters ​

type LanguageCodeTypeUsage: Switch game language types

Returns ​

booleanTrue modification successful false modification failed