From 920b95df45e98a097ef11824ba741bb0a9025b92 Mon Sep 17 00:00:00 2001 From: Belan Date: Wed, 14 Nov 2018 09:36:25 +0100 Subject: [PATCH] fix(TranslateService): compile translations only once (#956) Fixes #955 --- .../core/src/lib/translate.service.ts | 17 +++++++----- .../core/tests/translate.service.spec.ts | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/projects/ngx-translate/core/src/lib/translate.service.ts b/projects/ngx-translate/core/src/lib/translate.service.ts index 7a198494..e1b0c2c2 100644 --- a/projects/ngx-translate/core/src/lib/translate.service.ts +++ b/projects/ngx-translate/core/src/lib/translate.service.ts @@ -238,18 +238,23 @@ export class TranslateService { */ public getTranslation(lang: string): Observable { this.pending = true; - this.loadingTranslations = this.currentLoader.getTranslation(lang).pipe(share()); - - this.loadingTranslations.pipe(take(1)) + const loadingTranslations = this.currentLoader.getTranslation(lang).pipe(share()); + this.loadingTranslations = loadingTranslations.pipe( + take(1), + map((res: Object) => this.compiler.compileTranslations(res, lang)), + share() + ); + + this.loadingTranslations .subscribe((res: Object) => { - this.translations[lang] = this.compiler.compileTranslations(res, lang); + this.translations[lang] = res; this.updateLangs(); this.pending = false; }, (err: any) => { this.pending = false; }); - return this.loadingTranslations; + return loadingTranslations; } /** @@ -369,7 +374,7 @@ export class TranslateService { observer.error(err); }; this.loadingTranslations.subscribe((res: any) => { - res = this.getParsedResult(this.compiler.compileTranslations(res, this.currentLang), key, interpolateParams); + res = this.getParsedResult(res, key, interpolateParams); if (typeof res.subscribe === "function") { res.subscribe(onComplete, onError); } else { diff --git a/projects/ngx-translate/core/tests/translate.service.spec.ts b/projects/ngx-translate/core/tests/translate.service.spec.ts index 5a95a257..6448881b 100644 --- a/projects/ngx-translate/core/tests/translate.service.spec.ts +++ b/projects/ngx-translate/core/tests/translate.service.spec.ts @@ -406,4 +406,30 @@ describe('TranslateService', () => { expect(getTranslationCalls).toEqual(1); })); + + it('should compile translations only once, even when subscribing to translations while translations are loading', fakeAsync(() => { + spyOn(translate.currentLoader, 'getTranslation').and.callFake(() => { + return timer(1000).pipe(mapTo(of(translations))); + }); + + let translateCompilerCallCount = 0; + spyOn(translate.compiler, 'compile').and.callFake((value) => { + ++translateCompilerCallCount; + return value; + }); + spyOn(translate.compiler, 'compileTranslations').and.callFake((value) => { + ++translateCompilerCallCount; + return value; + }); + + translate.setDefaultLang('en-US'); + translate.get('TEST1').subscribe(); + translate.get('TEST2').subscribe(); + translate.get('TEST3').subscribe(); + translate.get('TEST4').subscribe(); + + tick(1001); + + expect(translateCompilerCallCount).toBe(1); + })); });