From 230b4fb6a45c628e962d7426569589dfcf12336f Mon Sep 17 00:00:00 2001 From: Olivier Combe Date: Tue, 14 Feb 2017 18:07:22 +0100 Subject: [PATCH] fix(TranslateDirective): detect changes when params are updated Fixes #423 --- src/translate.directive.ts | 22 ++++++++++++++-------- tests/translate.directive.spec.ts | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/translate.directive.ts b/src/translate.directive.ts index 62e845fd..d0098fd7 100644 --- a/src/translate.directive.ts +++ b/src/translate.directive.ts @@ -1,4 +1,4 @@ -import {Directive, ElementRef, AfterViewChecked, Input, OnDestroy} from '@angular/core'; +import {Directive, ElementRef, AfterViewChecked, Input, OnDestroy, ChangeDetectorRef} from '@angular/core'; import {Subscription} from 'rxjs'; import {isDefined} from './util'; import {TranslateService, LangChangeEvent} from './translate.service'; @@ -11,6 +11,7 @@ import {DefaultLangChangeEvent} from "./translate.service"; export class TranslateDirective implements AfterViewChecked, OnDestroy { key: string; lastParams: any; + currentParams: any; onLangChangeSub: Subscription; onDefaultLangChangeSub: Subscription; onTranslationChangeSub: Subscription; @@ -22,9 +23,14 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy { } } - @Input() translateParams: any; + @Input() set translateParams(params: any) { + if(this.currentParams !== params) { + this.currentParams = params; + this.checkNodes(); + } + } - constructor(private translateService: TranslateService, private element: ElementRef) { + constructor(private translateService: TranslateService, private element: ElementRef, private _ref: ChangeDetectorRef) { // subscribe to onTranslationChange event, in case the translations of the current lang change if(!this.onTranslationChangeSub) { this.onTranslationChangeSub = this.translateService.onTranslationChange.subscribe((event: TranslationChangeEvent) => { @@ -83,12 +89,11 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy { updateValue(key: string, node: any, translations: any) { if(key) { - let interpolateParams: Object = this.translateParams; - if(node.lastKey === key && this.lastParams === interpolateParams) { + if(node.lastKey === key && this.lastParams === this.currentParams) { return; } - this.lastParams = interpolateParams; + this.lastParams = this.currentParams; let onTranslation = (res: string) => { if(res !== key) { @@ -100,17 +105,18 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy { node.currentValue = isDefined(res) ? res : (node.originalContent || key); // we replace in the original content to preserve spaces that we might have trimmed node.textContent = this.key ? node.currentValue : node.originalContent.replace(key, node.currentValue); + this._ref.markForCheck(); }; if(isDefined(translations)) { - let res = this.translateService.getParsedResult(translations, key, interpolateParams); + let res = this.translateService.getParsedResult(translations, key, this.currentParams); if(typeof res.subscribe === "function") { res.subscribe(onTranslation); } else { onTranslation(res); } } else { - this.translateService.get(key, interpolateParams).subscribe(onTranslation); + this.translateService.get(key, this.currentParams).subscribe(onTranslation); } } } diff --git a/tests/translate.directive.spec.ts b/tests/translate.directive.spec.ts index 87b10f0e..42f161eb 100644 --- a/tests/translate.directive.spec.ts +++ b/tests/translate.directive.spec.ts @@ -11,8 +11,8 @@ import {TranslateService, TranslateModule} from '../index';
TEST
Some init content
TEST1 Hey TEST2
-
Some init content
-
TEST
+
Some init content
+
TEST
` }) class App { @@ -22,6 +22,7 @@ class App { @ViewChild('withOtherElements') withOtherElements: ElementRef; @ViewChild('withParams') withParams: ElementRef; @ViewChild('withParamsNoKey') withParamsNoKey: ElementRef; + value = {value: 'ok'}; constructor(viewContainerRef: ViewContainerRef) { this.viewContainerRef = viewContainerRef; @@ -102,6 +103,20 @@ describe('TranslateDirective', () => { expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('It is ok'); }); + it('should update the translation when params change', () => { + // replace the content with the key + expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST'); + + translate.setTranslation('en', {"TEST": "It is {{value}}"}); + translate.use('en'); + + expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('It is ok'); + fixture.componentInstance.value = {value: 'changed'}; + fixture.detectChanges(); + + expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('It is changed'); + }); + it('should update the DOM when the lang changes', () => { expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');