Skip to content

Commit 54aba57

Browse files
committed
fix(directive): support server side (universal)
Fixes #377 Closes #378
1 parent f1e693a commit 54aba57

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/translate.directive.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
6464
// if the element is empty
6565
if(!nodes.length) {
6666
// we add the key as content
67-
this.element.nativeElement.textContent = this.key;
67+
this.setContent(this.element.nativeElement, this.key);
6868
nodes = this.element.nativeElement.childNodes;
6969
}
7070
for(let i = 0; i < nodes.length; ++i) {
@@ -77,13 +77,13 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
7777
node.lastKey = null;
7878
}
7979
} else {
80-
let content = node.textContent.trim();
80+
let content = this.getContent(node).trim();
8181
if(content.length) {
8282
// we want to use the content as a key, not the translation value
8383
if(content !== node.currentValue) {
8484
key = content;
8585
// the content was changed from the user, we'll use it as a reference if needed
86-
node.originalContent = node.textContent;
86+
node.originalContent = this.getContent(node);
8787
} else if(node.originalContent && forceUpdate) { // the content seems ok, but the lang has changed
8888
node.lastKey = null;
8989
// the current content is the translation, not the key, use the last real content as key
@@ -109,11 +109,11 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
109109
node.lastKey = key;
110110
}
111111
if(!node.originalContent) {
112-
node.originalContent = node.textContent;
112+
node.originalContent = this.getContent(node);
113113
}
114114
node.currentValue = isDefined(res) ? res : (node.originalContent || key);
115115
// we replace in the original content to preserve spaces that we might have trimmed
116-
node.textContent = this.key ? node.currentValue : node.originalContent.replace(key, node.currentValue);
116+
this.setContent(node, this.key ? node.currentValue : node.originalContent.replace(key, node.currentValue));
117117
this._ref.markForCheck();
118118
};
119119

@@ -130,6 +130,18 @@ export class TranslateDirective implements AfterViewChecked, OnDestroy {
130130
}
131131
}
132132

133+
getContent(node: any): string {
134+
return isDefined(node.textContent) ? node.textContent : node.data;
135+
}
136+
137+
setContent(node: any, content: string): void {
138+
if(isDefined(node.textContent)) {
139+
node.textContent = content;
140+
} else {
141+
node.data = content;
142+
}
143+
}
144+
133145
ngOnDestroy() {
134146
if(this.onLangChangeSub) {
135147
this.onLangChangeSub.unsubscribe();

0 commit comments

Comments
 (0)