@senspark/ee
Version:
utility library for cocos creator
156 lines (129 loc) • 4.32 kB
text/typescript
import { LanguageManager } from "./LanguageManager";
const { ccclass, disallowMultiple, executeInEditMode, inspector, menu, property } = cc._decorator;
export class LanguageComponent extends cc.Component {
private static counter: number = 0;
/** Gets or sets the multilingual key. */
public _key: string = '{null}';
public get key(): string {
return this._key;
}
public set key(value: string) {
this._key = value;
this.updateText();
}
/** Gets the multilingual format corresponding to the current key. */
public get format(): string {
return this.getLanguageManager().getFormat(this.key) || '';
}
private _paramValues: string[] = [];
/** Gets the multilingual parameter keys. */
public get paramKeys(): string[] {
return this.parseParamKeys(this.format);
}
/** Gets or sets the multilingual parameter values. */
public get paramValues(): string[] {
while (this._paramValues.length < this.paramKeys.length) {
this._paramValues.push('');
}
return this._paramValues;
}
public set paramValues(value: string[]) {
this._paramValues = value;
this.updateText();
}
/** Gets the translated string. */
public get string(): string | undefined {
const options: any = {};
for (let i = 0; i < this.paramKeys.length; ++i) {
options[this.paramKeys[i]] = this.paramValues[i];
}
return this.getLanguageManager().parseFormat(this.key, options);
}
private get config(): string | undefined {
return this.getLanguageManager().getConfigDir();
}
private set config(value: string | undefined) {
if (value !== undefined && value.length > 0 /* May be empty */) {
this.getLanguageManager().setConfigDir(value);
} else {
this.getLanguageManager().resetConfigDir();
}
}
private get languages(): string[] {
return this.getLanguageManager().getLanguages();
}
private get language(): string | undefined {
return this.getLanguageManager().getCurrentLanguage();
}
private set language(value: string | undefined) {
this.getLanguageManager().setCurrentLanguage(value);
}
/** Unique ID for each language component. */
private componentId: string;
/** Associated label component. */
private label: cc.Label | null = null;
private manager?: LanguageManager;
public constructor() {
super();
this.componentId = (LanguageComponent.counter++).toString();
}
private getLanguageManager(): LanguageManager {
return this.manager || (this.manager = LanguageManager.getInstance());
}
public onEnable(): void {
this.getLanguageManager().addObserver(this.componentId, () => {
this.updateText();
});
this.updateText();
}
public onDisable(): void {
this.getLanguageManager().removeObserver(this.componentId);
}
public update(): void {
if (CC_EDITOR) {
// Repeatedly update string when in editor mode.
this.updateText();
}
}
private updateText(): void {
if (this.label === null) {
this.label = this.getComponent(cc.Label);
if (this.label === null) {
// Component not found.
return;
}
}
this.label.string = this.string || '';
}
private parseParamKeys(format: string): string[] {
const regex = /%{(.*?)}/g;
let match = regex.exec(format);
const params: string[] = [];
while (match !== null) {
params.push(match[1]);
match = regex.exec(format);
}
return params;
}
}