| | |
| | | @Injectable() |
| | | export class I18NService implements AlainI18NService { |
| | | |
| | | private _default = 'en'; |
| | | private _default = 'zh-CN'; |
| | | |
| | | private _langs = [ |
| | | { code: 'zh-CN', text: '中文' }, |
| | | { code: 'en', text: 'English' } |
| | | ]; |
| | | |
| | | constructor(settings: SettingsService, |
| | | constructor( |
| | | settings: SettingsService, |
| | | private nzLocalService: NzLocaleService, |
| | | private translate: TranslateService, |
| | | private injector: Injector) { |
| | | private injector: Injector |
| | | ) { |
| | | this._default = settings.layout.lang || translate.getBrowserLang(); |
| | | const lans = this._langs.map(item => item.code); |
| | | if (!lans.includes(this._default)) { |
| | | this._default = lans[0]; |
| | | } |
| | | translate.addLangs(lans); |
| | | translate.setDefaultLang(this._default); |
| | | } |
| | | |
| | | use(lang: string = null, firstLoad = true): Observable<any> { |
| | |
| | | if (!firstLoad) this.injector.get(Router).navigate([ '/' ]); |
| | | return this.translate.use(lang); |
| | | } |
| | | |
| | | /** 获取语言列表 */ |
| | | getLangs() { |
| | | return this._langs; |
| | | } |
| | | |
| | | /** 翻译 */ |
| | | fanyi(key: string) { |
| | | return this.translate.instant(key); |
| | | } |
| | | |
| | | /** 默认语言 */ |
| | | get defaultLang() { |
| | | return this._default; |
| | | } |
| | | /** 当前语言 */ |
| | | get currentLang() { |
| | | return this.translate.currentLang || this.translate.getDefaultLang() || this._default; |
| | | } |
| | | } |