1 | import { Directive, ElementRef, Input, ChangeDetectorRef } from '@angular/core';
|
2 | import { equals, isDefined } from './util';
|
3 | import { TranslateService } from './translate.service';
|
4 | var TranslateDirective = (function () {
|
5 | function TranslateDirective(translateService, element, _ref) {
|
6 | var _this = this;
|
7 | this.translateService = translateService;
|
8 | this.element = element;
|
9 | this._ref = _ref;
|
10 |
|
11 | if (!this.onTranslationChangeSub) {
|
12 | this.onTranslationChangeSub = this.translateService.onTranslationChange.subscribe(function (event) {
|
13 | if (event.lang === _this.translateService.currentLang) {
|
14 | _this.checkNodes(true, event.translations);
|
15 | }
|
16 | });
|
17 | }
|
18 |
|
19 | if (!this.onLangChangeSub) {
|
20 | this.onLangChangeSub = this.translateService.onLangChange.subscribe(function (event) {
|
21 | _this.checkNodes(true, event.translations);
|
22 | });
|
23 | }
|
24 |
|
25 | if (!this.onDefaultLangChangeSub) {
|
26 | this.onDefaultLangChangeSub = this.translateService.onDefaultLangChange.subscribe(function (event) {
|
27 | _this.checkNodes(true);
|
28 | });
|
29 | }
|
30 | }
|
31 | Object.defineProperty(TranslateDirective.prototype, "translate", {
|
32 | set: function (key) {
|
33 | if (key) {
|
34 | this.key = key;
|
35 | this.checkNodes();
|
36 | }
|
37 | },
|
38 | enumerable: true,
|
39 | configurable: true
|
40 | });
|
41 | Object.defineProperty(TranslateDirective.prototype, "translateParams", {
|
42 | set: function (params) {
|
43 | if (!equals(this.currentParams, params)) {
|
44 | this.currentParams = params;
|
45 | this.checkNodes(true);
|
46 | }
|
47 | },
|
48 | enumerable: true,
|
49 | configurable: true
|
50 | });
|
51 | TranslateDirective.prototype.ngAfterViewChecked = function () {
|
52 | this.checkNodes();
|
53 | };
|
54 | TranslateDirective.prototype.checkNodes = function (forceUpdate, translations) {
|
55 | if (forceUpdate === void 0) { forceUpdate = false; }
|
56 | var nodes = this.element.nativeElement.childNodes;
|
57 |
|
58 | if (!nodes.length) {
|
59 |
|
60 | this.setContent(this.element.nativeElement, this.key);
|
61 | nodes = this.element.nativeElement.childNodes;
|
62 | }
|
63 | for (var i = 0; i < nodes.length; ++i) {
|
64 | var node = nodes[i];
|
65 | if (node.nodeType === 3) {
|
66 | var key = void 0;
|
67 | if (this.key) {
|
68 | key = this.key;
|
69 | if (forceUpdate) {
|
70 | node.lastKey = null;
|
71 | }
|
72 | }
|
73 | else {
|
74 | var content = this.getContent(node).trim();
|
75 | if (content.length) {
|
76 |
|
77 | if (content !== node.currentValue) {
|
78 | key = content;
|
79 |
|
80 | node.originalContent = this.getContent(node);
|
81 | }
|
82 | else if (node.originalContent && forceUpdate) {
|
83 | node.lastKey = null;
|
84 |
|
85 | key = node.originalContent.trim();
|
86 | }
|
87 | }
|
88 | }
|
89 | this.updateValue(key, node, translations);
|
90 | }
|
91 | }
|
92 | };
|
93 | TranslateDirective.prototype.updateValue = function (key, node, translations) {
|
94 | var _this = this;
|
95 | if (key) {
|
96 | if (node.lastKey === key && this.lastParams === this.currentParams) {
|
97 | return;
|
98 | }
|
99 | this.lastParams = this.currentParams;
|
100 | var onTranslation = function (res) {
|
101 | if (res !== key) {
|
102 | node.lastKey = key;
|
103 | }
|
104 | if (!node.originalContent) {
|
105 | node.originalContent = _this.getContent(node);
|
106 | }
|
107 | node.currentValue = isDefined(res) ? res : (node.originalContent || key);
|
108 |
|
109 | _this.setContent(node, _this.key ? node.currentValue : node.originalContent.replace(key, node.currentValue));
|
110 | _this._ref.markForCheck();
|
111 | };
|
112 | if (isDefined(translations)) {
|
113 | var res = this.translateService.getParsedResult(translations, key, this.currentParams);
|
114 | if (typeof res.subscribe === "function") {
|
115 | res.subscribe(onTranslation);
|
116 | }
|
117 | else {
|
118 | onTranslation(res);
|
119 | }
|
120 | }
|
121 | else {
|
122 | this.translateService.get(key, this.currentParams).subscribe(onTranslation);
|
123 | }
|
124 | }
|
125 | };
|
126 | TranslateDirective.prototype.getContent = function (node) {
|
127 | return isDefined(node.textContent) ? node.textContent : node.data;
|
128 | };
|
129 | TranslateDirective.prototype.setContent = function (node, content) {
|
130 | if (isDefined(node.textContent)) {
|
131 | node.textContent = content;
|
132 | }
|
133 | else {
|
134 | node.data = content;
|
135 | }
|
136 | };
|
137 | TranslateDirective.prototype.ngOnDestroy = function () {
|
138 | if (this.onLangChangeSub) {
|
139 | this.onLangChangeSub.unsubscribe();
|
140 | }
|
141 | if (this.onDefaultLangChangeSub) {
|
142 | this.onDefaultLangChangeSub.unsubscribe();
|
143 | }
|
144 | if (this.onTranslationChangeSub) {
|
145 | this.onTranslationChangeSub.unsubscribe();
|
146 | }
|
147 | };
|
148 | return TranslateDirective;
|
149 | }());
|
150 | export { TranslateDirective };
|
151 | TranslateDirective.decorators = [
|
152 | { type: Directive, args: [{
|
153 | selector: '[translate],[ngx-translate]'
|
154 | },] },
|
155 | ];
|
156 |
|
157 | TranslateDirective.ctorParameters = function () { return [
|
158 | { type: TranslateService, },
|
159 | { type: ElementRef, },
|
160 | { type: ChangeDetectorRef, },
|
161 | ]; };
|
162 | TranslateDirective.propDecorators = {
|
163 | 'translate': [{ type: Input },],
|
164 | 'translateParams': [{ type: Input },],
|
165 | };
|