UNPKG

68.2 kBJavaScriptView Raw
1import { Injectable, EventEmitter, InjectionToken, Inject, Directive, ElementRef, ChangeDetectorRef, Input, Pipe, NgModule } from '@angular/core';
2import { __extends, __assign, __values } from 'tslib';
3import { of, isObservable, forkJoin, concat, defer } from 'rxjs';
4import { take, shareReplay, map, concatMap, switchMap } from 'rxjs/operators';
5
6/**
7 * @fileoverview added by tsickle
8 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
9 */
10/**
11 * @abstract
12 */
13var /**
14 * @abstract
15 */
16TranslateLoader = /** @class */ (function () {
17 function TranslateLoader() {
18 }
19 return TranslateLoader;
20}());
21if (false) {
22 /**
23 * @abstract
24 * @param {?} lang
25 * @return {?}
26 */
27 TranslateLoader.prototype.getTranslation = function (lang) { };
28}
29/**
30 * This loader is just a placeholder that does nothing, in case you don't need a loader at all
31 */
32var TranslateFakeLoader = /** @class */ (function (_super) {
33 __extends(TranslateFakeLoader, _super);
34 function TranslateFakeLoader() {
35 return _super !== null && _super.apply(this, arguments) || this;
36 }
37 /**
38 * @param {?} lang
39 * @return {?}
40 */
41 TranslateFakeLoader.prototype.getTranslation = /**
42 * @param {?} lang
43 * @return {?}
44 */
45 function (lang) {
46 return of({});
47 };
48 TranslateFakeLoader.decorators = [
49 { type: Injectable }
50 ];
51 return TranslateFakeLoader;
52}(TranslateLoader));
53
54/**
55 * @fileoverview added by tsickle
56 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
57 */
58/**
59 * @record
60 */
61function MissingTranslationHandlerParams() { }
62if (false) {
63 /**
64 * the key that's missing in translation files
65 * @type {?}
66 */
67 MissingTranslationHandlerParams.prototype.key;
68 /**
69 * an instance of the service that was unable to translate the key.
70 * @type {?}
71 */
72 MissingTranslationHandlerParams.prototype.translateService;
73 /**
74 * interpolation params that were passed along for translating the given key.
75 * @type {?|undefined}
76 */
77 MissingTranslationHandlerParams.prototype.interpolateParams;
78}
79/**
80 * @abstract
81 */
82var /**
83 * @abstract
84 */
85MissingTranslationHandler = /** @class */ (function () {
86 function MissingTranslationHandler() {
87 }
88 return MissingTranslationHandler;
89}());
90if (false) {
91 /**
92 * A function that handles missing translations.
93 *
94 * @abstract
95 * @param {?} params context for resolving a missing translation
96 * @return {?} a value or an observable
97 * If it returns a value, then this value is used.
98 * If it return an observable, the value returned by this observable will be used (except if the method was "instant").
99 * If it doesn't return then the key will be used as a value
100 */
101 MissingTranslationHandler.prototype.handle = function (params) { };
102}
103/**
104 * This handler is just a placeholder that does nothing, in case you don't need a missing translation handler at all
105 */
106var FakeMissingTranslationHandler = /** @class */ (function () {
107 function FakeMissingTranslationHandler() {
108 }
109 /**
110 * @param {?} params
111 * @return {?}
112 */
113 FakeMissingTranslationHandler.prototype.handle = /**
114 * @param {?} params
115 * @return {?}
116 */
117 function (params) {
118 return params.key;
119 };
120 FakeMissingTranslationHandler.decorators = [
121 { type: Injectable }
122 ];
123 return FakeMissingTranslationHandler;
124}());
125
126/**
127 * @fileoverview added by tsickle
128 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
129 */
130/* tslint:disable */
131/**
132 * Determines if two objects or two values are equivalent.
133 *
134 * Two objects or values are considered equivalent if at least one of the following is true:
135 *
136 * * Both objects or values pass `===` comparison.
137 * * Both objects or values are of the same type and all of their properties are equal by
138 * comparing them with `equals`.
139 *
140 * @param {?} o1 Object or value to compare.
141 * @param {?} o2 Object or value to compare.
142 * @return {?} true if arguments are equal.
143 */
144function equals(o1, o2) {
145 if (o1 === o2)
146 return true;
147 if (o1 === null || o2 === null)
148 return false;
149 if (o1 !== o1 && o2 !== o2)
150 return true; // NaN === NaN
151 // NaN === NaN
152 /** @type {?} */
153 var t1 = typeof o1;
154 /** @type {?} */
155 var t2 = typeof o2;
156 /** @type {?} */
157 var length;
158 /** @type {?} */
159 var key;
160 /** @type {?} */
161 var keySet;
162 if (t1 == t2 && t1 == 'object') {
163 if (Array.isArray(o1)) {
164 if (!Array.isArray(o2))
165 return false;
166 if ((length = o1.length) == o2.length) {
167 for (key = 0; key < length; key++) {
168 if (!equals(o1[key], o2[key]))
169 return false;
170 }
171 return true;
172 }
173 }
174 else {
175 if (Array.isArray(o2)) {
176 return false;
177 }
178 keySet = Object.create(null);
179 for (key in o1) {
180 if (!equals(o1[key], o2[key])) {
181 return false;
182 }
183 keySet[key] = true;
184 }
185 for (key in o2) {
186 if (!(key in keySet) && typeof o2[key] !== 'undefined') {
187 return false;
188 }
189 }
190 return true;
191 }
192 }
193 return false;
194}
195/* tslint:enable */
196/**
197 * @param {?} value
198 * @return {?}
199 */
200function isDefined(value) {
201 return typeof value !== 'undefined' && value !== null;
202}
203/**
204 * @param {?} item
205 * @return {?}
206 */
207function isObject(item) {
208 return (item && typeof item === 'object' && !Array.isArray(item));
209}
210/**
211 * @param {?} target
212 * @param {?} source
213 * @return {?}
214 */
215function mergeDeep(target, source) {
216 /** @type {?} */
217 var output = Object.assign({}, target);
218 if (isObject(target) && isObject(source)) {
219 Object.keys(source).forEach((/**
220 * @param {?} key
221 * @return {?}
222 */
223 function (key) {
224 var _a, _b;
225 if (isObject(source[key])) {
226 if (!(key in target)) {
227 Object.assign(output, (_a = {}, _a[key] = source[key], _a));
228 }
229 else {
230 output[key] = mergeDeep(target[key], source[key]);
231 }
232 }
233 else {
234 Object.assign(output, (_b = {}, _b[key] = source[key], _b));
235 }
236 }));
237 }
238 return output;
239}
240
241/**
242 * @fileoverview added by tsickle
243 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
244 */
245/**
246 * @abstract
247 */
248var /**
249 * @abstract
250 */
251TranslateParser = /** @class */ (function () {
252 function TranslateParser() {
253 }
254 return TranslateParser;
255}());
256if (false) {
257 /**
258 * Interpolates a string to replace parameters
259 * "This is a {{ key }}" ==> "This is a value", with params = { key: "value" }
260 * @abstract
261 * @param {?} expr
262 * @param {?=} params
263 * @return {?}
264 */
265 TranslateParser.prototype.interpolate = function (expr, params) { };
266 /**
267 * Gets a value from an object by composed key
268 * parser.getValue({ key1: { keyA: 'valueI' }}, 'key1.keyA') ==> 'valueI'
269 * @abstract
270 * @param {?} target
271 * @param {?} key
272 * @return {?}
273 */
274 TranslateParser.prototype.getValue = function (target, key) { };
275}
276var TranslateDefaultParser = /** @class */ (function (_super) {
277 __extends(TranslateDefaultParser, _super);
278 function TranslateDefaultParser() {
279 var _this = _super !== null && _super.apply(this, arguments) || this;
280 _this.templateMatcher = /{{\s?([^{}\s]*)\s?}}/g;
281 return _this;
282 }
283 /**
284 * @param {?} expr
285 * @param {?=} params
286 * @return {?}
287 */
288 TranslateDefaultParser.prototype.interpolate = /**
289 * @param {?} expr
290 * @param {?=} params
291 * @return {?}
292 */
293 function (expr, params) {
294 /** @type {?} */
295 var result;
296 if (typeof expr === 'string') {
297 result = this.interpolateString(expr, params);
298 }
299 else if (typeof expr === 'function') {
300 result = this.interpolateFunction(expr, params);
301 }
302 else {
303 // this should not happen, but an unrelated TranslateService test depends on it
304 result = (/** @type {?} */ (expr));
305 }
306 return result;
307 };
308 /**
309 * @param {?} target
310 * @param {?} key
311 * @return {?}
312 */
313 TranslateDefaultParser.prototype.getValue = /**
314 * @param {?} target
315 * @param {?} key
316 * @return {?}
317 */
318 function (target, key) {
319 /** @type {?} */
320 var keys = typeof key === 'string' ? key.split('.') : [key];
321 key = '';
322 do {
323 key += keys.shift();
324 if (isDefined(target) && isDefined(target[key]) && (typeof target[key] === 'object' || !keys.length)) {
325 target = target[key];
326 key = '';
327 }
328 else if (!keys.length) {
329 target = undefined;
330 }
331 else {
332 key += '.';
333 }
334 } while (keys.length);
335 return target;
336 };
337 /**
338 * @private
339 * @param {?} fn
340 * @param {?=} params
341 * @return {?}
342 */
343 TranslateDefaultParser.prototype.interpolateFunction = /**
344 * @private
345 * @param {?} fn
346 * @param {?=} params
347 * @return {?}
348 */
349 function (fn, params) {
350 return fn(params);
351 };
352 /**
353 * @private
354 * @param {?} expr
355 * @param {?=} params
356 * @return {?}
357 */
358 TranslateDefaultParser.prototype.interpolateString = /**
359 * @private
360 * @param {?} expr
361 * @param {?=} params
362 * @return {?}
363 */
364 function (expr, params) {
365 var _this = this;
366 if (!params) {
367 return expr;
368 }
369 return expr.replace(this.templateMatcher, (/**
370 * @param {?} substring
371 * @param {?} b
372 * @return {?}
373 */
374 function (substring, b) {
375 /** @type {?} */
376 var r = _this.getValue(params, b);
377 return isDefined(r) ? r : substring;
378 }));
379 };
380 TranslateDefaultParser.decorators = [
381 { type: Injectable }
382 ];
383 return TranslateDefaultParser;
384}(TranslateParser));
385if (false) {
386 /** @type {?} */
387 TranslateDefaultParser.prototype.templateMatcher;
388}
389
390/**
391 * @fileoverview added by tsickle
392 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
393 */
394/**
395 * @abstract
396 */
397var /**
398 * @abstract
399 */
400TranslateCompiler = /** @class */ (function () {
401 function TranslateCompiler() {
402 }
403 return TranslateCompiler;
404}());
405if (false) {
406 /**
407 * @abstract
408 * @param {?} value
409 * @param {?} lang
410 * @return {?}
411 */
412 TranslateCompiler.prototype.compile = function (value, lang) { };
413 /**
414 * @abstract
415 * @param {?} translations
416 * @param {?} lang
417 * @return {?}
418 */
419 TranslateCompiler.prototype.compileTranslations = function (translations, lang) { };
420}
421/**
422 * This compiler is just a placeholder that does nothing, in case you don't need a compiler at all
423 */
424var TranslateFakeCompiler = /** @class */ (function (_super) {
425 __extends(TranslateFakeCompiler, _super);
426 function TranslateFakeCompiler() {
427 return _super !== null && _super.apply(this, arguments) || this;
428 }
429 /**
430 * @param {?} value
431 * @param {?} lang
432 * @return {?}
433 */
434 TranslateFakeCompiler.prototype.compile = /**
435 * @param {?} value
436 * @param {?} lang
437 * @return {?}
438 */
439 function (value, lang) {
440 return value;
441 };
442 /**
443 * @param {?} translations
444 * @param {?} lang
445 * @return {?}
446 */
447 TranslateFakeCompiler.prototype.compileTranslations = /**
448 * @param {?} translations
449 * @param {?} lang
450 * @return {?}
451 */
452 function (translations, lang) {
453 return translations;
454 };
455 TranslateFakeCompiler.decorators = [
456 { type: Injectable }
457 ];
458 return TranslateFakeCompiler;
459}(TranslateCompiler));
460
461/**
462 * @fileoverview added by tsickle
463 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
464 */
465var TranslateStore = /** @class */ (function () {
466 function TranslateStore() {
467 /**
468 * The lang currently used
469 */
470 this.currentLang = this.defaultLang;
471 /**
472 * a list of translations per lang
473 */
474 this.translations = {};
475 /**
476 * an array of langs
477 */
478 this.langs = [];
479 /**
480 * An EventEmitter to listen to translation change events
481 * onTranslationChange.subscribe((params: TranslationChangeEvent) => {
482 * // do something
483 * });
484 */
485 this.onTranslationChange = new EventEmitter();
486 /**
487 * An EventEmitter to listen to lang change events
488 * onLangChange.subscribe((params: LangChangeEvent) => {
489 * // do something
490 * });
491 */
492 this.onLangChange = new EventEmitter();
493 /**
494 * An EventEmitter to listen to default lang change events
495 * onDefaultLangChange.subscribe((params: DefaultLangChangeEvent) => {
496 * // do something
497 * });
498 */
499 this.onDefaultLangChange = new EventEmitter();
500 }
501 return TranslateStore;
502}());
503if (false) {
504 /**
505 * The default lang to fallback when translations are missing on the current lang
506 * @type {?}
507 */
508 TranslateStore.prototype.defaultLang;
509 /**
510 * The lang currently used
511 * @type {?}
512 */
513 TranslateStore.prototype.currentLang;
514 /**
515 * a list of translations per lang
516 * @type {?}
517 */
518 TranslateStore.prototype.translations;
519 /**
520 * an array of langs
521 * @type {?}
522 */
523 TranslateStore.prototype.langs;
524 /**
525 * An EventEmitter to listen to translation change events
526 * onTranslationChange.subscribe((params: TranslationChangeEvent) => {
527 * // do something
528 * });
529 * @type {?}
530 */
531 TranslateStore.prototype.onTranslationChange;
532 /**
533 * An EventEmitter to listen to lang change events
534 * onLangChange.subscribe((params: LangChangeEvent) => {
535 * // do something
536 * });
537 * @type {?}
538 */
539 TranslateStore.prototype.onLangChange;
540 /**
541 * An EventEmitter to listen to default lang change events
542 * onDefaultLangChange.subscribe((params: DefaultLangChangeEvent) => {
543 * // do something
544 * });
545 * @type {?}
546 */
547 TranslateStore.prototype.onDefaultLangChange;
548}
549
550/**
551 * @fileoverview added by tsickle
552 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
553 */
554/** @type {?} */
555var USE_STORE = new InjectionToken('USE_STORE');
556/** @type {?} */
557var USE_DEFAULT_LANG = new InjectionToken('USE_DEFAULT_LANG');
558/** @type {?} */
559var DEFAULT_LANGUAGE = new InjectionToken('DEFAULT_LANGUAGE');
560/** @type {?} */
561var USE_EXTEND = new InjectionToken('USE_EXTEND');
562/**
563 * @record
564 */
565function TranslationChangeEvent() { }
566if (false) {
567 /** @type {?} */
568 TranslationChangeEvent.prototype.translations;
569 /** @type {?} */
570 TranslationChangeEvent.prototype.lang;
571}
572/**
573 * @record
574 */
575function LangChangeEvent() { }
576if (false) {
577 /** @type {?} */
578 LangChangeEvent.prototype.lang;
579 /** @type {?} */
580 LangChangeEvent.prototype.translations;
581}
582/**
583 * @record
584 */
585function DefaultLangChangeEvent() { }
586if (false) {
587 /** @type {?} */
588 DefaultLangChangeEvent.prototype.lang;
589 /** @type {?} */
590 DefaultLangChangeEvent.prototype.translations;
591}
592var TranslateService = /** @class */ (function () {
593 /**
594 *
595 * @param store an instance of the store (that is supposed to be unique)
596 * @param currentLoader An instance of the loader currently used
597 * @param compiler An instance of the compiler currently used
598 * @param parser An instance of the parser currently used
599 * @param missingTranslationHandler A handler for missing translations.
600 * @param useDefaultLang whether we should use default language translation when current language translation is missing.
601 * @param isolate whether this service should use the store or not
602 * @param extend To make a child module extend (and use) translations from parent modules.
603 * @param defaultLanguage Set the default language using configuration
604 */
605 function TranslateService(store, currentLoader, compiler, parser, missingTranslationHandler, useDefaultLang, isolate, extend, defaultLanguage) {
606 if (useDefaultLang === void 0) { useDefaultLang = true; }
607 if (isolate === void 0) { isolate = false; }
608 if (extend === void 0) { extend = false; }
609 this.store = store;
610 this.currentLoader = currentLoader;
611 this.compiler = compiler;
612 this.parser = parser;
613 this.missingTranslationHandler = missingTranslationHandler;
614 this.useDefaultLang = useDefaultLang;
615 this.isolate = isolate;
616 this.extend = extend;
617 this.pending = false;
618 this._onTranslationChange = new EventEmitter();
619 this._onLangChange = new EventEmitter();
620 this._onDefaultLangChange = new EventEmitter();
621 this._langs = [];
622 this._translations = {};
623 this._translationRequests = {};
624 /** set the default language from configuration */
625 if (defaultLanguage) {
626 this.setDefaultLang(defaultLanguage);
627 }
628 }
629 Object.defineProperty(TranslateService.prototype, "onTranslationChange", {
630 /**
631 * An EventEmitter to listen to translation change events
632 * onTranslationChange.subscribe((params: TranslationChangeEvent) => {
633 * // do something
634 * });
635 */
636 get: /**
637 * An EventEmitter to listen to translation change events
638 * onTranslationChange.subscribe((params: TranslationChangeEvent) => {
639 * // do something
640 * });
641 * @return {?}
642 */
643 function () {
644 return this.isolate ? this._onTranslationChange : this.store.onTranslationChange;
645 },
646 enumerable: true,
647 configurable: true
648 });
649 Object.defineProperty(TranslateService.prototype, "onLangChange", {
650 /**
651 * An EventEmitter to listen to lang change events
652 * onLangChange.subscribe((params: LangChangeEvent) => {
653 * // do something
654 * });
655 */
656 get: /**
657 * An EventEmitter to listen to lang change events
658 * onLangChange.subscribe((params: LangChangeEvent) => {
659 * // do something
660 * });
661 * @return {?}
662 */
663 function () {
664 return this.isolate ? this._onLangChange : this.store.onLangChange;
665 },
666 enumerable: true,
667 configurable: true
668 });
669 Object.defineProperty(TranslateService.prototype, "onDefaultLangChange", {
670 /**
671 * An EventEmitter to listen to default lang change events
672 * onDefaultLangChange.subscribe((params: DefaultLangChangeEvent) => {
673 * // do something
674 * });
675 */
676 get: /**
677 * An EventEmitter to listen to default lang change events
678 * onDefaultLangChange.subscribe((params: DefaultLangChangeEvent) => {
679 * // do something
680 * });
681 * @return {?}
682 */
683 function () {
684 return this.isolate ? this._onDefaultLangChange : this.store.onDefaultLangChange;
685 },
686 enumerable: true,
687 configurable: true
688 });
689 Object.defineProperty(TranslateService.prototype, "defaultLang", {
690 /**
691 * The default lang to fallback when translations are missing on the current lang
692 */
693 get: /**
694 * The default lang to fallback when translations are missing on the current lang
695 * @return {?}
696 */
697 function () {
698 return this.isolate ? this._defaultLang : this.store.defaultLang;
699 },
700 set: /**
701 * @param {?} defaultLang
702 * @return {?}
703 */
704 function (defaultLang) {
705 if (this.isolate) {
706 this._defaultLang = defaultLang;
707 }
708 else {
709 this.store.defaultLang = defaultLang;
710 }
711 },
712 enumerable: true,
713 configurable: true
714 });
715 Object.defineProperty(TranslateService.prototype, "currentLang", {
716 /**
717 * The lang currently used
718 */
719 get: /**
720 * The lang currently used
721 * @return {?}
722 */
723 function () {
724 return this.isolate ? this._currentLang : this.store.currentLang;
725 },
726 set: /**
727 * @param {?} currentLang
728 * @return {?}
729 */
730 function (currentLang) {
731 if (this.isolate) {
732 this._currentLang = currentLang;
733 }
734 else {
735 this.store.currentLang = currentLang;
736 }
737 },
738 enumerable: true,
739 configurable: true
740 });
741 Object.defineProperty(TranslateService.prototype, "langs", {
742 /**
743 * an array of langs
744 */
745 get: /**
746 * an array of langs
747 * @return {?}
748 */
749 function () {
750 return this.isolate ? this._langs : this.store.langs;
751 },
752 set: /**
753 * @param {?} langs
754 * @return {?}
755 */
756 function (langs) {
757 if (this.isolate) {
758 this._langs = langs;
759 }
760 else {
761 this.store.langs = langs;
762 }
763 },
764 enumerable: true,
765 configurable: true
766 });
767 Object.defineProperty(TranslateService.prototype, "translations", {
768 /**
769 * a list of translations per lang
770 */
771 get: /**
772 * a list of translations per lang
773 * @return {?}
774 */
775 function () {
776 return this.isolate ? this._translations : this.store.translations;
777 },
778 set: /**
779 * @param {?} translations
780 * @return {?}
781 */
782 function (translations) {
783 if (this.isolate) {
784 this._translations = translations;
785 }
786 else {
787 this.store.translations = translations;
788 }
789 },
790 enumerable: true,
791 configurable: true
792 });
793 /**
794 * Sets the default language to use as a fallback
795 */
796 /**
797 * Sets the default language to use as a fallback
798 * @param {?} lang
799 * @return {?}
800 */
801 TranslateService.prototype.setDefaultLang = /**
802 * Sets the default language to use as a fallback
803 * @param {?} lang
804 * @return {?}
805 */
806 function (lang) {
807 var _this = this;
808 if (lang === this.defaultLang) {
809 return;
810 }
811 /** @type {?} */
812 var pending = this.retrieveTranslations(lang);
813 if (typeof pending !== "undefined") {
814 // on init set the defaultLang immediately
815 if (this.defaultLang == null) {
816 this.defaultLang = lang;
817 }
818 pending.pipe(take(1))
819 .subscribe((/**
820 * @param {?} res
821 * @return {?}
822 */
823 function (res) {
824 _this.changeDefaultLang(lang);
825 }));
826 }
827 else { // we already have this language
828 this.changeDefaultLang(lang);
829 }
830 };
831 /**
832 * Gets the default language used
833 */
834 /**
835 * Gets the default language used
836 * @return {?}
837 */
838 TranslateService.prototype.getDefaultLang = /**
839 * Gets the default language used
840 * @return {?}
841 */
842 function () {
843 return this.defaultLang;
844 };
845 /**
846 * Changes the lang currently used
847 */
848 /**
849 * Changes the lang currently used
850 * @param {?} lang
851 * @return {?}
852 */
853 TranslateService.prototype.use = /**
854 * Changes the lang currently used
855 * @param {?} lang
856 * @return {?}
857 */
858 function (lang) {
859 var _this = this;
860 // don't change the language if the language given is already selected
861 if (lang === this.currentLang) {
862 return of(this.translations[lang]);
863 }
864 /** @type {?} */
865 var pending = this.retrieveTranslations(lang);
866 if (typeof pending !== "undefined") {
867 // on init set the currentLang immediately
868 if (!this.currentLang) {
869 this.currentLang = lang;
870 }
871 pending.pipe(take(1))
872 .subscribe((/**
873 * @param {?} res
874 * @return {?}
875 */
876 function (res) {
877 _this.changeLang(lang);
878 }));
879 return pending;
880 }
881 else { // we have this language, return an Observable
882 this.changeLang(lang);
883 return of(this.translations[lang]);
884 }
885 };
886 /**
887 * Retrieves the given translations
888 */
889 /**
890 * Retrieves the given translations
891 * @private
892 * @param {?} lang
893 * @return {?}
894 */
895 TranslateService.prototype.retrieveTranslations = /**
896 * Retrieves the given translations
897 * @private
898 * @param {?} lang
899 * @return {?}
900 */
901 function (lang) {
902 /** @type {?} */
903 var pending;
904 // if this language is unavailable or extend is true, ask for it
905 if (typeof this.translations[lang] === "undefined" || this.extend) {
906 this._translationRequests[lang] = this._translationRequests[lang] || this.getTranslation(lang);
907 pending = this._translationRequests[lang];
908 }
909 return pending;
910 };
911 /**
912 * Gets an object of translations for a given language with the current loader
913 * and passes it through the compiler
914 */
915 /**
916 * Gets an object of translations for a given language with the current loader
917 * and passes it through the compiler
918 * @param {?} lang
919 * @return {?}
920 */
921 TranslateService.prototype.getTranslation = /**
922 * Gets an object of translations for a given language with the current loader
923 * and passes it through the compiler
924 * @param {?} lang
925 * @return {?}
926 */
927 function (lang) {
928 var _this = this;
929 this.pending = true;
930 /** @type {?} */
931 var loadingTranslations = this.currentLoader.getTranslation(lang).pipe(shareReplay(1), take(1));
932 this.loadingTranslations = loadingTranslations.pipe(map((/**
933 * @param {?} res
934 * @return {?}
935 */
936 function (res) { return _this.compiler.compileTranslations(res, lang); })), shareReplay(1), take(1));
937 this.loadingTranslations
938 .subscribe({
939 next: (/**
940 * @param {?} res
941 * @return {?}
942 */
943 function (res) {
944 _this.translations[lang] = _this.extend && _this.translations[lang] ? __assign(__assign({}, res), _this.translations[lang]) : res;
945 _this.updateLangs();
946 _this.pending = false;
947 }),
948 error: (/**
949 * @param {?} err
950 * @return {?}
951 */
952 function (err) {
953 _this.pending = false;
954 })
955 });
956 return loadingTranslations;
957 };
958 /**
959 * Manually sets an object of translations for a given language
960 * after passing it through the compiler
961 */
962 /**
963 * Manually sets an object of translations for a given language
964 * after passing it through the compiler
965 * @param {?} lang
966 * @param {?} translations
967 * @param {?=} shouldMerge
968 * @return {?}
969 */
970 TranslateService.prototype.setTranslation = /**
971 * Manually sets an object of translations for a given language
972 * after passing it through the compiler
973 * @param {?} lang
974 * @param {?} translations
975 * @param {?=} shouldMerge
976 * @return {?}
977 */
978 function (lang, translations, shouldMerge) {
979 if (shouldMerge === void 0) { shouldMerge = false; }
980 translations = this.compiler.compileTranslations(translations, lang);
981 if ((shouldMerge || this.extend) && this.translations[lang]) {
982 this.translations[lang] = mergeDeep(this.translations[lang], translations);
983 }
984 else {
985 this.translations[lang] = translations;
986 }
987 this.updateLangs();
988 this.onTranslationChange.emit({ lang: lang, translations: this.translations[lang] });
989 };
990 /**
991 * Returns an array of currently available langs
992 */
993 /**
994 * Returns an array of currently available langs
995 * @return {?}
996 */
997 TranslateService.prototype.getLangs = /**
998 * Returns an array of currently available langs
999 * @return {?}
1000 */
1001 function () {
1002 return this.langs;
1003 };
1004 /**
1005 * Add available langs
1006 */
1007 /**
1008 * Add available langs
1009 * @param {?} langs
1010 * @return {?}
1011 */
1012 TranslateService.prototype.addLangs = /**
1013 * Add available langs
1014 * @param {?} langs
1015 * @return {?}
1016 */
1017 function (langs) {
1018 var _this = this;
1019 langs.forEach((/**
1020 * @param {?} lang
1021 * @return {?}
1022 */
1023 function (lang) {
1024 if (_this.langs.indexOf(lang) === -1) {
1025 _this.langs.push(lang);
1026 }
1027 }));
1028 };
1029 /**
1030 * Update the list of available langs
1031 */
1032 /**
1033 * Update the list of available langs
1034 * @private
1035 * @return {?}
1036 */
1037 TranslateService.prototype.updateLangs = /**
1038 * Update the list of available langs
1039 * @private
1040 * @return {?}
1041 */
1042 function () {
1043 this.addLangs(Object.keys(this.translations));
1044 };
1045 /**
1046 * Returns the parsed result of the translations
1047 */
1048 /**
1049 * Returns the parsed result of the translations
1050 * @param {?} translations
1051 * @param {?} key
1052 * @param {?=} interpolateParams
1053 * @return {?}
1054 */
1055 TranslateService.prototype.getParsedResult = /**
1056 * Returns the parsed result of the translations
1057 * @param {?} translations
1058 * @param {?} key
1059 * @param {?=} interpolateParams
1060 * @return {?}
1061 */
1062 function (translations, key, interpolateParams) {
1063 var e_1, _a;
1064 /** @type {?} */
1065 var res;
1066 if (key instanceof Array) {
1067 /** @type {?} */
1068 var result_1 = {};
1069 /** @type {?} */
1070 var observables = false;
1071 try {
1072 for (var key_1 = __values(key), key_1_1 = key_1.next(); !key_1_1.done; key_1_1 = key_1.next()) {
1073 var k = key_1_1.value;
1074 result_1[k] = this.getParsedResult(translations, k, interpolateParams);
1075 if (isObservable(result_1[k])) {
1076 observables = true;
1077 }
1078 }
1079 }
1080 catch (e_1_1) { e_1 = { error: e_1_1 }; }
1081 finally {
1082 try {
1083 if (key_1_1 && !key_1_1.done && (_a = key_1.return)) _a.call(key_1);
1084 }
1085 finally { if (e_1) throw e_1.error; }
1086 }
1087 if (observables) {
1088 /** @type {?} */
1089 var sources = key.map((/**
1090 * @param {?} k
1091 * @return {?}
1092 */
1093 function (k) { return isObservable(result_1[k]) ? result_1[k] : of((/** @type {?} */ (result_1[k]))); }));
1094 return forkJoin(sources).pipe(map((/**
1095 * @param {?} arr
1096 * @return {?}
1097 */
1098 function (arr) {
1099 /** @type {?} */
1100 var obj = {};
1101 arr.forEach((/**
1102 * @param {?} value
1103 * @param {?} index
1104 * @return {?}
1105 */
1106 function (value, index) {
1107 obj[key[index]] = value;
1108 }));
1109 return obj;
1110 })));
1111 }
1112 return result_1;
1113 }
1114 if (translations) {
1115 res = this.parser.interpolate(this.parser.getValue(translations, key), interpolateParams);
1116 }
1117 if (typeof res === "undefined" && this.defaultLang != null && this.defaultLang !== this.currentLang && this.useDefaultLang) {
1118 res = this.parser.interpolate(this.parser.getValue(this.translations[this.defaultLang], key), interpolateParams);
1119 }
1120 if (typeof res === "undefined") {
1121 /** @type {?} */
1122 var params = { key: key, translateService: this };
1123 if (typeof interpolateParams !== 'undefined') {
1124 params.interpolateParams = interpolateParams;
1125 }
1126 res = this.missingTranslationHandler.handle(params);
1127 }
1128 return typeof res !== "undefined" ? res : key;
1129 };
1130 /**
1131 * Gets the translated value of a key (or an array of keys)
1132 * @returns the translated key, or an object of translated keys
1133 */
1134 /**
1135 * Gets the translated value of a key (or an array of keys)
1136 * @param {?} key
1137 * @param {?=} interpolateParams
1138 * @return {?} the translated key, or an object of translated keys
1139 */
1140 TranslateService.prototype.get = /**
1141 * Gets the translated value of a key (or an array of keys)
1142 * @param {?} key
1143 * @param {?=} interpolateParams
1144 * @return {?} the translated key, or an object of translated keys
1145 */
1146 function (key, interpolateParams) {
1147 var _this = this;
1148 if (!isDefined(key) || !key.length) {
1149 throw new Error("Parameter \"key\" required");
1150 }
1151 // check if we are loading a new translation to use
1152 if (this.pending) {
1153 return this.loadingTranslations.pipe(concatMap((/**
1154 * @param {?} res
1155 * @return {?}
1156 */
1157 function (res) {
1158 res = _this.getParsedResult(res, key, interpolateParams);
1159 return isObservable(res) ? res : of(res);
1160 })));
1161 }
1162 else {
1163 /** @type {?} */
1164 var res = this.getParsedResult(this.translations[this.currentLang], key, interpolateParams);
1165 return isObservable(res) ? res : of(res);
1166 }
1167 };
1168 /**
1169 * Returns a stream of translated values of a key (or an array of keys) which updates
1170 * whenever the translation changes.
1171 * @returns A stream of the translated key, or an object of translated keys
1172 */
1173 /**
1174 * Returns a stream of translated values of a key (or an array of keys) which updates
1175 * whenever the translation changes.
1176 * @param {?} key
1177 * @param {?=} interpolateParams
1178 * @return {?} A stream of the translated key, or an object of translated keys
1179 */
1180 TranslateService.prototype.getStreamOnTranslationChange = /**
1181 * Returns a stream of translated values of a key (or an array of keys) which updates
1182 * whenever the translation changes.
1183 * @param {?} key
1184 * @param {?=} interpolateParams
1185 * @return {?} A stream of the translated key, or an object of translated keys
1186 */
1187 function (key, interpolateParams) {
1188 var _this = this;
1189 if (!isDefined(key) || !key.length) {
1190 throw new Error("Parameter \"key\" required");
1191 }
1192 return concat(defer((/**
1193 * @return {?}
1194 */
1195 function () { return _this.get(key, interpolateParams); })), this.onTranslationChange.pipe(switchMap((/**
1196 * @param {?} event
1197 * @return {?}
1198 */
1199 function (event) {
1200 /** @type {?} */
1201 var res = _this.getParsedResult(event.translations, key, interpolateParams);
1202 if (typeof res.subscribe === 'function') {
1203 return res;
1204 }
1205 else {
1206 return of(res);
1207 }
1208 }))));
1209 };
1210 /**
1211 * Returns a stream of translated values of a key (or an array of keys) which updates
1212 * whenever the language changes.
1213 * @returns A stream of the translated key, or an object of translated keys
1214 */
1215 /**
1216 * Returns a stream of translated values of a key (or an array of keys) which updates
1217 * whenever the language changes.
1218 * @param {?} key
1219 * @param {?=} interpolateParams
1220 * @return {?} A stream of the translated key, or an object of translated keys
1221 */
1222 TranslateService.prototype.stream = /**
1223 * Returns a stream of translated values of a key (or an array of keys) which updates
1224 * whenever the language changes.
1225 * @param {?} key
1226 * @param {?=} interpolateParams
1227 * @return {?} A stream of the translated key, or an object of translated keys
1228 */
1229 function (key, interpolateParams) {
1230 var _this = this;
1231 if (!isDefined(key) || !key.length) {
1232 throw new Error("Parameter \"key\" required");
1233 }
1234 return concat(defer((/**
1235 * @return {?}
1236 */
1237 function () { return _this.get(key, interpolateParams); })), this.onLangChange.pipe(switchMap((/**
1238 * @param {?} event
1239 * @return {?}
1240 */
1241 function (event) {
1242 /** @type {?} */
1243 var res = _this.getParsedResult(event.translations, key, interpolateParams);
1244 return isObservable(res) ? res : of(res);
1245 }))));
1246 };
1247 /**
1248 * Returns a translation instantly from the internal state of loaded translation.
1249 * All rules regarding the current language, the preferred language of even fallback languages will be used except any promise handling.
1250 */
1251 /**
1252 * Returns a translation instantly from the internal state of loaded translation.
1253 * All rules regarding the current language, the preferred language of even fallback languages will be used except any promise handling.
1254 * @param {?} key
1255 * @param {?=} interpolateParams
1256 * @return {?}
1257 */
1258 TranslateService.prototype.instant = /**
1259 * Returns a translation instantly from the internal state of loaded translation.
1260 * All rules regarding the current language, the preferred language of even fallback languages will be used except any promise handling.
1261 * @param {?} key
1262 * @param {?=} interpolateParams
1263 * @return {?}
1264 */
1265 function (key, interpolateParams) {
1266 if (!isDefined(key) || !key.length) {
1267 throw new Error("Parameter \"key\" required");
1268 }
1269 /** @type {?} */
1270 var res = this.getParsedResult(this.translations[this.currentLang], key, interpolateParams);
1271 if (isObservable(res)) {
1272 if (key instanceof Array) {
1273 /** @type {?} */
1274 var obj_1 = {};
1275 key.forEach((/**
1276 * @param {?} value
1277 * @param {?} index
1278 * @return {?}
1279 */
1280 function (value, index) {
1281 obj_1[key[index]] = key[index];
1282 }));
1283 return obj_1;
1284 }
1285 return key;
1286 }
1287 else {
1288 return res;
1289 }
1290 };
1291 /**
1292 * Sets the translated value of a key, after compiling it
1293 */
1294 /**
1295 * Sets the translated value of a key, after compiling it
1296 * @param {?} key
1297 * @param {?} value
1298 * @param {?=} lang
1299 * @return {?}
1300 */
1301 TranslateService.prototype.set = /**
1302 * Sets the translated value of a key, after compiling it
1303 * @param {?} key
1304 * @param {?} value
1305 * @param {?=} lang
1306 * @return {?}
1307 */
1308 function (key, value, lang) {
1309 if (lang === void 0) { lang = this.currentLang; }
1310 this.translations[lang][key] = this.compiler.compile(value, lang);
1311 this.updateLangs();
1312 this.onTranslationChange.emit({ lang: lang, translations: this.translations[lang] });
1313 };
1314 /**
1315 * Changes the current lang
1316 */
1317 /**
1318 * Changes the current lang
1319 * @private
1320 * @param {?} lang
1321 * @return {?}
1322 */
1323 TranslateService.prototype.changeLang = /**
1324 * Changes the current lang
1325 * @private
1326 * @param {?} lang
1327 * @return {?}
1328 */
1329 function (lang) {
1330 this.currentLang = lang;
1331 this.onLangChange.emit({ lang: lang, translations: this.translations[lang] });
1332 // if there is no default lang, use the one that we just set
1333 if (this.defaultLang == null) {
1334 this.changeDefaultLang(lang);
1335 }
1336 };
1337 /**
1338 * Changes the default lang
1339 */
1340 /**
1341 * Changes the default lang
1342 * @private
1343 * @param {?} lang
1344 * @return {?}
1345 */
1346 TranslateService.prototype.changeDefaultLang = /**
1347 * Changes the default lang
1348 * @private
1349 * @param {?} lang
1350 * @return {?}
1351 */
1352 function (lang) {
1353 this.defaultLang = lang;
1354 this.onDefaultLangChange.emit({ lang: lang, translations: this.translations[lang] });
1355 };
1356 /**
1357 * Allows to reload the lang file from the file
1358 */
1359 /**
1360 * Allows to reload the lang file from the file
1361 * @param {?} lang
1362 * @return {?}
1363 */
1364 TranslateService.prototype.reloadLang = /**
1365 * Allows to reload the lang file from the file
1366 * @param {?} lang
1367 * @return {?}
1368 */
1369 function (lang) {
1370 this.resetLang(lang);
1371 return this.getTranslation(lang);
1372 };
1373 /**
1374 * Deletes inner translation
1375 */
1376 /**
1377 * Deletes inner translation
1378 * @param {?} lang
1379 * @return {?}
1380 */
1381 TranslateService.prototype.resetLang = /**
1382 * Deletes inner translation
1383 * @param {?} lang
1384 * @return {?}
1385 */
1386 function (lang) {
1387 this._translationRequests[lang] = undefined;
1388 this.translations[lang] = undefined;
1389 };
1390 /**
1391 * Returns the language code name from the browser, e.g. "de"
1392 */
1393 /**
1394 * Returns the language code name from the browser, e.g. "de"
1395 * @return {?}
1396 */
1397 TranslateService.prototype.getBrowserLang = /**
1398 * Returns the language code name from the browser, e.g. "de"
1399 * @return {?}
1400 */
1401 function () {
1402 if (typeof window === 'undefined' || typeof window.navigator === 'undefined') {
1403 return undefined;
1404 }
1405 /** @type {?} */
1406 var browserLang = window.navigator.languages ? window.navigator.languages[0] : null;
1407 browserLang = browserLang || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage;
1408 if (typeof browserLang === 'undefined') {
1409 return undefined;
1410 }
1411 if (browserLang.indexOf('-') !== -1) {
1412 browserLang = browserLang.split('-')[0];
1413 }
1414 if (browserLang.indexOf('_') !== -1) {
1415 browserLang = browserLang.split('_')[0];
1416 }
1417 return browserLang;
1418 };
1419 /**
1420 * Returns the culture language code name from the browser, e.g. "de-DE"
1421 */
1422 /**
1423 * Returns the culture language code name from the browser, e.g. "de-DE"
1424 * @return {?}
1425 */
1426 TranslateService.prototype.getBrowserCultureLang = /**
1427 * Returns the culture language code name from the browser, e.g. "de-DE"
1428 * @return {?}
1429 */
1430 function () {
1431 if (typeof window === 'undefined' || typeof window.navigator === 'undefined') {
1432 return undefined;
1433 }
1434 /** @type {?} */
1435 var browserCultureLang = window.navigator.languages ? window.navigator.languages[0] : null;
1436 browserCultureLang = browserCultureLang || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage;
1437 return browserCultureLang;
1438 };
1439 TranslateService.decorators = [
1440 { type: Injectable }
1441 ];
1442 /** @nocollapse */
1443 TranslateService.ctorParameters = function () { return [
1444 { type: TranslateStore },
1445 { type: TranslateLoader },
1446 { type: TranslateCompiler },
1447 { type: TranslateParser },
1448 { type: MissingTranslationHandler },
1449 { type: Boolean, decorators: [{ type: Inject, args: [USE_DEFAULT_LANG,] }] },
1450 { type: Boolean, decorators: [{ type: Inject, args: [USE_STORE,] }] },
1451 { type: Boolean, decorators: [{ type: Inject, args: [USE_EXTEND,] }] },
1452 { type: String, decorators: [{ type: Inject, args: [DEFAULT_LANGUAGE,] }] }
1453 ]; };
1454 return TranslateService;
1455}());
1456if (false) {
1457 /**
1458 * @type {?}
1459 * @private
1460 */
1461 TranslateService.prototype.loadingTranslations;
1462 /**
1463 * @type {?}
1464 * @private
1465 */
1466 TranslateService.prototype.pending;
1467 /**
1468 * @type {?}
1469 * @private
1470 */
1471 TranslateService.prototype._onTranslationChange;
1472 /**
1473 * @type {?}
1474 * @private
1475 */
1476 TranslateService.prototype._onLangChange;
1477 /**
1478 * @type {?}
1479 * @private
1480 */
1481 TranslateService.prototype._onDefaultLangChange;
1482 /**
1483 * @type {?}
1484 * @private
1485 */
1486 TranslateService.prototype._defaultLang;
1487 /**
1488 * @type {?}
1489 * @private
1490 */
1491 TranslateService.prototype._currentLang;
1492 /**
1493 * @type {?}
1494 * @private
1495 */
1496 TranslateService.prototype._langs;
1497 /**
1498 * @type {?}
1499 * @private
1500 */
1501 TranslateService.prototype._translations;
1502 /**
1503 * @type {?}
1504 * @private
1505 */
1506 TranslateService.prototype._translationRequests;
1507 /** @type {?} */
1508 TranslateService.prototype.store;
1509 /** @type {?} */
1510 TranslateService.prototype.currentLoader;
1511 /** @type {?} */
1512 TranslateService.prototype.compiler;
1513 /** @type {?} */
1514 TranslateService.prototype.parser;
1515 /** @type {?} */
1516 TranslateService.prototype.missingTranslationHandler;
1517 /**
1518 * @type {?}
1519 * @private
1520 */
1521 TranslateService.prototype.useDefaultLang;
1522 /**
1523 * @type {?}
1524 * @private
1525 */
1526 TranslateService.prototype.isolate;
1527 /**
1528 * @type {?}
1529 * @private
1530 */
1531 TranslateService.prototype.extend;
1532}
1533
1534/**
1535 * @fileoverview added by tsickle
1536 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1537 */
1538var TranslateDirective = /** @class */ (function () {
1539 function TranslateDirective(translateService, element, _ref) {
1540 var _this = this;
1541 this.translateService = translateService;
1542 this.element = element;
1543 this._ref = _ref;
1544 // subscribe to onTranslationChange event, in case the translations of the current lang change
1545 if (!this.onTranslationChangeSub) {
1546 this.onTranslationChangeSub = this.translateService.onTranslationChange.subscribe((/**
1547 * @param {?} event
1548 * @return {?}
1549 */
1550 function (event) {
1551 if (event.lang === _this.translateService.currentLang) {
1552 _this.checkNodes(true, event.translations);
1553 }
1554 }));
1555 }
1556 // subscribe to onLangChange event, in case the language changes
1557 if (!this.onLangChangeSub) {
1558 this.onLangChangeSub = this.translateService.onLangChange.subscribe((/**
1559 * @param {?} event
1560 * @return {?}
1561 */
1562 function (event) {
1563 _this.checkNodes(true, event.translations);
1564 }));
1565 }
1566 // subscribe to onDefaultLangChange event, in case the default language changes
1567 if (!this.onDefaultLangChangeSub) {
1568 this.onDefaultLangChangeSub = this.translateService.onDefaultLangChange.subscribe((/**
1569 * @param {?} event
1570 * @return {?}
1571 */
1572 function (event) {
1573 _this.checkNodes(true);
1574 }));
1575 }
1576 }
1577 Object.defineProperty(TranslateDirective.prototype, "translate", {
1578 set: /**
1579 * @param {?} key
1580 * @return {?}
1581 */
1582 function (key) {
1583 if (key) {
1584 this.key = key;
1585 this.checkNodes();
1586 }
1587 },
1588 enumerable: true,
1589 configurable: true
1590 });
1591 Object.defineProperty(TranslateDirective.prototype, "translateParams", {
1592 set: /**
1593 * @param {?} params
1594 * @return {?}
1595 */
1596 function (params) {
1597 if (!equals(this.currentParams, params)) {
1598 this.currentParams = params;
1599 this.checkNodes(true);
1600 }
1601 },
1602 enumerable: true,
1603 configurable: true
1604 });
1605 /**
1606 * @return {?}
1607 */
1608 TranslateDirective.prototype.ngAfterViewChecked = /**
1609 * @return {?}
1610 */
1611 function () {
1612 this.checkNodes();
1613 };
1614 /**
1615 * @param {?=} forceUpdate
1616 * @param {?=} translations
1617 * @return {?}
1618 */
1619 TranslateDirective.prototype.checkNodes = /**
1620 * @param {?=} forceUpdate
1621 * @param {?=} translations
1622 * @return {?}
1623 */
1624 function (forceUpdate, translations) {
1625 if (forceUpdate === void 0) { forceUpdate = false; }
1626 /** @type {?} */
1627 var nodes = this.element.nativeElement.childNodes;
1628 // if the element is empty
1629 if (!nodes.length) {
1630 // we add the key as content
1631 this.setContent(this.element.nativeElement, this.key);
1632 nodes = this.element.nativeElement.childNodes;
1633 }
1634 for (var i = 0; i < nodes.length; ++i) {
1635 /** @type {?} */
1636 var node = nodes[i];
1637 if (node.nodeType === 3) { // node type 3 is a text node
1638 // node type 3 is a text node
1639 /** @type {?} */
1640 var key = void 0;
1641 if (forceUpdate) {
1642 node.lastKey = null;
1643 }
1644 if (isDefined(node.lookupKey)) {
1645 key = node.lookupKey;
1646 }
1647 else if (this.key) {
1648 key = this.key;
1649 }
1650 else {
1651 /** @type {?} */
1652 var content = this.getContent(node);
1653 /** @type {?} */
1654 var trimmedContent = content.trim();
1655 if (trimmedContent.length) {
1656 node.lookupKey = trimmedContent;
1657 // we want to use the content as a key, not the translation value
1658 if (content !== node.currentValue) {
1659 key = trimmedContent;
1660 // the content was changed from the user, we'll use it as a reference if needed
1661 node.originalContent = content || node.originalContent;
1662 }
1663 else if (node.originalContent) { // the content seems ok, but the lang has changed
1664 // the current content is the translation, not the key, use the last real content as key
1665 key = node.originalContent.trim();
1666 }
1667 else if (content !== node.currentValue) {
1668 // we want to use the content as a key, not the translation value
1669 key = trimmedContent;
1670 // the content was changed from the user, we'll use it as a reference if needed
1671 node.originalContent = content || node.originalContent;
1672 }
1673 }
1674 }
1675 this.updateValue(key, node, translations);
1676 }
1677 }
1678 };
1679 /**
1680 * @param {?} key
1681 * @param {?} node
1682 * @param {?} translations
1683 * @return {?}
1684 */
1685 TranslateDirective.prototype.updateValue = /**
1686 * @param {?} key
1687 * @param {?} node
1688 * @param {?} translations
1689 * @return {?}
1690 */
1691 function (key, node, translations) {
1692 var _this = this;
1693 if (key) {
1694 if (node.lastKey === key && this.lastParams === this.currentParams) {
1695 return;
1696 }
1697 this.lastParams = this.currentParams;
1698 /** @type {?} */
1699 var onTranslation = (/**
1700 * @param {?} res
1701 * @return {?}
1702 */
1703 function (res) {
1704 if (res !== key) {
1705 node.lastKey = key;
1706 }
1707 if (!node.originalContent) {
1708 node.originalContent = _this.getContent(node);
1709 }
1710 node.currentValue = isDefined(res) ? res : (node.originalContent || key);
1711 // we replace in the original content to preserve spaces that we might have trimmed
1712 _this.setContent(node, _this.key ? node.currentValue : node.originalContent.replace(key, node.currentValue));
1713 _this._ref.markForCheck();
1714 });
1715 if (isDefined(translations)) {
1716 /** @type {?} */
1717 var res = this.translateService.getParsedResult(translations, key, this.currentParams);
1718 if (isObservable(res)) {
1719 res.subscribe(onTranslation);
1720 }
1721 else {
1722 onTranslation(res);
1723 }
1724 }
1725 else {
1726 this.translateService.get(key, this.currentParams).subscribe(onTranslation);
1727 }
1728 }
1729 };
1730 /**
1731 * @param {?} node
1732 * @return {?}
1733 */
1734 TranslateDirective.prototype.getContent = /**
1735 * @param {?} node
1736 * @return {?}
1737 */
1738 function (node) {
1739 return isDefined(node.textContent) ? node.textContent : node.data;
1740 };
1741 /**
1742 * @param {?} node
1743 * @param {?} content
1744 * @return {?}
1745 */
1746 TranslateDirective.prototype.setContent = /**
1747 * @param {?} node
1748 * @param {?} content
1749 * @return {?}
1750 */
1751 function (node, content) {
1752 if (isDefined(node.textContent)) {
1753 node.textContent = content;
1754 }
1755 else {
1756 node.data = content;
1757 }
1758 };
1759 /**
1760 * @return {?}
1761 */
1762 TranslateDirective.prototype.ngOnDestroy = /**
1763 * @return {?}
1764 */
1765 function () {
1766 if (this.onLangChangeSub) {
1767 this.onLangChangeSub.unsubscribe();
1768 }
1769 if (this.onDefaultLangChangeSub) {
1770 this.onDefaultLangChangeSub.unsubscribe();
1771 }
1772 if (this.onTranslationChangeSub) {
1773 this.onTranslationChangeSub.unsubscribe();
1774 }
1775 };
1776 TranslateDirective.decorators = [
1777 { type: Directive, args: [{
1778 selector: '[translate],[ngx-translate]'
1779 },] }
1780 ];
1781 /** @nocollapse */
1782 TranslateDirective.ctorParameters = function () { return [
1783 { type: TranslateService },
1784 { type: ElementRef },
1785 { type: ChangeDetectorRef }
1786 ]; };
1787 TranslateDirective.propDecorators = {
1788 translate: [{ type: Input }],
1789 translateParams: [{ type: Input }]
1790 };
1791 return TranslateDirective;
1792}());
1793if (false) {
1794 /** @type {?} */
1795 TranslateDirective.prototype.key;
1796 /** @type {?} */
1797 TranslateDirective.prototype.lastParams;
1798 /** @type {?} */
1799 TranslateDirective.prototype.currentParams;
1800 /** @type {?} */
1801 TranslateDirective.prototype.onLangChangeSub;
1802 /** @type {?} */
1803 TranslateDirective.prototype.onDefaultLangChangeSub;
1804 /** @type {?} */
1805 TranslateDirective.prototype.onTranslationChangeSub;
1806 /**
1807 * @type {?}
1808 * @private
1809 */
1810 TranslateDirective.prototype.translateService;
1811 /**
1812 * @type {?}
1813 * @private
1814 */
1815 TranslateDirective.prototype.element;
1816 /**
1817 * @type {?}
1818 * @private
1819 */
1820 TranslateDirective.prototype._ref;
1821}
1822
1823/**
1824 * @fileoverview added by tsickle
1825 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
1826 */
1827var TranslatePipe = /** @class */ (function () {
1828 function TranslatePipe(translate, _ref) {
1829 this.translate = translate;
1830 this._ref = _ref;
1831 this.value = '';
1832 }
1833 /**
1834 * @param {?} key
1835 * @param {?=} interpolateParams
1836 * @param {?=} translations
1837 * @return {?}
1838 */
1839 TranslatePipe.prototype.updateValue = /**
1840 * @param {?} key
1841 * @param {?=} interpolateParams
1842 * @param {?=} translations
1843 * @return {?}
1844 */
1845 function (key, interpolateParams, translations) {
1846 var _this = this;
1847 /** @type {?} */
1848 var onTranslation = (/**
1849 * @param {?} res
1850 * @return {?}
1851 */
1852 function (res) {
1853 _this.value = res !== undefined ? res : key;
1854 _this.lastKey = key;
1855 _this._ref.markForCheck();
1856 });
1857 if (translations) {
1858 /** @type {?} */
1859 var res = this.translate.getParsedResult(translations, key, interpolateParams);
1860 if (isObservable(res.subscribe)) {
1861 res.subscribe(onTranslation);
1862 }
1863 else {
1864 onTranslation(res);
1865 }
1866 }
1867 this.translate.get(key, interpolateParams).subscribe(onTranslation);
1868 };
1869 /**
1870 * @param {?} query
1871 * @param {...?} args
1872 * @return {?}
1873 */
1874 TranslatePipe.prototype.transform = /**
1875 * @param {?} query
1876 * @param {...?} args
1877 * @return {?}
1878 */
1879 function (query) {
1880 var _this = this;
1881 var args = [];
1882 for (var _i = 1; _i < arguments.length; _i++) {
1883 args[_i - 1] = arguments[_i];
1884 }
1885 if (!query || !query.length) {
1886 return query;
1887 }
1888 // if we ask another time for the same key, return the last value
1889 if (equals(query, this.lastKey) && equals(args, this.lastParams)) {
1890 return this.value;
1891 }
1892 /** @type {?} */
1893 var interpolateParams;
1894 if (isDefined(args[0]) && args.length) {
1895 if (typeof args[0] === 'string' && args[0].length) {
1896 // we accept objects written in the template such as {n:1}, {'n':1}, {n:'v'}
1897 // which is why we might need to change it to real JSON objects such as {"n":1} or {"n":"v"}
1898 /** @type {?} */
1899 var validArgs = args[0]
1900 .replace(/(\')?([a-zA-Z0-9_]+)(\')?(\s)?:/g, '"$2":')
1901 .replace(/:(\s)?(\')(.*?)(\')/g, ':"$3"');
1902 try {
1903 interpolateParams = JSON.parse(validArgs);
1904 }
1905 catch (e) {
1906 throw new SyntaxError("Wrong parameter in TranslatePipe. Expected a valid Object, received: " + args[0]);
1907 }
1908 }
1909 else if (typeof args[0] === 'object' && !Array.isArray(args[0])) {
1910 interpolateParams = args[0];
1911 }
1912 }
1913 // store the query, in case it changes
1914 this.lastKey = query;
1915 // store the params, in case they change
1916 this.lastParams = args;
1917 // set the value
1918 this.updateValue(query, interpolateParams);
1919 // if there is a subscription to onLangChange, clean it
1920 this._dispose();
1921 // subscribe to onTranslationChange event, in case the translations change
1922 if (!this.onTranslationChange) {
1923 this.onTranslationChange = this.translate.onTranslationChange.subscribe((/**
1924 * @param {?} event
1925 * @return {?}
1926 */
1927 function (event) {
1928 if (_this.lastKey && event.lang === _this.translate.currentLang) {
1929 _this.lastKey = null;
1930 _this.updateValue(query, interpolateParams, event.translations);
1931 }
1932 }));
1933 }
1934 // subscribe to onLangChange event, in case the language changes
1935 if (!this.onLangChange) {
1936 this.onLangChange = this.translate.onLangChange.subscribe((/**
1937 * @param {?} event
1938 * @return {?}
1939 */
1940 function (event) {
1941 if (_this.lastKey) {
1942 _this.lastKey = null; // we want to make sure it doesn't return the same value until it's been updated
1943 _this.updateValue(query, interpolateParams, event.translations);
1944 }
1945 }));
1946 }
1947 // subscribe to onDefaultLangChange event, in case the default language changes
1948 if (!this.onDefaultLangChange) {
1949 this.onDefaultLangChange = this.translate.onDefaultLangChange.subscribe((/**
1950 * @return {?}
1951 */
1952 function () {
1953 if (_this.lastKey) {
1954 _this.lastKey = null; // we want to make sure it doesn't return the same value until it's been updated
1955 _this.updateValue(query, interpolateParams);
1956 }
1957 }));
1958 }
1959 return this.value;
1960 };
1961 /**
1962 * Clean any existing subscription to change events
1963 */
1964 /**
1965 * Clean any existing subscription to change events
1966 * @private
1967 * @return {?}
1968 */
1969 TranslatePipe.prototype._dispose = /**
1970 * Clean any existing subscription to change events
1971 * @private
1972 * @return {?}
1973 */
1974 function () {
1975 if (typeof this.onTranslationChange !== 'undefined') {
1976 this.onTranslationChange.unsubscribe();
1977 this.onTranslationChange = undefined;
1978 }
1979 if (typeof this.onLangChange !== 'undefined') {
1980 this.onLangChange.unsubscribe();
1981 this.onLangChange = undefined;
1982 }
1983 if (typeof this.onDefaultLangChange !== 'undefined') {
1984 this.onDefaultLangChange.unsubscribe();
1985 this.onDefaultLangChange = undefined;
1986 }
1987 };
1988 /**
1989 * @return {?}
1990 */
1991 TranslatePipe.prototype.ngOnDestroy = /**
1992 * @return {?}
1993 */
1994 function () {
1995 this._dispose();
1996 };
1997 TranslatePipe.decorators = [
1998 { type: Injectable },
1999 { type: Pipe, args: [{
2000 name: 'translate',
2001 pure: false // required to update the value when the promise is resolved
2002 },] }
2003 ];
2004 /** @nocollapse */
2005 TranslatePipe.ctorParameters = function () { return [
2006 { type: TranslateService },
2007 { type: ChangeDetectorRef }
2008 ]; };
2009 return TranslatePipe;
2010}());
2011if (false) {
2012 /** @type {?} */
2013 TranslatePipe.prototype.value;
2014 /** @type {?} */
2015 TranslatePipe.prototype.lastKey;
2016 /** @type {?} */
2017 TranslatePipe.prototype.lastParams;
2018 /** @type {?} */
2019 TranslatePipe.prototype.onTranslationChange;
2020 /** @type {?} */
2021 TranslatePipe.prototype.onLangChange;
2022 /** @type {?} */
2023 TranslatePipe.prototype.onDefaultLangChange;
2024 /**
2025 * @type {?}
2026 * @private
2027 */
2028 TranslatePipe.prototype.translate;
2029 /**
2030 * @type {?}
2031 * @private
2032 */
2033 TranslatePipe.prototype._ref;
2034}
2035
2036/**
2037 * @fileoverview added by tsickle
2038 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
2039 */
2040/**
2041 * @record
2042 */
2043function TranslateModuleConfig() { }
2044if (false) {
2045 /** @type {?|undefined} */
2046 TranslateModuleConfig.prototype.loader;
2047 /** @type {?|undefined} */
2048 TranslateModuleConfig.prototype.compiler;
2049 /** @type {?|undefined} */
2050 TranslateModuleConfig.prototype.parser;
2051 /** @type {?|undefined} */
2052 TranslateModuleConfig.prototype.missingTranslationHandler;
2053 /** @type {?|undefined} */
2054 TranslateModuleConfig.prototype.isolate;
2055 /** @type {?|undefined} */
2056 TranslateModuleConfig.prototype.extend;
2057 /** @type {?|undefined} */
2058 TranslateModuleConfig.prototype.useDefaultLang;
2059 /** @type {?|undefined} */
2060 TranslateModuleConfig.prototype.defaultLanguage;
2061}
2062var TranslateModule = /** @class */ (function () {
2063 function TranslateModule() {
2064 }
2065 /**
2066 * Use this method in your root module to provide the TranslateService
2067 */
2068 /**
2069 * Use this method in your root module to provide the TranslateService
2070 * @param {?=} config
2071 * @return {?}
2072 */
2073 TranslateModule.forRoot = /**
2074 * Use this method in your root module to provide the TranslateService
2075 * @param {?=} config
2076 * @return {?}
2077 */
2078 function (config) {
2079 if (config === void 0) { config = {}; }
2080 return {
2081 ngModule: TranslateModule,
2082 providers: [
2083 config.loader || { provide: TranslateLoader, useClass: TranslateFakeLoader },
2084 config.compiler || { provide: TranslateCompiler, useClass: TranslateFakeCompiler },
2085 config.parser || { provide: TranslateParser, useClass: TranslateDefaultParser },
2086 config.missingTranslationHandler || { provide: MissingTranslationHandler, useClass: FakeMissingTranslationHandler },
2087 TranslateStore,
2088 { provide: USE_STORE, useValue: config.isolate },
2089 { provide: USE_DEFAULT_LANG, useValue: config.useDefaultLang },
2090 { provide: USE_EXTEND, useValue: config.extend },
2091 { provide: DEFAULT_LANGUAGE, useValue: config.defaultLanguage },
2092 TranslateService
2093 ]
2094 };
2095 };
2096 /**
2097 * Use this method in your other (non root) modules to import the directive/pipe
2098 */
2099 /**
2100 * Use this method in your other (non root) modules to import the directive/pipe
2101 * @param {?=} config
2102 * @return {?}
2103 */
2104 TranslateModule.forChild = /**
2105 * Use this method in your other (non root) modules to import the directive/pipe
2106 * @param {?=} config
2107 * @return {?}
2108 */
2109 function (config) {
2110 if (config === void 0) { config = {}; }
2111 return {
2112 ngModule: TranslateModule,
2113 providers: [
2114 config.loader || { provide: TranslateLoader, useClass: TranslateFakeLoader },
2115 config.compiler || { provide: TranslateCompiler, useClass: TranslateFakeCompiler },
2116 config.parser || { provide: TranslateParser, useClass: TranslateDefaultParser },
2117 config.missingTranslationHandler || { provide: MissingTranslationHandler, useClass: FakeMissingTranslationHandler },
2118 { provide: USE_STORE, useValue: config.isolate },
2119 { provide: USE_DEFAULT_LANG, useValue: config.useDefaultLang },
2120 { provide: USE_EXTEND, useValue: config.extend },
2121 { provide: DEFAULT_LANGUAGE, useValue: config.defaultLanguage },
2122 TranslateService
2123 ]
2124 };
2125 };
2126 TranslateModule.decorators = [
2127 { type: NgModule, args: [{
2128 declarations: [
2129 TranslatePipe,
2130 TranslateDirective
2131 ],
2132 exports: [
2133 TranslatePipe,
2134 TranslateDirective
2135 ]
2136 },] }
2137 ];
2138 return TranslateModule;
2139}());
2140
2141/**
2142 * @fileoverview added by tsickle
2143 * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
2144 */
2145
2146export { DEFAULT_LANGUAGE, FakeMissingTranslationHandler, MissingTranslationHandler, TranslateCompiler, TranslateDefaultParser, TranslateDirective, TranslateFakeCompiler, TranslateFakeLoader, TranslateLoader, TranslateModule, TranslateParser, TranslatePipe, TranslateService, TranslateStore, USE_DEFAULT_LANG, USE_EXTEND, USE_STORE };
2147//# sourceMappingURL=ngx-translate-core.js.map