I18nLable.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Learn TypeScript:
  2. import I18n from './I18n';
  3. import executeInEditMode = cc._decorator.executeInEditMode;
  4. const {ccclass, property} = cc._decorator;
  5. @ccclass
  6. @executeInEditMode()
  7. export default class I18nLable extends cc.Component {
  8. private label: cc.Label | null = null;
  9. @property({ visible: false })
  10. key: string = '';
  11. // 声明property属性Key
  12. @property({ displayName: '翻译id', visible: true })
  13. get _key() {
  14. return this.key;
  15. }
  16. set _key(str: string) {
  17. this.key = str;
  18. this.updateLabel();
  19. }
  20. onLoad()
  21. {
  22. this.fetchRender();
  23. }
  24. onEnable(): void {
  25. this.updateLabel();
  26. }
  27. public fetchRender () {
  28. // 获取文本Label组件
  29. let label = this.getComponent('cc.Label') as cc.Label;
  30. if (label) {
  31. this.label = label;
  32. this.updateLabel();
  33. return;
  34. }
  35. }
  36. public updateLabel () {
  37. // 通过LanguageData的方法接口,根据语言类型和key获取指定的内容
  38. // 然后改变内容的显示
  39. this.label && (this.label.string = I18n.t(this.key));
  40. }
  41. }