i18n 国际化

JSON Schema 本身只是一个 JSON 对象,因此本质上已经是支持国际化。此外,sf 还支持一些比较快捷的国际化方式,但它支持的元素比较基础:titledescriptionoptionalHelp 三个元素。

代码演示

i18n 国际化 - 图1

基础样例

name 元素采用内置的国际化方式;password 采用外部国际化方式。

  1. import { Component, Inject, ViewChild } from '@angular/core';
  2. import { SFSchema, SFComponent } from '@delon/form';
  3. import { NzMessageService } from 'ng-zorro-antd/message';
  4. import { ALAIN_I18N_TOKEN } from '@delon/theme';
  5. import { I18NService } from '@core';
  6. @Component({
  7. selector: 'form-i18n-simple',
  8. template: `
  9. <button nz-button type="button" (click)="changeLang('srv')">Change Language Via Service</button>
  10. <button nz-button type="button" (click)="changeLang('ref')">Change Language Via call refresh schema</button>
  11. <sf #sf [schema]="schema" (formSubmit)="submit($event)"></sf>
  12. `,
  13. })
  14. export class FormI18nSimpleComponent {
  15. @ViewChild('sf', { static: true }) comp: SFComponent;
  16. schema = this.i18nSchema;
  17. private get i18nSchema(): SFSchema {
  18. return {
  19. properties: {
  20. name: {
  21. type: 'string',
  22. ui: {
  23. i18n: 'sf.name',
  24. descriptionI18n: 'sf.description',
  25. optionalHelp: {
  26. i18n: 'sf.description',
  27. },
  28. },
  29. },
  30. password: {
  31. type: 'string',
  32. title: this.i18n.fanyi('sf.name'),
  33. description: this.i18n.fanyi('sf.description'),
  34. ui: {
  35. type: 'password',
  36. },
  37. },
  38. },
  39. required: ['name', 'password'],
  40. };
  41. }
  42. constructor(public msg: NzMessageService, @Inject(ALAIN_I18N_TOKEN) private i18n: I18NService) {}
  43. changeLang(type: 'srv' | 'ref') {
  44. this.i18n.use(this.i18n.zone === 'zh' ? 'en-US' : 'zh-CN');
  45. if (type === 'ref') {
  46. this.comp.refreshSchema(this.i18nSchema);
  47. }
  48. }
  49. submit(value: any) {
  50. this.msg.success(JSON.stringify(value));
  51. }
  52. }