1 | import { Pipe, Injectable, ChangeDetectorRef } from '@angular/core';
|
2 | import { TranslateService } from './translate.service';
|
3 | import { equals, isDefined } from './util';
|
4 | var TranslatePipe = (function () {
|
5 | function TranslatePipe(translate, _ref) {
|
6 | this.translate = translate;
|
7 | this._ref = _ref;
|
8 | this.value = '';
|
9 | }
|
10 | TranslatePipe.prototype.updateValue = function (key, interpolateParams, translations) {
|
11 | var _this = this;
|
12 | var onTranslation = function (res) {
|
13 | _this.value = res !== undefined ? res : key;
|
14 | _this.lastKey = key;
|
15 | _this._ref.markForCheck();
|
16 | };
|
17 | if (translations) {
|
18 | var res = this.translate.getParsedResult(translations, key, interpolateParams);
|
19 | if (typeof res.subscribe === 'function') {
|
20 | res.subscribe(onTranslation);
|
21 | }
|
22 | else {
|
23 | onTranslation(res);
|
24 | }
|
25 | }
|
26 | this.translate.get(key, interpolateParams).subscribe(onTranslation);
|
27 | };
|
28 | TranslatePipe.prototype.transform = function (query) {
|
29 | var _this = this;
|
30 | var args = [];
|
31 | for (var _i = 1; _i < arguments.length; _i++) {
|
32 | args[_i - 1] = arguments[_i];
|
33 | }
|
34 | if (!query || query.length === 0) {
|
35 | return query;
|
36 | }
|
37 |
|
38 | if (equals(query, this.lastKey) && equals(args, this.lastParams)) {
|
39 | return this.value;
|
40 | }
|
41 | var interpolateParams;
|
42 | if (isDefined(args[0]) && args.length) {
|
43 | if (typeof args[0] === 'string' && args[0].length) {
|
44 |
|
45 |
|
46 | var validArgs = args[0]
|
47 | .replace(/(\')?([a-zA-Z0-9_]+)(\')?(\s)?:/g, '"$2":')
|
48 | .replace(/:(\s)?(\')(.*?)(\')/g, ':"$3"');
|
49 | try {
|
50 | interpolateParams = JSON.parse(validArgs);
|
51 | }
|
52 | catch (e) {
|
53 | throw new SyntaxError("Wrong parameter in TranslatePipe. Expected a valid Object, received: " + args[0]);
|
54 | }
|
55 | }
|
56 | else if (typeof args[0] === 'object' && !Array.isArray(args[0])) {
|
57 | interpolateParams = args[0];
|
58 | }
|
59 | }
|
60 |
|
61 | this.lastKey = query;
|
62 |
|
63 | this.lastParams = args;
|
64 |
|
65 | this.updateValue(query, interpolateParams);
|
66 |
|
67 | this._dispose();
|
68 |
|
69 | if (!this.onTranslationChange) {
|
70 | this.onTranslationChange = this.translate.onTranslationChange.subscribe(function (event) {
|
71 | if (_this.lastKey && event.lang === _this.translate.currentLang) {
|
72 | _this.lastKey = null;
|
73 | _this.updateValue(query, interpolateParams, event.translations);
|
74 | }
|
75 | });
|
76 | }
|
77 |
|
78 | if (!this.onLangChange) {
|
79 | this.onLangChange = this.translate.onLangChange.subscribe(function (event) {
|
80 | if (_this.lastKey) {
|
81 | _this.lastKey = null;
|
82 | _this.updateValue(query, interpolateParams, event.translations);
|
83 | }
|
84 | });
|
85 | }
|
86 |
|
87 | if (!this.onDefaultLangChange) {
|
88 | this.onDefaultLangChange = this.translate.onDefaultLangChange.subscribe(function () {
|
89 | if (_this.lastKey) {
|
90 | _this.lastKey = null;
|
91 | _this.updateValue(query, interpolateParams);
|
92 | }
|
93 | });
|
94 | }
|
95 | return this.value;
|
96 | };
|
97 | |
98 |
|
99 |
|
100 |
|
101 | TranslatePipe.prototype._dispose = function () {
|
102 | if (typeof this.onTranslationChange !== 'undefined') {
|
103 | this.onTranslationChange.unsubscribe();
|
104 | this.onTranslationChange = undefined;
|
105 | }
|
106 | if (typeof this.onLangChange !== 'undefined') {
|
107 | this.onLangChange.unsubscribe();
|
108 | this.onLangChange = undefined;
|
109 | }
|
110 | if (typeof this.onDefaultLangChange !== 'undefined') {
|
111 | this.onDefaultLangChange.unsubscribe();
|
112 | this.onDefaultLangChange = undefined;
|
113 | }
|
114 | };
|
115 | TranslatePipe.prototype.ngOnDestroy = function () {
|
116 | this._dispose();
|
117 | };
|
118 | return TranslatePipe;
|
119 | }());
|
120 | export { TranslatePipe };
|
121 | TranslatePipe.decorators = [
|
122 | { type: Injectable },
|
123 | { type: Pipe, args: [{
|
124 | name: 'translate',
|
125 | pure: false
|
126 | },] },
|
127 | ];
|
128 |
|
129 | TranslatePipe.ctorParameters = function () { return [
|
130 | { type: TranslateService, },
|
131 | { type: ChangeDetectorRef, },
|
132 | ]; };
|