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

LocaleProvider 国际化 - 图1