i18n.service.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // 请参考:https://ng-alain.com/docs/i18n
  2. import {Platform} from '@angular/cdk/platform';
  3. import {registerLocaleData} from '@angular/common';
  4. import ngEn from '@angular/common/locales/en';
  5. import ngZh from '@angular/common/locales/zh';
  6. import ngZhTw from '@angular/common/locales/zh-Hant';
  7. import {Injectable} from '@angular/core';
  8. import {
  9. _HttpClient,
  10. AlainI18nBaseService,
  11. DelonLocaleService,
  12. en_US as delonEnUS,
  13. SettingsService,
  14. zh_CN as delonZhCn,
  15. zh_TW as delonZhTw
  16. } from '@delon/theme';
  17. import {AlainConfigService} from '@delon/util/config';
  18. import {enUS as dfEn, zhCN as dfZhCn, zhTW as dfZhTw} from 'date-fns/locale';
  19. import {NzSafeAny} from 'ng-zorro-antd/core/types';
  20. import {en_US as zorroEnUS, NzI18nService, zh_CN as zorroZhCN, zh_TW as zorroZhTW} from 'ng-zorro-antd/i18n';
  21. import {Observable, zip} from 'rxjs';
  22. import {map} from "rxjs/operators";
  23. import {Message} from "../../pojo/Message";
  24. interface LangConfigData {
  25. abbr: string;
  26. text: string;
  27. ng: NzSafeAny;
  28. zorro: NzSafeAny;
  29. date: NzSafeAny;
  30. delon: NzSafeAny;
  31. }
  32. const DEFAULT = 'zh-CN';
  33. const LANGS: { [key: string]: LangConfigData } = {
  34. 'zh-CN': {
  35. text: '简体中文',
  36. ng: ngZh,
  37. zorro: zorroZhCN,
  38. date: dfZhCn,
  39. delon: delonZhCn,
  40. abbr: '🇨🇳'
  41. },
  42. 'zh-TW': {
  43. text: '繁体中文',
  44. ng: ngZhTw,
  45. zorro: zorroZhTW,
  46. date: dfZhTw,
  47. delon: delonZhTw,
  48. abbr: '🇭🇰'
  49. },
  50. 'en-US': {
  51. text: 'English',
  52. ng: ngEn,
  53. zorro: zorroEnUS,
  54. date: dfEn,
  55. delon: delonEnUS,
  56. abbr: '🇬🇧'
  57. }
  58. };
  59. @Injectable({ providedIn: 'root' })
  60. export class I18NService extends AlainI18nBaseService {
  61. protected _defaultLang = DEFAULT;
  62. private _langs = Object.keys(LANGS).map(code => {
  63. const item = LANGS[code];
  64. return { code, text: item.text, abbr: item.abbr };
  65. });
  66. constructor(
  67. private http: _HttpClient,
  68. private settings: SettingsService,
  69. private nzI18nService: NzI18nService,
  70. private delonLocaleService: DelonLocaleService,
  71. private platform: Platform,
  72. cogSrv: AlainConfigService
  73. ) {
  74. super(cogSrv);
  75. const defaultLang = this.getDefaultLang();
  76. this._defaultLang = this._langs.findIndex(w => w.code === defaultLang) === -1 ? DEFAULT : defaultLang;
  77. }
  78. private getDefaultLang(): string {
  79. if (!this.platform.isBrowser) {
  80. return DEFAULT;
  81. }
  82. if (this.settings.layout.lang) {
  83. return this.settings.layout.lang;
  84. }
  85. let res = (navigator.languages ? navigator.languages[0] : null) || navigator.language;
  86. const arr = res.split('-');
  87. return arr.length <= 1 ? res : `${arr[0]}-${arr[1].toUpperCase()}`;
  88. }
  89. loadLangData(lang: string): Observable<NzSafeAny> {
  90. return zip(this.http.get(`http://localhost:4200/assets/tmp/i18n/${lang}.json`),this.http.get(`/i18n/${lang}`))
  91. .pipe(
  92. map(([langLocalData, langRemoteData]: [Record<string, string>, Message<any>]) => {
  93. let remote:Record<string, string> = langRemoteData.data;
  94. Object.keys(remote).forEach(key => {
  95. langLocalData[key] = remote[key];
  96. });
  97. return langLocalData;
  98. })
  99. )
  100. }
  101. use(lang: string, data: Record<string, unknown>): void {
  102. if (this._currentLang === lang) return;
  103. this._data = this.flatData(data, []);
  104. const item = LANGS[lang];
  105. registerLocaleData(item.ng);
  106. this.nzI18nService.setLocale(item.zorro);
  107. this.nzI18nService.setDateLocale(item.date);
  108. this.delonLocaleService.setLocale(item.delon);
  109. this._currentLang = lang;
  110. this._change$.next(lang);
  111. }
  112. getLangs(): Array<{ code: string; text: string; abbr: string }> {
  113. return this._langs;
  114. }
  115. }