Skip to content

Commit cf45ef5

Browse files
CodeAndWebocombe
authored andcommitted
fix(directive): fix for translations starting or ending with whitespaces
Fixes #790
1 parent 61aacee commit cf45ef5

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/src/translate.directive.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
7575
node.lastKey = null;
7676
}
7777
} else {
78-
let content = this.getContent(node).trim();
79-
if (content.length) {
78+
let content = this.getContent(node);
79+
let trimmedContent = content.trim();
80+
if (trimmedContent.length) {
8081
// we want to use the content as a key, not the translation value
8182
if (content !== node.currentValue) {
82-
key = content;
83+
key = trimmedContent;
8384
// the content was changed from the user, we'll use it as a reference if needed
8485
node.originalContent = this.getContent(node);
8586
} else if (node.originalContent && forceUpdate) { // the content seems ok, but the lang has changed

tests/translate.directive.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ describe('TranslateDirective', () => {
137137
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual("C'est un test");
138138
});
139139

140+
it('should update the DOM when the lang changes and the translation ends with space', () => {
141+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
142+
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual('TEST');
143+
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual('TEST');
144+
145+
const en=" This is a test - with spaces ";
146+
const fr=" C'est un test - pardon, je ne parle pas francais :) ";
147+
148+
translate.setTranslation('en', {"TEST": en});
149+
translate.setTranslation('fr', {"TEST": fr});
150+
151+
translate.use('en');
152+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(en);
153+
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual(en);
154+
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual(en);
155+
156+
translate.use('fr');
157+
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual(fr);
158+
expect(fixture.componentInstance.withParams.nativeElement.innerHTML).toEqual(fr);
159+
expect(fixture.componentInstance.noContent.nativeElement.innerHTML).toEqual(fr);
160+
});
161+
140162
it('should update the DOM when the default lang changes', () => {
141163
expect(fixture.componentInstance.noKey.nativeElement.innerHTML).toEqual('TEST');
142164

0 commit comments

Comments
 (0)