1
- import { Directive , ElementRef , AfterViewChecked , Input , OnDestroy } from '@angular/core' ;
1
+ import { Directive , ElementRef , AfterViewChecked , Input , OnDestroy , ChangeDetectorRef } from '@angular/core' ;
2
2
import { Subscription } from 'rxjs' ;
3
3
import { isDefined } from './util' ;
4
4
import { TranslateService , LangChangeEvent } from './translate.service' ;
@@ -11,6 +11,7 @@ import {DefaultLangChangeEvent} from "./translate.service";
11
11
export class TranslateDirective implements AfterViewChecked , OnDestroy {
12
12
key : string ;
13
13
lastParams : any ;
14
+ currentParams : any ;
14
15
onLangChangeSub : Subscription ;
15
16
onDefaultLangChangeSub : Subscription ;
16
17
onTranslationChangeSub : Subscription ;
@@ -22,9 +23,14 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
22
23
}
23
24
}
24
25
25
- @Input ( ) translateParams : any ;
26
+ @Input ( ) set translateParams ( params : any ) {
27
+ if ( this . currentParams !== params ) {
28
+ this . currentParams = params ;
29
+ this . checkNodes ( ) ;
30
+ }
31
+ }
26
32
27
- constructor ( private translateService : TranslateService , private element : ElementRef ) {
33
+ constructor ( private translateService : TranslateService , private element : ElementRef , private _ref : ChangeDetectorRef ) {
28
34
// subscribe to onTranslationChange event, in case the translations of the current lang change
29
35
if ( ! this . onTranslationChangeSub ) {
30
36
this . onTranslationChangeSub = this . translateService . onTranslationChange . subscribe ( ( event : TranslationChangeEvent ) => {
@@ -83,12 +89,11 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
83
89
84
90
updateValue ( key : string , node : any , translations : any ) {
85
91
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 ) {
88
93
return ;
89
94
}
90
95
91
- this . lastParams = interpolateParams ;
96
+ this . lastParams = this . currentParams ;
92
97
93
98
let onTranslation = ( res : string ) => {
94
99
if ( res !== key ) {
@@ -100,17 +105,18 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
100
105
node . currentValue = isDefined ( res ) ? res : ( node . originalContent || key ) ;
101
106
// we replace in the original content to preserve spaces that we might have trimmed
102
107
node . textContent = this . key ? node . currentValue : node . originalContent . replace ( key , node . currentValue ) ;
108
+ this . _ref . markForCheck ( ) ;
103
109
} ;
104
110
105
111
if ( isDefined ( translations ) ) {
106
- let res = this . translateService . getParsedResult ( translations , key , interpolateParams ) ;
112
+ let res = this . translateService . getParsedResult ( translations , key , this . currentParams ) ;
107
113
if ( typeof res . subscribe === "function" ) {
108
114
res . subscribe ( onTranslation ) ;
109
115
} else {
110
116
onTranslation ( res ) ;
111
117
}
112
118
} else {
113
- this . translateService . get ( key , interpolateParams ) . subscribe ( onTranslation ) ;
119
+ this . translateService . get ( key , this . currentParams ) . subscribe ( onTranslation ) ;
114
120
}
115
121
}
116
122
}
0 commit comments