| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- // 请参考:https://ng-alain.com/docs/i18n
- import {Platform} from '@angular/cdk/platform';
- import {registerLocaleData} from '@angular/common';
- import ngEn from '@angular/common/locales/en';
- import ngZh from '@angular/common/locales/zh';
- import ngZhTw from '@angular/common/locales/zh-Hant';
- import {Injectable} from '@angular/core';
- import {
- _HttpClient,
- AlainI18nBaseService,
- DelonLocaleService,
- en_US as delonEnUS,
- SettingsService,
- zh_CN as delonZhCn,
- zh_TW as delonZhTw
- } from '@delon/theme';
- import {AlainConfigService} from '@delon/util/config';
- import {enUS as dfEn, zhCN as dfZhCn, zhTW as dfZhTw} from 'date-fns/locale';
- import {NzSafeAny} from 'ng-zorro-antd/core/types';
- import {en_US as zorroEnUS, NzI18nService, zh_CN as zorroZhCN, zh_TW as zorroZhTW} from 'ng-zorro-antd/i18n';
- import {Observable, zip} from 'rxjs';
- import {map} from "rxjs/operators";
- import {Message} from "../../pojo/Message";
- interface LangConfigData {
- abbr: string;
- text: string;
- ng: NzSafeAny;
- zorro: NzSafeAny;
- date: NzSafeAny;
- delon: NzSafeAny;
- }
- const DEFAULT = 'zh-CN';
- const LANGS: { [key: string]: LangConfigData } = {
- 'zh-CN': {
- text: '简体中文',
- ng: ngZh,
- zorro: zorroZhCN,
- date: dfZhCn,
- delon: delonZhCn,
- abbr: '🇨🇳'
- },
- 'zh-TW': {
- text: '繁体中文',
- ng: ngZhTw,
- zorro: zorroZhTW,
- date: dfZhTw,
- delon: delonZhTw,
- abbr: '🇭🇰'
- },
- 'en-US': {
- text: 'English',
- ng: ngEn,
- zorro: zorroEnUS,
- date: dfEn,
- delon: delonEnUS,
- abbr: '🇬🇧'
- }
- };
- @Injectable({ providedIn: 'root' })
- export class I18NService extends AlainI18nBaseService {
- protected _defaultLang = DEFAULT;
- private _langs = Object.keys(LANGS).map(code => {
- const item = LANGS[code];
- return { code, text: item.text, abbr: item.abbr };
- });
- constructor(
- private http: _HttpClient,
- private settings: SettingsService,
- private nzI18nService: NzI18nService,
- private delonLocaleService: DelonLocaleService,
- private platform: Platform,
- cogSrv: AlainConfigService
- ) {
- super(cogSrv);
- const defaultLang = this.getDefaultLang();
- this._defaultLang = this._langs.findIndex(w => w.code === defaultLang) === -1 ? DEFAULT : defaultLang;
- }
- private getDefaultLang(): string {
- if (!this.platform.isBrowser) {
- return DEFAULT;
- }
- if (this.settings.layout.lang) {
- return this.settings.layout.lang;
- }
- let res = (navigator.languages ? navigator.languages[0] : null) || navigator.language;
- const arr = res.split('-');
- return arr.length <= 1 ? res : `${arr[0]}-${arr[1].toUpperCase()}`;
- }
- loadLangData(lang: string): Observable<NzSafeAny> {
- return zip(this.http.get(`http://localhost:4200/assets/tmp/i18n/${lang}.json`),this.http.get(`/i18n/${lang}`))
- .pipe(
- map(([langLocalData, langRemoteData]: [Record<string, string>, Message<any>]) => {
- let remote:Record<string, string> = langRemoteData.data;
- Object.keys(remote).forEach(key => {
- langLocalData[key] = remote[key];
- });
- return langLocalData;
- })
- )
- }
- use(lang: string, data: Record<string, unknown>): void {
- if (this._currentLang === lang) return;
- this._data = this.flatData(data, []);
- const item = LANGS[lang];
- registerLocaleData(item.ng);
- this.nzI18nService.setLocale(item.zorro);
- this.nzI18nService.setDateLocale(item.date);
- this.delonLocaleService.setLocale(item.delon);
- this._currentLang = lang;
- this._change$.next(lang);
- }
- getLangs(): Array<{ code: string; text: string; abbr: string }> {
- return this._langs;
- }
- }
|