Skip to content

Commit 230b4fb

Browse files
committed
fix(TranslateDirective): detect changes when params are updated
Fixes #423
1 parent 6715233 commit 230b4fb

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

src/translate.directive.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Directive, ElementRef, AfterViewChecked, Input, OnDestroy} from '@angular/core';
1+
import {Directive, ElementRef, AfterViewChecked, Input, OnDestroy, ChangeDetectorRef} from '@angular/core';
22
import {Subscription} from 'rxjs';
33
import {isDefined} from './util';
44
import {TranslateService, LangChangeEvent} from './translate.service';
@@ -11,6 +11,7 @@ import {DefaultLangChangeEvent} from "./translate.service";
1111
export class TranslateDirective implements AfterViewChecked, OnDestroy {
1212
key: string;
1313
lastParams: any;
14+
currentParams: any;
1415
onLangChangeSub: Subscription;
1516
onDefaultLangChangeSub: Subscription;
1617
onTranslationChangeSub: Subscription;
@@ -22,9 +23,14 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
2223
}
2324
}
2425

25-
@Input() translateParams: any;
26+
@Input() set translateParams(params: any) {
27+
if(this.currentParams !== params) {
28+
this.currentParams = params;
29+
this.checkNodes();
30+
}
31+
}
2632

27-
constructor(private translateService: TranslateService, private element: ElementRef) {
33+
constructor(private translateService: TranslateService, private element: ElementRef, private _ref: ChangeDetectorRef) {
2834
// subscribe to onTranslationChange event, in case the translations of the current lang change
2935
if(!this.onTranslationChangeSub) {
3036
this.onTranslationChangeSub = this.translateService.onTranslationChange.subscribe((event: TranslationChangeEvent) => {
@@ -83,12 +89,11 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
8389

8490
updateValue(key: string, node: any, translations: any) {
8591
if(key) {
86-
let interpolateParams: Object = this.translateParams;
87-
if(node.lastKey === key && this.lastParams === interpolateParams) {
92+
if(node.lastKey === key && this.lastParams === this.currentParams) {
8893
return;
8994
}
9095

91-
this.lastParams = interpolateParams;
96+
this.lastParams = this.currentParams;
9297

9398
let onTranslation = (res: string) => {
9499
if(res !== key) {
@@ -100,17 +105,18 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
100105
node.currentValue = isDefined(res) ? res : (node.originalContent || key);
101106
// we replace in the original content to preserve spaces that we might have trimmed
102107
node.textContent = this.key ? node.currentValue : node.originalContent.replace(key, node.currentValue);
108+
this._ref.markForCheck();
103109
};
104110

105111
if(isDefined(translations)) {
106-
let res = this.translateService.getParsedResult(translations, key, interpolateParams);
112+
let res = this.translateService.getParsedResult(translations, key, this.currentParams);
107113
if(typeof res.subscribe === "function") {
108114
res.subscribe(onTranslation);
109115
} else {
110116
onTranslation(res);
111117
}
112118
} else {
113-
this.translateService.get(key, interpolateParams).subscribe(onTranslation);
119+
this.translateService.get(key, this.currentParams).subscribe(onTranslation);
114120
}
115121
}
116122
}

tests/translate.directive.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {TranslateService, TranslateModule} from '../index';
1111
<div #noKey translate>TEST</div>
1212
<div #withKey [translate]="'TEST'">Some init content</div>
1313
<div #withOtherElements translate>TEST1 <span>Hey</span> TEST2</div>
14-
<div #withParams [translate]="'TEST'" [translateParams]="{value: 'ok'}">Some init content</div>
15-
<div #withParamsNoKey translate [translateParams]="{value: 'ok'}">TEST</div>
14+
<div #withParams [translate]="'TEST'" [translateParams]="value">Some init content</div>
15+
<div #withParamsNoKey translate [translateParams]="value">TEST</div>
1616
`
1717
})
1818
class App {
@@ -22,6 +22,7 @@ class App {
2222
@ViewChild('withOtherElements') withOtherElements: ElementRef;
2323
@ViewChild('withParams') withParams: ElementRef;
2424
@ViewChild('withParamsNoKey') withParamsNoKey: ElementRef;
25+
value = {value: 'ok'};
2526

2627
constructor(viewContainerRef: ViewContainerRef) {
2728
this.viewContainerRef = viewContainerRef;
@@ -102,6 +103,20 @@ describe('TranslateDirective', () => {
102103
expect(fixture.componentInstance.withParamsNoKey.nativeElement.innerHTML).toEqual('It is ok');
103104
});
104105

106+
it('should update the translation when params change', () => {
107+
// replace the content with the key
108+
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST');
109+
110+
translate.setTranslation('en', {"TEST": "It is {{value}}"});
111+
translate.use('en');
112+
113+
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('It is ok');
114+
fixture.componentInstance.value = {value: 'changed'};
115+
fixture.detectChanges();
116+
117+
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('It is changed');
118+
});
119+
105120
it('should update the DOM when the lang changes', () => {
106121
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
107122

0 commit comments

Comments
 (0)