Skip to content

Commit 610e4f1

Browse files
committed
Cleanup use() and setDefaultLang()
1 parent ae09020 commit 610e4f1

1 file changed

Lines changed: 37 additions & 53 deletions

File tree

projects/ngx-translate/src/lib/translate.service.ts

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -182,30 +182,23 @@ export class TranslateService {
182182
/**
183183
* Sets the default language to use as a fallback
184184
*/
185-
public setDefaultLang(lang: string): void {
186-
if (lang === this.defaultLang) {
187-
return;
185+
public setDefaultLang(lang: string): Observable<InterpolatableTranslationObject>
186+
{
187+
if (this.defaultLang == null)
188+
{
189+
// on init set the defaultLang immediately, but do not emit a change yet
190+
this.store.setDefaultLang(lang, false);
188191
}
189192

190-
const pending = this.retrieveTranslations(lang);
191-
192-
if (typeof pending !== "undefined") {
193-
194-
if (this.defaultLang == null) {
195-
// on init set the defaultLang immediately
196-
// but do not emit the change yet
197-
this.store.setDefaultLang(lang, false);
198-
}
199-
200-
pending.pipe(take(1))
201-
.subscribe(() => {
202-
this.store.setDefaultLang(lang);
203-
});
204-
}
205-
else {
206-
// we already have this language
207-
this.store.setDefaultLang(lang);
193+
const pending = this.loadOrExtendLanguage(lang);
194+
if (isObservable(pending))
195+
{
196+
pending.pipe(take(1)).subscribe(() => { this.store.setDefaultLang(lang); });
197+
return pending;
208198
}
199+
200+
this.store.setDefaultLang(lang);
201+
return of(this.store.getTranslations(lang));
209202
}
210203

211204
/**
@@ -225,32 +218,37 @@ export class TranslateService {
225218
// where translation loads might complete in random order
226219
this.lastUseLanguage = lang;
227220

228-
// don't change the language if the language given is already selected
229-
if (lang === this.currentLang) {
230-
return of(this.store.getTranslations(lang));
221+
if (!this.currentLang)
222+
{
223+
// on init set the currentLang immediately, but do not emit a change yet
224+
this.store.setCurrentLang(lang, false);
231225
}
232226

233-
// on init set the currentLang immediately
234-
if (!this.currentLang) {
235-
this.store.setCurrentLang(lang, false);
227+
const pending = this.loadOrExtendLanguage(lang);
228+
if (isObservable(pending))
229+
{
230+
pending.pipe(take(1)).subscribe(() => { this.changeLang(lang); });
231+
return pending;
236232
}
237233

238-
const pending = this.retrieveTranslations(lang);
234+
this.changeLang(lang);
235+
return of(this.store.getTranslations(lang));
236+
}
239237

240-
if (isObservable(pending)) {
241238

242-
pending.pipe(take(1))
243-
.subscribe(() => {
244-
this.changeLang(lang);
245-
});
246239

247-
return pending;
248-
}
249-
else {
250-
// we have this language, return an Observable
251-
this.changeLang(lang);
252-
return of(this.store.getTranslations(lang));
240+
/**
241+
* Retrieves the given translations
242+
*/
243+
private loadOrExtendLanguage(lang: string): Observable<TranslationObject> | undefined {
244+
245+
// if this language is unavailable or extend is true, ask for it
246+
if (!this.store.hasTranslationFor(lang) || this.extend) {
247+
this._translationRequests[lang] = this._translationRequests[lang] || this.loadAndCompileTranslations(lang);
248+
return this._translationRequests[lang];
253249
}
250+
251+
return undefined;
254252
}
255253

256254

@@ -275,20 +273,6 @@ export class TranslateService {
275273
}
276274

277275

278-
/**
279-
* Retrieves the given translations
280-
*/
281-
private retrieveTranslations(lang: string): Observable<TranslationObject> | undefined {
282-
283-
// if this language is unavailable or extend is true, ask for it
284-
if (!this.store.hasTranslationFor(lang) || this.extend) {
285-
this._translationRequests[lang] = this._translationRequests[lang] || this.loadAndCompileTranslations(lang);
286-
return this._translationRequests[lang];
287-
}
288-
289-
return undefined;
290-
}
291-
292276

293277
private loadAndCompileTranslations(lang: string): Observable<InterpolatableTranslationObject> {
294278

0 commit comments

Comments
 (0)