1- import { Directive , ElementRef , AfterViewChecked , Input , OnDestroy } from '@angular/core' ;
1+ import { Directive , ElementRef , AfterViewChecked , Input , OnDestroy , ChangeDetectorRef } from '@angular/core' ;
22import { Subscription } from 'rxjs' ;
33import { isDefined } from './util' ;
44import { TranslateService , LangChangeEvent } from './translate.service' ;
@@ -11,6 +11,7 @@ import {DefaultLangChangeEvent} from "./translate.service";
1111export 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 }
0 commit comments