UNPKG

5.51 kBJavaScriptView Raw
1import { Pipe, Injectable, ChangeDetectorRef } from '@angular/core';
2import { TranslateService } from './translate.service';
3import { equals, isDefined } from './util';
4export 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 // if we ask another time for the same key, return the last value
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 // we accept objects written in the template such as {n:1}, {'n':1}, {n:'v'}
45 // which is why we might need to change it to real JSON objects such as {"n":1} or {"n":"v"}
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 // store the query, in case it changes
61 this.lastKey = query;
62 // store the params, in case they change
63 this.lastParams = args;
64 // set the value
65 this.updateValue(query, interpolateParams);
66 // if there is a subscription to onLangChange, clean it
67 this._dispose();
68 // subscribe to onTranslationChange event, in case the translations change
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 // subscribe to onLangChange event, in case the language changes
78 if (!this.onLangChange) {
79 this.onLangChange = this.translate.onLangChange.subscribe(function (event) {
80 if (_this.lastKey) {
81 _this.lastKey = null; // we want to make sure it doesn't return the same value until it's been updated
82 _this.updateValue(query, interpolateParams, event.translations);
83 }
84 });
85 }
86 // subscribe to onDefaultLangChange event, in case the default language changes
87 if (!this.onDefaultLangChange) {
88 this.onDefaultLangChange = this.translate.onDefaultLangChange.subscribe(function () {
89 if (_this.lastKey) {
90 _this.lastKey = null; // we want to make sure it doesn't return the same value until it's been updated
91 _this.updateValue(query, interpolateParams);
92 }
93 });
94 }
95 return this.value;
96 };
97 /**
98 * Clean any existing subscription to change events
99 * @private
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 TranslatePipe.decorators = [
119 { type: Injectable },
120 { type: Pipe, args: [{
121 name: 'translate',
122 pure: false // required to update the value when the promise is resolved
123 },] },
124 ];
125 /** @nocollapse */
126 TranslatePipe.ctorParameters = function () { return [
127 { type: TranslateService, },
128 { type: ChangeDetectorRef, },
129 ]; };
130 return TranslatePipe;
131}());