LocalizedLabel.ts 730 B

12345678910111213141516171819202122232425262728293031323334
  1. import * as i18n from './LanguageData';
  2. import { _decorator, Component, Label } from 'cc';
  3. const { ccclass, property, executeInEditMode } = _decorator;
  4. @ccclass('LocalizedLabel')
  5. @executeInEditMode
  6. export class LocalizedLabel extends Component {
  7. label: Label | null = null;
  8. @property
  9. key: string = '';
  10. onLoad() {
  11. if (!i18n.ready) {
  12. i18n.init('zh');
  13. }
  14. this.fetchRender();
  15. }
  16. fetchRender () {
  17. let label = this.getComponent('cc.Label') as Label;
  18. if (label) {
  19. this.label = label;
  20. this.updateLabel();
  21. return;
  22. }
  23. }
  24. updateLabel () {
  25. this.label && (this.label.string = i18n.t(this.key));
  26. }
  27. }