Skip to content

Commit 35427b0

Browse files
author
David P. Mott
authored
fix(directive): trim white spaces to find matching keys
Fixes #998
1 parent 355f239 commit 35427b0

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

projects/ngx-translate/core/src/lib/translate.directive.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
7878
let content = this.getContent(node);
7979
let trimmedContent = content.trim();
8080
if (trimmedContent.length) {
81-
// we want to use the content as a key, not the translation value
82-
if (content !== node.currentValue) {
83-
key = trimmedContent;
84-
// the content was changed from the user, we'll use it as a reference if needed
85-
node.originalContent = this.getContent(node);
86-
} else if (node.originalContent && forceUpdate) { // the content seems ok, but the lang has changed
81+
if (node.originalContent && forceUpdate) { // the content seems ok, but the lang has changed
8782
node.lastKey = null;
8883
// the current content is the translation, not the key, use the last real content as key
8984
key = node.originalContent.trim();
85+
} else if (content !== node.currentValue) {
86+
// we want to use the content as a key, not the translation value
87+
key = trimmedContent;
88+
// the content was changed from the user, we'll use it as a reference if needed
89+
node.originalContent = node.originalContent || content;
9090
}
9191
}
9292
}

projects/ngx-translate/core/tests/translate.directive.spec.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {TranslateModule, TranslateService} from '../src/public_api';
77
selector: 'hmx-app',
88
changeDetection: ChangeDetectionStrategy.OnPush,
99
template: `
10-
<div #noKey translate>TEST</div>
10+
<div #noKey translate>
11+
TEST
12+
</div>
1113
<div #withKey [translate]="'TEST'">Some init content</div>
1214
<div #noContent [translate]="'TEST'"></div>
1315
<div #withOtherElements translate>TEST1 <span>Hey</span> TEST2</div>
@@ -53,12 +55,12 @@ describe('TranslateDirective', () => {
5355
});
5456

5557
it('should translate a string using the container value', () => {
56-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
58+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(' TEST ');
5759

5860
translate.setTranslation('en', {"TEST": "This is a test"});
5961
translate.use('en');
6062

61-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('This is a test');
63+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(' This is a test ');
6264
});
6365

6466
it('should translate a string using the key value', () => {
@@ -81,6 +83,16 @@ describe('TranslateDirective', () => {
8183
expect(fixture.componentInstance.withOtherElements.nativeElement.innerHTML).toEqual('Awesome <span>Hey</span> it works');
8284
});
8385

86+
it('should translate first child strings without recursion', () => {
87+
// replace the content with the key
88+
expect(fixture.componentInstance.withOtherElements.nativeElement.innerHTML).toEqual('TEST1 <span>Hey</span> TEST2');
89+
90+
translate.setTranslation('en', {"TEST1": "TEST2", "TEST2": "it works"});
91+
translate.use('en');
92+
93+
expect(fixture.componentInstance.withOtherElements.nativeElement.innerHTML).toEqual('TEST2 <span>Hey</span> it works');
94+
});
95+
8496
it('should translate a string with params and a key', () => {
8597
// replace the content with the key
8698
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST');
@@ -119,26 +131,26 @@ describe('TranslateDirective', () => {
119131
});
120132

121133
it('should update the DOM when the lang changes', () => {
122-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
134+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(' TEST ');
123135
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST');
124136
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual('TEST');
125137

126138
translate.setTranslation('en', {"TEST": "This is a test"});
127139
translate.setTranslation('fr', {"TEST": "C'est un test"});
128140

129141
translate.use('en');
130-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('This is a test');
142+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(' This is a test ');
131143
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('This is a test');
132144
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual('This is a test');
133145

134146
translate.use('fr');
135-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual("C'est un test");
147+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(" C'est un test ");
136148
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual("C'est un test");
137149
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual("C'est un test");
138150
});
139151

140152
it('should update the DOM when the lang changes and the translation ends with space', () => {
141-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
153+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(' TEST ');
142154
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST');
143155
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual('TEST');
144156

@@ -149,26 +161,26 @@ describe('TranslateDirective', () => {
149161
translate.setTranslation('fr', {"TEST": fr});
150162

151163
translate.use('en');
152-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(en);
164+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(` ${en} `);
153165
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual(en);
154166
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual(en);
155167

156168
translate.use('fr');
157-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(fr);
169+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(` ${fr} `);
158170
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual(fr);
159171
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual(fr);
160172
});
161173

162174
it('should update the DOM when the default lang changes', () => {
163-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
175+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(' TEST ');
164176

165177
translate.setTranslation('en', {"TEST": "This is a test"});
166178
translate.setTranslation('fr', {"TEST": "C'est un test"});
167179
translate.setDefaultLang('en');
168-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('This is a test');
180+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(' This is a test ');
169181

170182
translate.setDefaultLang('fr');
171-
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual("C'est un test");
183+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(" C'est un test ");
172184
});
173185

174186
it('should unsubscribe from lang change subscription on destroy', () => {

0 commit comments

Comments
 (0)