@ngx-translate/core
Version:
The internationalization (i18n) library for Angular
1,343 lines (1,331 loc) • 168 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('rxjs'), require('rxjs/operators')) :
typeof define === 'function' && define.amd ? define('@ngx-translate/core', ['exports', '@angular/core', 'rxjs', 'rxjs/operators'], factory) :
(factory((global['ngx-translate'] = global['ngx-translate'] || {}, global['ngx-translate'].core = {}),global.ng.core,global.rxjs,global.rxjs.operators));
}(this, (function (exports,core,rxjs,operators) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b)
if (b.hasOwnProperty(p))
d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
function __values(o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m)
return m.call(o);
return {
next: function () {
if (o && i >= o.length)
o = void 0;
return { value: o && o[i++], done: !o };
}
};
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @abstract
*/
var /**
* @abstract
*/ TranslateLoader = /** @class */ (function () {
function TranslateLoader() {
}
return TranslateLoader;
}());
/**
* This loader is just a placeholder that does nothing, in case you don't need a loader at all
*/
var TranslateFakeLoader = /** @class */ (function (_super) {
__extends(TranslateFakeLoader, _super);
function TranslateFakeLoader() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @param {?} lang
* @return {?}
*/
TranslateFakeLoader.prototype.getTranslation = /**
* @param {?} lang
* @return {?}
*/
function (lang) {
return rxjs.of({});
};
TranslateFakeLoader.decorators = [
{ type: core.Injectable }
];
return TranslateFakeLoader;
}(TranslateLoader));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @abstract
*/
var /**
* @abstract
*/ MissingTranslationHandler = /** @class */ (function () {
function MissingTranslationHandler() {
}
return MissingTranslationHandler;
}());
/**
* This handler is just a placeholder that does nothing, in case you don't need a missing translation handler at all
*/
var FakeMissingTranslationHandler = /** @class */ (function () {
function FakeMissingTranslationHandler() {
}
/**
* @param {?} params
* @return {?}
*/
FakeMissingTranslationHandler.prototype.handle = /**
* @param {?} params
* @return {?}
*/
function (params) {
return params.key;
};
FakeMissingTranslationHandler.decorators = [
{ type: core.Injectable }
];
return FakeMissingTranslationHandler;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @abstract
*/
var /**
* @abstract
*/ TranslateCompiler = /** @class */ (function () {
function TranslateCompiler() {
}
return TranslateCompiler;
}());
/**
* This compiler is just a placeholder that does nothing, in case you don't need a compiler at all
*/
var TranslateFakeCompiler = /** @class */ (function (_super) {
__extends(TranslateFakeCompiler, _super);
function TranslateFakeCompiler() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @param {?} value
* @param {?} lang
* @return {?}
*/
TranslateFakeCompiler.prototype.compile = /**
* @param {?} value
* @param {?} lang
* @return {?}
*/
function (value, lang) {
return value;
};
/**
* @param {?} translations
* @param {?} lang
* @return {?}
*/
TranslateFakeCompiler.prototype.compileTranslations = /**
* @param {?} translations
* @param {?} lang
* @return {?}
*/
function (translations, lang) {
return translations;
};
TranslateFakeCompiler.decorators = [
{ type: core.Injectable }
];
return TranslateFakeCompiler;
}(TranslateCompiler));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/* tslint:disable */
/**
* Determines if two objects or two values are equivalent.
*
* Two objects or values are considered equivalent if at least one of the following is true:
*
* * Both objects or values pass `===` comparison.
* * Both objects or values are of the same type and all of their properties are equal by
* comparing them with `equals`.
*
* @param {?} o1 Object or value to compare.
* @param {?} o2 Object or value to compare.
* @return {?} true if arguments are equal.
*/
function equals(o1, o2) {
if (o1 === o2)
return true;
if (o1 === null || o2 === null)
return false;
if (o1 !== o1 && o2 !== o2)
return true; // NaN === NaN
// NaN === NaN
/** @type {?} */
var t1 = typeof o1;
/** @type {?} */
var t2 = typeof o2;
/** @type {?} */
var length;
/** @type {?} */
var key;
/** @type {?} */
var keySet;
if (t1 == t2 && t1 == 'object') {
if (Array.isArray(o1)) {
if (!Array.isArray(o2))
return false;
if ((length = o1.length) == o2.length) {
for (key = 0; key < length; key++) {
if (!equals(o1[key], o2[key]))
return false;
}
return true;
}
}
else {
if (Array.isArray(o2)) {
return false;
}
keySet = Object.create(null);
for (key in o1) {
if (!equals(o1[key], o2[key])) {
return false;
}
keySet[key] = true;
}
for (key in o2) {
if (!(key in keySet) && typeof o2[key] !== 'undefined') {
return false;
}
}
return true;
}
}
return false;
}
/* tslint:enable */
/**
* @param {?} value
* @return {?}
*/
function isDefined(value) {
return typeof value !== 'undefined' && value !== null;
}
/**
* @param {?} item
* @return {?}
*/
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item));
}
/**
* @param {?} target
* @param {?} source
* @return {?}
*/
function mergeDeep(target, source) {
/** @type {?} */
var output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(function (key) {
var _a, _b;
if (isObject(source[key])) {
if (!(key in target)) {
Object.assign(output, (_a = {}, _a[key] = source[key], _a));
}
else {
output[key] = mergeDeep(target[key], source[key]);
}
}
else {
Object.assign(output, (_b = {}, _b[key] = source[key], _b));
}
});
}
return output;
}
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/**
* @abstract
*/
var /**
* @abstract
*/ TranslateParser = /** @class */ (function () {
function TranslateParser() {
}
return TranslateParser;
}());
var TranslateDefaultParser = /** @class */ (function (_super) {
__extends(TranslateDefaultParser, _super);
function TranslateDefaultParser() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.templateMatcher = /{{\s?([^{}\s]*)\s?}}/g;
return _this;
}
/**
* @param {?} expr
* @param {?=} params
* @return {?}
*/
TranslateDefaultParser.prototype.interpolate = /**
* @param {?} expr
* @param {?=} params
* @return {?}
*/
function (expr, params) {
/** @type {?} */
var result;
if (typeof expr === 'string') {
result = this.interpolateString(expr, params);
}
else if (typeof expr === 'function') {
result = this.interpolateFunction(expr, params);
}
else {
// this should not happen, but an unrelated TranslateService test depends on it
result = ( /** @type {?} */(expr));
}
return result;
};
/**
* @param {?} target
* @param {?} key
* @return {?}
*/
TranslateDefaultParser.prototype.getValue = /**
* @param {?} target
* @param {?} key
* @return {?}
*/
function (target, key) {
/** @type {?} */
var keys = key.split('.');
key = '';
do {
key += keys.shift();
if (isDefined(target) && isDefined(target[key]) && (typeof target[key] === 'object' || !keys.length)) {
target = target[key];
key = '';
}
else if (!keys.length) {
target = undefined;
}
else {
key += '.';
}
} while (keys.length);
return target;
};
/**
* @param {?} fn
* @param {?=} params
* @return {?}
*/
TranslateDefaultParser.prototype.interpolateFunction = /**
* @param {?} fn
* @param {?=} params
* @return {?}
*/
function (fn, params) {
return fn(params);
};
/**
* @param {?} expr
* @param {?=} params
* @return {?}
*/
TranslateDefaultParser.prototype.interpolateString = /**
* @param {?} expr
* @param {?=} params
* @return {?}
*/
function (expr, params) {
var _this = this;
if (!params) {
return expr;
}
return expr.replace(this.templateMatcher, function (substring, b) {
/** @type {?} */
var r = _this.getValue(params, b);
return isDefined(r) ? r : substring;
});
};
TranslateDefaultParser.decorators = [
{ type: core.Injectable }
];
return TranslateDefaultParser;
}(TranslateParser));
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var TranslateStore = /** @class */ (function () {
function TranslateStore() {
/**
* The lang currently used
*/
this.currentLang = this.defaultLang;
/**
* a list of translations per lang
*/
this.translations = {};
/**
* an array of langs
*/
this.langs = [];
/**
* An EventEmitter to listen to translation change events
* onTranslationChange.subscribe((params: TranslationChangeEvent) => {
* // do something
* });
*/
this.onTranslationChange = new core.EventEmitter();
/**
* An EventEmitter to listen to lang change events
* onLangChange.subscribe((params: LangChangeEvent) => {
* // do something
* });
*/
this.onLangChange = new core.EventEmitter();
/**
* An EventEmitter to listen to default lang change events
* onDefaultLangChange.subscribe((params: DefaultLangChangeEvent) => {
* // do something
* });
*/
this.onDefaultLangChange = new core.EventEmitter();
}
return TranslateStore;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
/** @type {?} */
var USE_STORE = new core.InjectionToken('USE_STORE');
/** @type {?} */
var USE_DEFAULT_LANG = new core.InjectionToken('USE_DEFAULT_LANG');
var TranslateService = /** @class */ (function () {
/**
*
* @param store an instance of the store (that is supposed to be unique)
* @param currentLoader An instance of the loader currently used
* @param compiler An instance of the compiler currently used
* @param parser An instance of the parser currently used
* @param missingTranslationHandler A handler for missing translations.
* @param isolate whether this service should use the store or not
* @param useDefaultLang whether we should use default language translation when current language translation is missing.
*/
function TranslateService(store, currentLoader, compiler, parser, missingTranslationHandler, useDefaultLang, isolate) {
if (useDefaultLang === void 0) {
useDefaultLang = true;
}
if (isolate === void 0) {
isolate = false;
}
this.store = store;
this.currentLoader = currentLoader;
this.compiler = compiler;
this.parser = parser;
this.missingTranslationHandler = missingTranslationHandler;
this.useDefaultLang = useDefaultLang;
this.isolate = isolate;
this.pending = false;
this._onTranslationChange = new core.EventEmitter();
this._onLangChange = new core.EventEmitter();
this._onDefaultLangChange = new core.EventEmitter();
this._langs = [];
this._translations = {};
this._translationRequests = {};
}
Object.defineProperty(TranslateService.prototype, "onTranslationChange", {
/**
* An EventEmitter to listen to translation change events
* onTranslationChange.subscribe((params: TranslationChangeEvent) => {
* // do something
* });
*/
get: /**
* An EventEmitter to listen to translation change events
* onTranslationChange.subscribe((params: TranslationChangeEvent) => {
* // do something
* });
* @return {?}
*/ function () {
return this.isolate ? this._onTranslationChange : this.store.onTranslationChange;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TranslateService.prototype, "onLangChange", {
/**
* An EventEmitter to listen to lang change events
* onLangChange.subscribe((params: LangChangeEvent) => {
* // do something
* });
*/
get: /**
* An EventEmitter to listen to lang change events
* onLangChange.subscribe((params: LangChangeEvent) => {
* // do something
* });
* @return {?}
*/ function () {
return this.isolate ? this._onLangChange : this.store.onLangChange;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TranslateService.prototype, "onDefaultLangChange", {
/**
* An EventEmitter to listen to default lang change events
* onDefaultLangChange.subscribe((params: DefaultLangChangeEvent) => {
* // do something
* });
*/
get: /**
* An EventEmitter to listen to default lang change events
* onDefaultLangChange.subscribe((params: DefaultLangChangeEvent) => {
* // do something
* });
* @return {?}
*/ function () {
return this.isolate ? this._onDefaultLangChange : this.store.onDefaultLangChange;
},
enumerable: true,
configurable: true
});
Object.defineProperty(TranslateService.prototype, "defaultLang", {
/**
* The default lang to fallback when translations are missing on the current lang
*/
get: /**
* The default lang to fallback when translations are missing on the current lang
* @return {?}
*/ function () {
return this.isolate ? this._defaultLang : this.store.defaultLang;
},
set: /**
* @param {?} defaultLang
* @return {?}
*/ function (defaultLang) {
if (this.isolate) {
this._defaultLang = defaultLang;
}
else {
this.store.defaultLang = defaultLang;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(TranslateService.prototype, "currentLang", {
/**
* The lang currently used
*/
get: /**
* The lang currently used
* @return {?}
*/ function () {
return this.isolate ? this._currentLang : this.store.currentLang;
},
set: /**
* @param {?} currentLang
* @return {?}
*/ function (currentLang) {
if (this.isolate) {
this._currentLang = currentLang;
}
else {
this.store.currentLang = currentLang;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(TranslateService.prototype, "langs", {
/**
* an array of langs
*/
get: /**
* an array of langs
* @return {?}
*/ function () {
return this.isolate ? this._langs : this.store.langs;
},
set: /**
* @param {?} langs
* @return {?}
*/ function (langs) {
if (this.isolate) {
this._langs = langs;
}
else {
this.store.langs = langs;
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(TranslateService.prototype, "translations", {
/**
* a list of translations per lang
*/
get: /**
* a list of translations per lang
* @return {?}
*/ function () {
return this.isolate ? this._translations : this.store.translations;
},
set: /**
* @param {?} translations
* @return {?}
*/ function (translations) {
if (this.isolate) {
this._translations = translations;
}
else {
this.store.translations = translations;
}
},
enumerable: true,
configurable: true
});
/**
* Sets the default language to use as a fallback
*/
/**
* Sets the default language to use as a fallback
* @param {?} lang
* @return {?}
*/
TranslateService.prototype.setDefaultLang = /**
* Sets the default language to use as a fallback
* @param {?} lang
* @return {?}
*/
function (lang) {
var _this = this;
if (lang === this.defaultLang) {
return;
}
/** @type {?} */
var pending = this.retrieveTranslations(lang);
if (typeof pending !== "undefined") {
// on init set the defaultLang immediately
if (!this.defaultLang) {
this.defaultLang = lang;
}
pending.pipe(operators.take(1))
.subscribe(function (res) {
_this.changeDefaultLang(lang);
});
}
else { // we already have this language
this.changeDefaultLang(lang);
}
};
/**
* Gets the default language used
*/
/**
* Gets the default language used
* @return {?}
*/
TranslateService.prototype.getDefaultLang = /**
* Gets the default language used
* @return {?}
*/
function () {
return this.defaultLang;
};
/**
* Changes the lang currently used
*/
/**
* Changes the lang currently used
* @param {?} lang
* @return {?}
*/
TranslateService.prototype.use = /**
* Changes the lang currently used
* @param {?} lang
* @return {?}
*/
function (lang) {
var _this = this;
// don't change the language if the language given is already selected
if (lang === this.currentLang) {
return rxjs.of(this.translations[lang]);
}
/** @type {?} */
var pending = this.retrieveTranslations(lang);
if (typeof pending !== "undefined") {
// on init set the currentLang immediately
if (!this.currentLang) {
this.currentLang = lang;
}
pending.pipe(operators.take(1))
.subscribe(function (res) {
_this.changeLang(lang);
});
return pending;
}
else { // we have this language, return an Observable
this.changeLang(lang);
return rxjs.of(this.translations[lang]);
}
};
/**
* Retrieves the given translations
*/
/**
* Retrieves the given translations
* @param {?} lang
* @return {?}
*/
TranslateService.prototype.retrieveTranslations = /**
* Retrieves the given translations
* @param {?} lang
* @return {?}
*/
function (lang) {
/** @type {?} */
var pending;
// if this language is unavailable, ask for it
if (typeof this.translations[lang] === "undefined") {
this._translationRequests[lang] = this._translationRequests[lang] || this.getTranslation(lang);
pending = this._translationRequests[lang];
}
return pending;
};
/**
* Gets an object of translations for a given language with the current loader
* and passes it through the compiler
*/
/**
* Gets an object of translations for a given language with the current loader
* and passes it through the compiler
* @param {?} lang
* @return {?}
*/
TranslateService.prototype.getTranslation = /**
* Gets an object of translations for a given language with the current loader
* and passes it through the compiler
* @param {?} lang
* @return {?}
*/
function (lang) {
var _this = this;
this.pending = true;
this.loadingTranslations = this.currentLoader.getTranslation(lang).pipe(operators.share());
this.loadingTranslations.pipe(operators.take(1))
.subscribe(function (res) {
_this.translations[lang] = _this.compiler.compileTranslations(res, lang);
_this.updateLangs();
_this.pending = false;
}, function (err) {
_this.pending = false;
});
return this.loadingTranslations;
};
/**
* Manually sets an object of translations for a given language
* after passing it through the compiler
*/
/**
* Manually sets an object of translations for a given language
* after passing it through the compiler
* @param {?} lang
* @param {?} translations
* @param {?=} shouldMerge
* @return {?}
*/
TranslateService.prototype.setTranslation = /**
* Manually sets an object of translations for a given language
* after passing it through the compiler
* @param {?} lang
* @param {?} translations
* @param {?=} shouldMerge
* @return {?}
*/
function (lang, translations, shouldMerge) {
if (shouldMerge === void 0) {
shouldMerge = false;
}
translations = this.compiler.compileTranslations(translations, lang);
if (shouldMerge && this.translations[lang]) {
this.translations[lang] = mergeDeep(this.translations[lang], translations);
}
else {
this.translations[lang] = translations;
}
this.updateLangs();
this.onTranslationChange.emit({ lang: lang, translations: this.translations[lang] });
};
/**
* Returns an array of currently available langs
*/
/**
* Returns an array of currently available langs
* @return {?}
*/
TranslateService.prototype.getLangs = /**
* Returns an array of currently available langs
* @return {?}
*/
function () {
return this.langs;
};
/**
* Add available langs
*/
/**
* Add available langs
* @param {?} langs
* @return {?}
*/
TranslateService.prototype.addLangs = /**
* Add available langs
* @param {?} langs
* @return {?}
*/
function (langs) {
var _this = this;
langs.forEach(function (lang) {
if (_this.langs.indexOf(lang) === -1) {
_this.langs.push(lang);
}
});
};
/**
* Update the list of available langs
*/
/**
* Update the list of available langs
* @return {?}
*/
TranslateService.prototype.updateLangs = /**
* Update the list of available langs
* @return {?}
*/
function () {
this.addLangs(Object.keys(this.translations));
};
/**
* Returns the parsed result of the translations
*/
/**
* Returns the parsed result of the translations
* @param {?} translations
* @param {?} key
* @param {?=} interpolateParams
* @return {?}
*/
TranslateService.prototype.getParsedResult = /**
* Returns the parsed result of the translations
* @param {?} translations
* @param {?} key
* @param {?=} interpolateParams
* @return {?}
*/
function (translations, key, interpolateParams) {
var e_1, _a, e_2, _b;
/** @type {?} */
var res;
if (key instanceof Array) {
/** @type {?} */
var result = {};
/** @type {?} */
var observables = false;
try {
for (var key_1 = __values(key), key_1_1 = key_1.next(); !key_1_1.done; key_1_1 = key_1.next()) {
var k = key_1_1.value;
result[k] = this.getParsedResult(translations, k, interpolateParams);
if (typeof result[k].subscribe === "function") {
observables = true;
}
}
}
catch (e_1_1) {
e_1 = { error: e_1_1 };
}
finally {
try {
if (key_1_1 && !key_1_1.done && (_a = key_1.return))
_a.call(key_1);
}
finally {
if (e_1)
throw e_1.error;
}
}
if (observables) {
/** @type {?} */
var mergedObs = void 0;
try {
for (var key_2 = __values(key), key_2_1 = key_2.next(); !key_2_1.done; key_2_1 = key_2.next()) {
var k = key_2_1.value;
/** @type {?} */
var obs = typeof result[k].subscribe === "function" ? result[k] : rxjs.of(( /** @type {?} */(result[k])));
if (typeof mergedObs === "undefined") {
mergedObs = obs;
}
else {
mergedObs = rxjs.merge(mergedObs, obs);
}
}
}
catch (e_2_1) {
e_2 = { error: e_2_1 };
}
finally {
try {
if (key_2_1 && !key_2_1.done && (_b = key_2.return))
_b.call(key_2);
}
finally {
if (e_2)
throw e_2.error;
}
}
return mergedObs.pipe(operators.toArray(), operators.map(function (arr) {
/** @type {?} */
var obj = {};
arr.forEach(function (value, index) {
obj[key[index]] = value;
});
return obj;
}));
}
return result;
}
if (translations) {
res = this.parser.interpolate(this.parser.getValue(translations, key), interpolateParams);
}
if (typeof res === "undefined" && this.defaultLang && this.defaultLang !== this.currentLang && this.useDefaultLang) {
res = this.parser.interpolate(this.parser.getValue(this.translations[this.defaultLang], key), interpolateParams);
}
if (typeof res === "undefined") {
/** @type {?} */
var params = { key: key, translateService: this };
if (typeof interpolateParams !== 'undefined') {
params.interpolateParams = interpolateParams;
}
res = this.missingTranslationHandler.handle(params);
}
return typeof res !== "undefined" ? res : key;
};
/**
* Gets the translated value of a key (or an array of keys)
* @returns the translated key, or an object of translated keys
*/
/**
* Gets the translated value of a key (or an array of keys)
* @param {?} key
* @param {?=} interpolateParams
* @return {?} the translated key, or an object of translated keys
*/
TranslateService.prototype.get = /**
* Gets the translated value of a key (or an array of keys)
* @param {?} key
* @param {?=} interpolateParams
* @return {?} the translated key, or an object of translated keys
*/
function (key, interpolateParams) {
var _this = this;
if (!isDefined(key) || !key.length) {
throw new Error("Parameter \"key\" required");
}
// check if we are loading a new translation to use
if (this.pending) {
return rxjs.Observable.create(function (observer) {
/** @type {?} */
var onComplete = function (res) {
observer.next(res);
observer.complete();
};
/** @type {?} */
var onError = function (err) {
observer.error(err);
};
_this.loadingTranslations.subscribe(function (res) {
res = _this.getParsedResult(_this.compiler.compileTranslations(res, _this.currentLang), key, interpolateParams);
if (typeof res.subscribe === "function") {
res.subscribe(onComplete, onError);
}
else {
onComplete(res);
}
}, onError);
});
}
else {
/** @type {?} */
var res = this.getParsedResult(this.translations[this.currentLang], key, interpolateParams);
if (typeof res.subscribe === "function") {
return res;
}
else {
return rxjs.of(res);
}
}
};
/**
* Returns a stream of translated values of a key (or an array of keys) which updates
* whenever the language changes.
* @returns A stream of the translated key, or an object of translated keys
*/
/**
* Returns a stream of translated values of a key (or an array of keys) which updates
* whenever the language changes.
* @param {?} key
* @param {?=} interpolateParams
* @return {?} A stream of the translated key, or an object of translated keys
*/
TranslateService.prototype.stream = /**
* Returns a stream of translated values of a key (or an array of keys) which updates
* whenever the language changes.
* @param {?} key
* @param {?=} interpolateParams
* @return {?} A stream of the translated key, or an object of translated keys
*/
function (key, interpolateParams) {
var _this = this;
if (!isDefined(key) || !key.length) {
throw new Error("Parameter \"key\" required");
}
return rxjs.concat(this.get(key, interpolateParams), this.onLangChange.pipe(operators.switchMap(function (event) {
/** @type {?} */
var res = _this.getParsedResult(event.translations, key, interpolateParams);
if (typeof res.subscribe === "function") {
return res;
}
else {
return rxjs.of(res);
}
})));
};
/**
* Returns a translation instantly from the internal state of loaded translation.
* All rules regarding the current language, the preferred language of even fallback languages will be used except any promise handling.
*/
/**
* Returns a translation instantly from the internal state of loaded translation.
* All rules regarding the current language, the preferred language of even fallback languages will be used except any promise handling.
* @param {?} key
* @param {?=} interpolateParams
* @return {?}
*/
TranslateService.prototype.instant = /**
* Returns a translation instantly from the internal state of loaded translation.
* All rules regarding the current language, the preferred language of even fallback languages will be used except any promise handling.
* @param {?} key
* @param {?=} interpolateParams
* @return {?}
*/
function (key, interpolateParams) {
if (!isDefined(key) || !key.length) {
throw new Error("Parameter \"key\" required");
}
/** @type {?} */
var res = this.getParsedResult(this.translations[this.currentLang], key, interpolateParams);
if (typeof res.subscribe !== "undefined") {
if (key instanceof Array) {
/** @type {?} */
var obj_1 = {};
key.forEach(function (value, index) {
obj_1[key[index]] = key[index];
});
return obj_1;
}
return key;
}
else {
return res;
}
};
/**
* Sets the translated value of a key, after compiling it
*/
/**
* Sets the translated value of a key, after compiling it
* @param {?} key
* @param {?} value
* @param {?=} lang
* @return {?}
*/
TranslateService.prototype.set = /**
* Sets the translated value of a key, after compiling it
* @param {?} key
* @param {?} value
* @param {?=} lang
* @return {?}
*/
function (key, value, lang) {
if (lang === void 0) {
lang = this.currentLang;
}
this.translations[lang][key] = this.compiler.compile(value, lang);
this.updateLangs();
this.onTranslationChange.emit({ lang: lang, translations: this.translations[lang] });
};
/**
* Changes the current lang
*/
/**
* Changes the current lang
* @param {?} lang
* @return {?}
*/
TranslateService.prototype.changeLang = /**
* Changes the current lang
* @param {?} lang
* @return {?}
*/
function (lang) {
this.currentLang = lang;
this.onLangChange.emit({ lang: lang, translations: this.translations[lang] });
// if there is no default lang, use the one that we just set
if (!this.defaultLang) {
this.changeDefaultLang(lang);
}
};
/**
* Changes the default lang
*/
/**
* Changes the default lang
* @param {?} lang
* @return {?}
*/
TranslateService.prototype.changeDefaultLang = /**
* Changes the default lang
* @param {?} lang
* @return {?}
*/
function (lang) {
this.defaultLang = lang;
this.onDefaultLangChange.emit({ lang: lang, translations: this.translations[lang] });
};
/**
* Allows to reload the lang file from the file
*/
/**
* Allows to reload the lang file from the file
* @param {?} lang
* @return {?}
*/
TranslateService.prototype.reloadLang = /**
* Allows to reload the lang file from the file
* @param {?} lang
* @return {?}
*/
function (lang) {
this.resetLang(lang);
return this.getTranslation(lang);
};
/**
* Deletes inner translation
*/
/**
* Deletes inner translation
* @param {?} lang
* @return {?}
*/
TranslateService.prototype.resetLang = /**
* Deletes inner translation
* @param {?} lang
* @return {?}
*/
function (lang) {
this._translationRequests[lang] = undefined;
this.translations[lang] = undefined;
};
/**
* Returns the language code name from the browser, e.g. "de"
*/
/**
* Returns the language code name from the browser, e.g. "de"
* @return {?}
*/
TranslateService.prototype.getBrowserLang = /**
* Returns the language code name from the browser, e.g. "de"
* @return {?}
*/
function () {
if (typeof window === 'undefined' || typeof window.navigator === 'undefined') {
return undefined;
}
/** @type {?} */
var browserLang = window.navigator.languages ? window.navigator.languages[0] : null;
browserLang = browserLang || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage;
if (browserLang.indexOf('-') !== -1) {
browserLang = browserLang.split('-')[0];
}
if (browserLang.indexOf('_') !== -1) {
browserLang = browserLang.split('_')[0];
}
return browserLang;
};
/**
* Returns the culture language code name from the browser, e.g. "de-DE"
*/
/**
* Returns the culture language code name from the browser, e.g. "de-DE"
* @return {?}
*/
TranslateService.prototype.getBrowserCultureLang = /**
* Returns the culture language code name from the browser, e.g. "de-DE"
* @return {?}
*/
function () {
if (typeof window === 'undefined' || typeof window.navigator === 'undefined') {
return undefined;
}
/** @type {?} */
var browserCultureLang = window.navigator.languages ? window.navigator.languages[0] : null;
browserCultureLang = browserCultureLang || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage;
return browserCultureLang;
};
TranslateService.decorators = [
{ type: core.Injectable }
];
/** @nocollapse */
TranslateService.ctorParameters = function () {
return [
{ type: TranslateStore },
{ type: TranslateLoader },
{ type: TranslateCompiler },
{ type: TranslateParser },
{ type: MissingTranslationHandler },
{ type: Boolean, decorators: [{ type: core.Inject, args: [USE_DEFAULT_LANG,] }] },
{ type: Boolean, decorators: [{ type: core.Inject, args: [USE_STORE,] }] }
];
};
return TranslateService;
}());
/**
* @fileoverview added by tsickle
* @suppress {checkTypes,extraRequire,uselessCode} checked by tsc
*/
var TranslateDirective = /** @class */ (function () {
function TranslateDirective(translateService, element, _ref) {
var _this = this;
this.translateService = translateService;
this.element = element;
this._ref = _ref;
// subscribe to onTranslationChange event, in case the translations of the current lang change
if (!this.onTranslationChangeSub) {
this.onTranslationChangeSub = this.translateService.onTranslationChange.subscribe(function (event) {
if (event.lang === _this.translateService.currentLang) {
_this.checkNodes(true, event.translations);
}
});
}
// subscribe to onLangChange event, in case the language changes
if (!this.onLangChangeSub) {
this.onLangChangeSub = this.translateService.onLangChange.subscribe(function (event) {
_this.checkNodes(true, event.translations);
});
}
// subscribe to onDefaultLangChange event, in case the default language changes
if (!this.onDefaultLangChangeSub) {
this.onDefaultLangChangeSub = this.translateService.onDefaultLangChange.subscribe(function (event) {
_this.checkNodes(true);
});
}
}
Object.defineProperty(TranslateDirective.prototype, "translate", {
set: /**
* @param {?} key
* @return {?}
*/ function (key) {
if (key) {
this.key = key;
this.checkNodes();
}
},
enumerable: true,
configurable: true
});
Object.defineProperty(TranslateDirective.prototype, "translateParams", {
set: /**
* @param {?} params
* @return {?}
*/ function (params) {
if (!equals(this.currentParams, params)) {
this.currentParams = params;
this.checkNodes(true);
}
},
enumerable: true,
configurable: true
});
/**
* @return {?}
*/
TranslateDirective.prototype.ngAfterViewChecked = /**
* @return {?}
*/
function () {
this.checkNodes();
};
/**
* @param {?=} forceUpdate
* @param {?=} translations
* @return {?}
*/
TranslateDirective.prototype.checkNodes = /**
* @param {?=} forceUpdate
* @param {?=} translations
* @return {?}
*/
function (forceUpdate, translations) {
if (forceUpdate === void 0) {
forceUpdate = false;
}
/** @type {?} */
var nodes = this.element.nativeElement.childNodes;
// if the element is empty
if (!nodes.length) {
// we add the key as content
this.setContent(this.element.nativeElement, this.key);
nodes = this.element.nativeElement.childNodes;