Localization is common in Cocos Creator. Plain strings are easy, but rich text and parameters require additional handling. Parameters are especially important because word order differs between languages, so fixed %s positions are not always sufficient.
Rich text
You can either embed Cocos rich-text markup directly in translations, or use custom semantic tags and convert them with regular expressions. The second approach keeps the translation format easier to control.
Suppose “Terms” and “Privacy Policy” must be blue and clickable:
1 | "Terms": { |
Assign the converted string to RichText:
1 | this.richText.string = this.parseToRichText(Lang.getText('Terms')); |
1 | parseToRichText(text: string): string { |
Positional parameters
If parameter order is identical across languages, %s is adequate:
1 | "Terms": { |
1 | this.label.string = Lang.getText('Terms', 'Terms', 'Privacy Policy'); |
Named parameters
When word order differs, use named placeholders:
1 | "Terms": { |
1 | this.label.string = Lang.getText('Terms', { |
This helper supports both formats:
1 | public static getText(key: string, ...args: any[]): string { |
Treat this as a starting point and adapt the tags, escaping rules, and parameter validation to your project.