import { Injectable } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { Observable, BehaviorSubject } from 'rxjs'; import { filter } from 'rxjs/operators'; @Injectable() export class NaTranslateService { private change$ = new BehaviorSubject(new Object()); constructor(private translate: TranslateService) { if (translate.getLangs() == null || translate.getLangs().length <= 0) { // translate.addLangs(['zh', 'en']); translate.addLangs([this.translate.getBrowserLang()]); } // if (this.translate.currentLang == null) { // this.translate.use(this.translate.getBrowserLang()); // } if (this.translate.getDefaultLang() == null) { // this.translate.setDefaultLang(this.translate.getBrowserCultureLang()); // zh_CN this.translate.setDefaultLang(this.translate.getBrowserLang()); // zh } } get change(): Observable { return this.change$.asObservable().pipe(filter(w => w != null)); } get currentLang() { return this.translate.currentLang; } /** * Gets the translated value of a key (or an array of keys) */ get(key: string | Array, interpolateParams?: Object): Observable { return this.translate.get(key, interpolateParams); } /** * Returns a stream of translated values of a key (or an array of keys) which updates * whenever the language changes. */ public stream(key: string | Array, interpolateParams?: Object): Observable { return this.translate.stream(key, interpolateParams); } /** * Returns a translation instantly from the internal state of loaded translation. * All rules regarding the current language, the preferred language of even fallback languages will be used except any promise handling. */ public instant(key: string | Array, interpolateParams?: Object): string | any { return this.translate.instant(key, interpolateParams); } /** * Changes the lang currently used */ use(lang: string): Observable { this.change$.next(lang); return this.translate.use(lang); } /** * Sets the default language to use as a fallback */ public setDefaultLang(lang: string): void { this.translate.setDefaultLang(lang); } /** * Gets the default language used * @returns string */ public getDefaultLang(): string { return this.translate.defaultLang; } /** * Returns an array of currently available langs * @returns any */ public getLangs(): Array { return this.translate.langs; } /** * @param langs * Add available langs */ public addLangs(langs: Array): void { this.translate.addLangs(langs); } /** * Sets the translated value of a key */ public set(key: string, value: string, lang: string = this.translate.currentLang): void { this.translate.set(key, value, lang); } /** * Allows to reload the lang file from the file */ public reloadLang(lang: string): Observable { return this.translate.reloadLang(lang); } /** * Deletes inner translation */ public resetLang(lang: string): void { this.translate.resetLang(lang); } /** * Returns the language code name from the browser, e.g. "de" * * @returns string */ public getBrowserLang(): string { return this.translate.getBrowserLang(); } /** * Returns the culture language code name from the browser, e.g. "de-DE" * * @returns string */ public getBrowserCultureLang(): string { return this.translate.getBrowserCultureLang(); } }