LocaleProvider国际化

为组件内建文案提供统一的国际化支持。

使用

全局配置

ng-zorro-antd-mobile 提供了一个配置型 token LOCAL_PROVIDER_TOKEN 用于全局配置国际化文案,可以在引入模块时初始化语言。

  1. /** 配置 ng-zorro-antd-mobile 国际化 **/
  2. import { LOCAL_PROVIDER_TOKEN, en_US } from 'ng-zorro-antd-mobile-mobile';
  3. @NgModule({
  4. ...
  5. imports : [ NgZorroAntdMobileModule ],
  6. providers : [ { provide: LOCAL_PROVIDER_TOKEN, useValue: en_US } ]
  7. })
  8. export class AppModule { }

运行时修改

ng-zorro-antd-mobile 提供了一个服务 LocaleProviderService 用于动态修改国际化文案。

  1. import { en_US, LocaleProviderService } from 'ng-zorro-antd-mobile';
  2. ...
  3. constructor(private localeProviderService:LocaleProviderService) {
  4. }
  5. switchLanguage() {
  6. this.localeProviderService.setLocale(en_US);
  7. }

注意:en_US 是语言包名称,以下表格也遵循同样的规则。

代码演示

基本用法

最简单的用法。

  1. import { Component } from '@angular/core';
  2. import { LocaleProviderService } from 'ng-zorro-antd-mobile';
  3. import { en_US, ru_RU, zh_CN, sv_SE, da_DK } from 'ng-zorro-antd-mobile';
  4. @Component({
  5. selector: 'demo-locale-provider-basic',
  6. styles: [],
  7. template: `
  8. <WingBlank>
  9. <ListItem
  10. Picker
  11. [arrow]="'horizontal'"
  12. [cols]="1"
  13. [extra]="locale"
  14. [(ngModel)]="lang"
  15. [data]="languages"
  16. (ngModelChange)="onChange($event)"
  17. >
  18. Choose language
  19. </ListItem>
  20. <WhiteSpace [size]="'xl'"></WhiteSpace>
  21. <WhiteSpace [size]="'xl'"></WhiteSpace>
  22. <div>
  23. <Pagination [total]="5" [current]="1"></Pagination>
  24. <WhiteSpace></WhiteSpace>
  25. <List class="date-picker-list" style="background-color: white">
  26. <ListItem DatePicker [arrow]="'horizontal'" [mode]="'date'" [title]="'Select date'">datePicker </ListItem>
  27. <ListItem Picker [arrow]="'horizontal'" [data]="seasons" [cascade]="false">
  28. picker
  29. </ListItem>
  30. </List>
  31. <WhiteSpace></WhiteSpace>
  32. <SearchBar [placeholder]="'Search'" [showCancelButton]="true"></SearchBar>
  33. <WhiteSpace></WhiteSpace>
  34. <InputItem [type]="'money'" [placeholder]="'money input'"></InputItem>
  35. </div>
  36. </WingBlank>
  37. `,
  38. providers: []
  39. })
  40. export class DemoLocaleProviderBasicComponent {
  41. lang = [
  42. {
  43. value: '中文',
  44. label: '中文',
  45. language: zh_CN
  46. }
  47. ];
  48. locale = '中文';
  49. data = [
  50. {
  51. value: '1',
  52. label: 'Food'
  53. },
  54. {
  55. value: '2',
  56. label: 'Supermarket'
  57. },
  58. {
  59. value: '3',
  60. label: 'Extra',
  61. isLeaf: true
  62. }
  63. ];
  64. seasons = [
  65. {
  66. label: '2013',
  67. children: [
  68. {
  69. label: '春',
  70. children: []
  71. },
  72. {
  73. label: '夏',
  74. children: []
  75. }
  76. ]
  77. },
  78. {
  79. label: '2014',
  80. children: [
  81. {
  82. label: '春'
  83. },
  84. {
  85. label: '夏'
  86. }
  87. ]
  88. }
  89. ];
  90. languages = [
  91. {
  92. value: '中文',
  93. label: '中文',
  94. language: zh_CN
  95. },
  96. {
  97. value: 'English',
  98. label: 'English',
  99. language: en_US
  100. },
  101. {
  102. value: 'Русский',
  103. label: 'Русский',
  104. language: ru_RU
  105. },
  106. {
  107. value: 'Swedish',
  108. label: 'Swedish',
  109. language: sv_SE
  110. },
  111. {
  112. value: 'Danish',
  113. label: 'Danish',
  114. language: da_DK
  115. }
  116. ];
  117. constructor(private _localeProviderService: LocaleProviderService) {}
  118. onChange = item => {
  119. this.locale = item[0].value;
  120. const currentLocale = this.languages.find(i => i.value === this.locale).language;
  121. this._localeProviderService.setLocale(currentLocale);
  122. }
  123. }

LocaleProvider国际化 - 图1