Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(service): make initial translation streaming lazy #960

Merged
merged 1 commit into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions projects/ngx-translate/core/src/lib/translate.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {EventEmitter, Inject, Injectable, InjectionToken} from "@angular/core";
import {concat, forkJoin, isObservable, Observable, of} from "rxjs";
import {concat, forkJoin, isObservable, Observable, of, defer} from "rxjs";
import {concatMap, map, shareReplay, switchMap, take} from "rxjs/operators";
import {MissingTranslationHandler, MissingTranslationHandlerParams} from "./missing-translation-handler";
import {TranslateCompiler} from "./translate.compiler";
Expand Down Expand Up @@ -388,7 +388,7 @@ export class TranslateService {
}

return concat(
this.get(key, interpolateParams),
defer(() => this.get(key, interpolateParams)),
this.onTranslationChange.pipe(
switchMap((event: TranslationChangeEvent) => {
const res = this.getParsedResult(event.translations, key, interpolateParams);
Expand All @@ -413,7 +413,7 @@ export class TranslateService {
}

return concat(
this.get(key, interpolateParams),
defer(() => this.get(key, interpolateParams)),
this.onLangChange.pipe(
switchMap((event: LangChangeEvent) => {
const res = this.getParsedResult(event.translations, key, interpolateParams);
Expand Down
33 changes: 32 additions & 1 deletion projects/ngx-translate/core/tests/translate.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {fakeAsync, TestBed, tick} from "@angular/core/testing";
import {Observable, of, timer, zip, defer} from "rxjs";
import {mapTo, take, toArray} from 'rxjs/operators';
import {mapTo, take, toArray, first} from 'rxjs/operators';
import {LangChangeEvent, TranslateLoader, TranslateModule, TranslateService, TranslationChangeEvent} from '../src/public_api';

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

it('should update lazy streaming translations on translation change', (done: Function) => {
translations = {"TEST": "This is a test"};
translate.use('en');

const translation$ = translate.getStreamOnTranslationChange('TEST');

translate.setTranslation('en', {"TEST": "This is a test2"});

translation$.pipe(first()).subscribe((res: string[]) => {
const expected = "This is a test2";
expect(res).toEqual(expected);
done();
});
});

it('should update lazy streaming translations on language change', (done: Function) => {
translations = {"TEST": "This is a test"};
translate.use('en');

const translation$ = translate.stream('TEST');

translate.setTranslation('nl', {"TEST": "Dit is een test"});
translate.use('nl');

translation$.pipe(first()).subscribe((res: string[]) => {
const expected = 'Dit is een test';
expect(res).toEqual(expected);
done();
});
});

it('should update streaming translations of an array on language change', (done: Function) => {
const en = {"TEST": "This is a test", "TEST2": "This is a test2"};
const nl = {"TEST": "Dit is een test", "TEST2": "Dit is een test2"};
Expand Down