多语言模块
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
import { _decorator, Component, Label, Node } from "cc";
|
||||
import LanguageUtils, { LanguageType } from "./LanguageUtils";
|
||||
|
||||
const { ccclass, property, requireComponent } = _decorator;
|
||||
|
||||
@ccclass("LanguageLabel")
|
||||
@requireComponent(Label)
|
||||
export class LanguageLabel extends Component {
|
||||
@property({
|
||||
displayName: "Language Key",
|
||||
tooltip: "多语言配置表中的key值",
|
||||
})
|
||||
private languageKey: string = "";
|
||||
|
||||
@property({
|
||||
displayName: "Default Text",
|
||||
tooltip: "如果找不到对应的翻译时显示的默认文本",
|
||||
})
|
||||
private defaultText: string = "";
|
||||
|
||||
@property({
|
||||
displayName: "Auto Update",
|
||||
tooltip: "是否自动监听语言变化并更新",
|
||||
})
|
||||
private autoUpdate: boolean = true;
|
||||
|
||||
private label: Label = null;
|
||||
private params: { [key: string]: any } = null;
|
||||
|
||||
onLoad() {
|
||||
this.label = this.getComponent(Label);
|
||||
if (!this.label) {
|
||||
console.error("LanguageLabel: Label component not found!");
|
||||
return;
|
||||
}
|
||||
|
||||
this.updateText();
|
||||
|
||||
if (this.autoUpdate) {
|
||||
LanguageUtils.onLanguageChanged(this.onLanguageChanged, this);
|
||||
}
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
if (this.autoUpdate) {
|
||||
LanguageUtils.offLanguageChanged(this.onLanguageChanged, this);
|
||||
}
|
||||
}
|
||||
|
||||
onEnable() {
|
||||
this.updateText();
|
||||
}
|
||||
|
||||
private onLanguageChanged(language: LanguageType) {
|
||||
this.updateText();
|
||||
}
|
||||
|
||||
private updateText() {
|
||||
if (!this.label || !this.languageKey) {
|
||||
return;
|
||||
}
|
||||
|
||||
let text: string;
|
||||
if (this.params && Object.keys(this.params).length > 0) {
|
||||
text = LanguageUtils.getTextWithParams(
|
||||
this.languageKey,
|
||||
this.params,
|
||||
this.defaultText
|
||||
);
|
||||
} else {
|
||||
text = LanguageUtils.getText(this.languageKey, this.defaultText);
|
||||
}
|
||||
|
||||
this.label.string = text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置语言key
|
||||
*/
|
||||
public setLanguageKey(key: string) {
|
||||
this.languageKey = key;
|
||||
this.updateText();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置文本参数(用于替换文本中的占位符)
|
||||
* @param params 参数对象,如 {name: "玩家", score: 100}
|
||||
*/
|
||||
public setParams(params: { [key: string]: any }) {
|
||||
this.params = params;
|
||||
this.updateText();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加或更新单个参数
|
||||
*/
|
||||
public setParam(key: string, value: any) {
|
||||
if (!this.params) {
|
||||
this.params = {};
|
||||
}
|
||||
this.params[key] = value;
|
||||
this.updateText();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除所有参数
|
||||
*/
|
||||
public clearParams() {
|
||||
this.params = null;
|
||||
this.updateText();
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动刷新文本
|
||||
*/
|
||||
public refresh() {
|
||||
this.updateText();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前的语言key
|
||||
*/
|
||||
public getLanguageKey(): string {
|
||||
return this.languageKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置默认文本
|
||||
*/
|
||||
public setDefaultText(text: string) {
|
||||
this.defaultText = text;
|
||||
this.updateText();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user