Skip to content

Commit 1c78cf3

Browse files
authored
fix(service): make initial translation streaming lazy (#960)
Fixes #815
1 parent 9ff74dc commit 1c78cf3

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {EventEmitter, Inject, Injectable, InjectionToken} from "@angular/core";
2-
import {concat, forkJoin, isObservable, Observable, of} from "rxjs";
2+
import {concat, forkJoin, isObservable, Observable, of, defer} from "rxjs";
33
import {concatMap, map, shareReplay, switchMap, take} from "rxjs/operators";
44
import {MissingTranslationHandler, MissingTranslationHandlerParams} from "./missing-translation-handler";
55
import {TranslateCompiler} from "./translate.compiler";
@@ -388,7 +388,7 @@ export class TranslateService {
388388
}
389389

390390
return concat(
391-
this.get(key, interpolateParams),
391+
defer(() => this.get(key, interpolateParams)),
392392
this.onTranslationChange.pipe(
393393
switchMap((event: TranslationChangeEvent) => {
394394
const res = this.getParsedResult(event.translations, key, interpolateParams);
@@ -413,7 +413,7 @@ export class TranslateService {
413413
}
414414

415415
return concat(
416-
this.get(key, interpolateParams),
416+
defer(() => this.get(key, interpolateParams)),
417417
this.onLangChange.pipe(
418418
switchMap((event: LangChangeEvent) => {
419419
const res = this.getParsedResult(event.translations, key, interpolateParams);

projects/ngx-translate/core/tests/translate.service.spec.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {fakeAsync, TestBed, tick} from "@angular/core/testing";
22
import {Observable, of, timer, zip, defer} from "rxjs";
3-
import {mapTo, take, toArray} from 'rxjs/operators';
3+
import {mapTo, take, toArray, first} from 'rxjs/operators';
44
import {LangChangeEvent, TranslateLoader, TranslateModule, TranslateService, TranslationChangeEvent} from '../src/public_api';
55

66
let translations: any = {"TEST": "This is a test"};
@@ -299,6 +299,37 @@ describe('TranslateService', () => {
299299
translate.use('en');
300300
});
301301

302+
it('should update lazy streaming translations on translation change', (done: Function) => {
303+
translations = {"TEST": "This is a test"};
304+
translate.use('en');
305+
306+
const translation$ = translate.getStreamOnTranslationChange('TEST');
307+
308+
translate.setTranslation('en', {"TEST": "This is a test2"});
309+
310+
translation$.pipe(first()).subscribe((res: string[]) => {
311+
const expected = "This is a test2";
312+
expect(res).toEqual(expected);
313+
done();
314+
});
315+
});
316+
317+
it('should update lazy streaming translations on language change', (done: Function) => {
318+
translations = {"TEST": "This is a test"};
319+
translate.use('en');
320+
321+
const translation$ = translate.stream('TEST');
322+
323+
translate.setTranslation('nl', {"TEST": "Dit is een test"});
324+
translate.use('nl');
325+
326+
translation$.pipe(first()).subscribe((res: string[]) => {
327+
const expected = 'Dit is een test';
328+
expect(res).toEqual(expected);
329+
done();
330+
});
331+
});
332+
302333
it('should update streaming translations of an array on language change', (done: Function) => {
303334
const en = {"TEST": "This is a test", "TEST2": "This is a test2"};
304335
const nl = {"TEST": "Dit is een test", "TEST2": "Dit is een test2"};

0 commit comments

Comments
 (0)