diff --git a/packages/core/lib/translate.service.ts b/packages/core/lib/translate.service.ts index 656689d..8c0ff71 100644 --- a/packages/core/lib/translate.service.ts +++ b/packages/core/lib/translate.service.ts @@ -161,7 +161,11 @@ export class TranslateService { @Inject(DEFAULT_LANGUAGE) defaultLanguage: string) { /** set the default language from configuration */ if (defaultLanguage) { - this.setDefaultLang(defaultLanguage); + this._setDefaultLang(defaultLanguage, this.extend); + } + + if (this.extend) { + this._use(this.currentLang, true); } } @@ -169,11 +173,15 @@ export class TranslateService { * Sets the default language to use as a fallback */ public setDefaultLang(lang: string): void { - if (lang === this.defaultLang) { + this._setDefaultLang(lang); + } + + protected _setDefaultLang(lang: string, forceLoad: boolean = false): void { + if (lang === this.defaultLang && !forceLoad) { return; } - let pending = this.retrieveTranslations(lang); + let pending = this.retrieveTranslations(lang, forceLoad); if (typeof pending !== "undefined") { // on init set the defaultLang immediately @@ -201,12 +209,16 @@ export class TranslateService { * Changes the lang currently used */ public use(lang: string): Observable { + return this._use(lang); + } + + protected _use(lang: string, forceLoad: boolean = false): Observable { // don't change the language if the language given is already selected - if (lang === this.currentLang) { + if (lang === this.currentLang && !forceLoad) { return of(this.translations[lang]); } - let pending = this.retrieveTranslations(lang); + let pending = this.retrieveTranslations(lang, forceLoad); if (typeof pending !== "undefined") { // on init set the currentLang immediately @@ -230,7 +242,11 @@ export class TranslateService { /** * Retrieves the given translations */ - private retrieveTranslations(lang: string): Observable | undefined { + private retrieveTranslations(lang: string, forceLoad: boolean = false): Observable | undefined { + if (forceLoad) { + return this.getTranslation(lang); + } + let pending: Observable | undefined; // if this language is unavailable or extend is true, ask for it