UNPKG

18.3 kBJavaScriptView Raw
1!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("@angular/core"),require("rxjs"),require("rxjs/operators")):"function"==typeof define&&define.amd?define("@ngx-translate/core",["exports","@angular/core","rxjs","rxjs/operators"],e):e(((t=t||self)["ngx-translate"]=t["ngx-translate"]||{},t["ngx-translate"].core={}),t.ng.core,t.rxjs,t.rxjs.operators)}(this,(function(t,e,n,r){"use strict";
2/*! *****************************************************************************
3 Copyright (c) Microsoft Corporation. All rights reserved.
4 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
5 this file except in compliance with the License. You may obtain a copy of the
6 License at http://www.apache.org/licenses/LICENSE-2.0
7
8 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9 KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
10 WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
11 MERCHANTABLITY OR NON-INFRINGEMENT.
12
13 See the Apache Version 2.0 License for specific language governing permissions
14 and limitations under the License.
15 ***************************************************************************** */var a=function(t,e){return(a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n])})(t,e)};function i(t,e){function n(){this.constructor=t}a(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}var s=function(){return(s=Object.assign||function(t){for(var e,n=1,r=arguments.length;n<r;n++)for(var a in e=arguments[n])Object.prototype.hasOwnProperty.call(e,a)&&(t[a]=e[a]);return t}).apply(this,arguments)};function o(t){var e="function"==typeof Symbol&&t[Symbol.iterator],n=0;return e?e.call(t):{next:function(){return t&&n>=t.length&&(t=void 0),{value:t&&t[n++],done:!t}}}}var u=function(){};var l=function(t){function r(){return null!==t&&t.apply(this,arguments)||this}return i(r,t),r.prototype.getTranslation=function(t){return n.of({})},r.decorators=[{type:e.Injectable}],r}(u);var h=function(){};var g=function(){function t(){}return t.prototype.handle=function(t){return t.key},t.decorators=[{type:e.Injectable}],t}();function c(t,e){if(t===e)return!0;if(null===t||null===e)return!1;if(t!=t&&e!=e)return!0;var n,r,a,i=typeof t;if(i==typeof e&&"object"==i){if(!Array.isArray(t)){if(Array.isArray(e))return!1;for(r in a=Object.create(null),t){if(!c(t[r],e[r]))return!1;a[r]=!0}for(r in e)if(!(r in a)&&void 0!==e[r])return!1;return!0}if(!Array.isArray(e))return!1;if((n=t.length)==e.length){for(r=0;r<n;r++)if(!c(t[r],e[r]))return!1;return!0}}return!1}function f(t){return null!=t}function p(t){return t&&"object"==typeof t&&!Array.isArray(t)}var d=function(){};var y=function(t){function n(){var e=null!==t&&t.apply(this,arguments)||this;return e.templateMatcher=/{{\s?([^{}\s]*)\s?}}/g,e}return i(n,t),n.prototype.interpolate=function(t,e){return"string"==typeof t?this.interpolateString(t,e):"function"==typeof t?this.interpolateFunction(t,e):t},n.prototype.getValue=function(t,e){var n="string"==typeof e?e.split("."):[e];e="";do{e+=n.shift(),!f(t)||!f(t[e])||"object"!=typeof t[e]&&n.length?n.length?e+=".":t=void 0:(t=t[e],e="")}while(n.length);return t},n.prototype.interpolateFunction=function(t,e){return t(e)},n.prototype.interpolateString=function(t,e){var n=this;return e?t.replace(this.templateMatcher,(function(t,r){var a=n.getValue(e,r);return f(a)?a:t})):t},n.decorators=[{type:e.Injectable}],n}(d);var v=function(){};var b=function(t){function n(){return null!==t&&t.apply(this,arguments)||this}return i(n,t),n.prototype.compile=function(t,e){return t},n.prototype.compileTranslations=function(t,e){return t},n.decorators=[{type:e.Injectable}],n}(v),L=function(){this.currentLang=this.defaultLang,this.translations={},this.langs=[],this.onTranslationChange=new e.EventEmitter,this.onLangChange=new e.EventEmitter,this.onDefaultLangChange=new e.EventEmitter};var C=new e.InjectionToken("USE_STORE"),m=new e.InjectionToken("USE_DEFAULT_LANG"),T=new e.InjectionToken("DEFAULT_LANGUAGE"),w=new e.InjectionToken("USE_EXTEND");var _=function(){function t(t,n,r,a,i,s,o,u,l){void 0===s&&(s=!0),void 0===o&&(o=!1),void 0===u&&(u=!1),this.store=t,this.currentLoader=n,this.compiler=r,this.parser=a,this.missingTranslationHandler=i,this.useDefaultLang=s,this.isolate=o,this.extend=u,this.pending=!1,this._onTranslationChange=new e.EventEmitter,this._onLangChange=new e.EventEmitter,this._onDefaultLangChange=new e.EventEmitter,this._langs=[],this._translations={},this._translationRequests={},l&&this.setDefaultLang(l)}return Object.defineProperty(t.prototype,"onTranslationChange",{get:function(){return this.isolate?this._onTranslationChange:this.store.onTranslationChange},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"onLangChange",{get:function(){return this.isolate?this._onLangChange:this.store.onLangChange},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"onDefaultLangChange",{get:function(){return this.isolate?this._onDefaultLangChange:this.store.onDefaultLangChange},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"defaultLang",{get:function(){return this.isolate?this._defaultLang:this.store.defaultLang},set:function(t){this.isolate?this._defaultLang=t:this.store.defaultLang=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"currentLang",{get:function(){return this.isolate?this._currentLang:this.store.currentLang},set:function(t){this.isolate?this._currentLang=t:this.store.currentLang=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"langs",{get:function(){return this.isolate?this._langs:this.store.langs},set:function(t){this.isolate?this._langs=t:this.store.langs=t},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translations",{get:function(){return this.isolate?this._translations:this.store.translations},set:function(t){this.isolate?this._translations=t:this.store.translations=t},enumerable:!0,configurable:!0}),t.prototype.setDefaultLang=function(t){var e=this;if(t!==this.defaultLang){var n=this.retrieveTranslations(t);void 0!==n?(null==this.defaultLang&&(this.defaultLang=t),n.pipe(r.take(1)).subscribe((function(n){e.changeDefaultLang(t)}))):this.changeDefaultLang(t)}},t.prototype.getDefaultLang=function(){return this.defaultLang},t.prototype.use=function(t){var e=this;if(t===this.currentLang)return n.of(this.translations[t]);var a=this.retrieveTranslations(t);return void 0!==a?(this.currentLang||(this.currentLang=t),a.pipe(r.take(1)).subscribe((function(n){e.changeLang(t)})),a):(this.changeLang(t),n.of(this.translations[t]))},t.prototype.retrieveTranslations=function(t){var e;return(void 0===this.translations[t]||this.extend)&&(this._translationRequests[t]=this._translationRequests[t]||this.getTranslation(t),e=this._translationRequests[t]),e},t.prototype.getTranslation=function(t){var e=this;this.pending=!0;var n=this.currentLoader.getTranslation(t).pipe(r.shareReplay(1),r.take(1));return this.loadingTranslations=n.pipe(r.map((function(n){return e.compiler.compileTranslations(n,t)})),r.shareReplay(1),r.take(1)),this.loadingTranslations.subscribe({next:function(n){e.translations[t]=e.extend&&e.translations[t]?s(s({},n),e.translations[t]):n,e.updateLangs(),e.pending=!1},error:function(t){e.pending=!1}}),n},t.prototype.setTranslation=function(t,e,n){void 0===n&&(n=!1),e=this.compiler.compileTranslations(e,t),(n||this.extend)&&this.translations[t]?this.translations[t]=function t(e,n){var r=Object.assign({},e);return p(e)&&p(n)&&Object.keys(n).forEach((function(a){var i,s;p(n[a])?a in e?r[a]=t(e[a],n[a]):Object.assign(r,((i={})[a]=n[a],i)):Object.assign(r,((s={})[a]=n[a],s))})),r}(this.translations[t],e):this.translations[t]=e,this.updateLangs(),this.onTranslationChange.emit({lang:t,translations:this.translations[t]})},t.prototype.getLangs=function(){return this.langs},t.prototype.addLangs=function(t){var e=this;t.forEach((function(t){-1===e.langs.indexOf(t)&&e.langs.push(t)}))},t.prototype.updateLangs=function(){this.addLangs(Object.keys(this.translations))},t.prototype.getParsedResult=function(t,e,a){var i,s,u;if(e instanceof Array){var l={},h=!1;try{for(var g=o(e),c=g.next();!c.done;c=g.next()){var f=c.value;l[f]=this.getParsedResult(t,f,a),n.isObservable(l[f])&&(h=!0)}}catch(t){i={error:t}}finally{try{c&&!c.done&&(s=g.return)&&s.call(g)}finally{if(i)throw i.error}}if(h){var p=e.map((function(t){return n.isObservable(l[t])?l[t]:n.of(l[t])}));return n.forkJoin(p).pipe(r.map((function(t){var n={};return t.forEach((function(t,r){n[e[r]]=t})),n})))}return l}if(t&&(u=this.parser.interpolate(this.parser.getValue(t,e),a)),void 0===u&&null!=this.defaultLang&&this.defaultLang!==this.currentLang&&this.useDefaultLang&&(u=this.parser.interpolate(this.parser.getValue(this.translations[this.defaultLang],e),a)),void 0===u){var d={key:e,translateService:this};void 0!==a&&(d.interpolateParams=a),u=this.missingTranslationHandler.handle(d)}return void 0!==u?u:e},t.prototype.get=function(t,e){var a=this;if(!f(t)||!t.length)throw new Error('Parameter "key" required');if(this.pending)return this.loadingTranslations.pipe(r.concatMap((function(r){return r=a.getParsedResult(r,t,e),n.isObservable(r)?r:n.of(r)})));var i=this.getParsedResult(this.translations[this.currentLang],t,e);return n.isObservable(i)?i:n.of(i)},t.prototype.getStreamOnTranslationChange=function(t,e){var a=this;if(!f(t)||!t.length)throw new Error('Parameter "key" required');return n.concat(n.defer((function(){return a.get(t,e)})),this.onTranslationChange.pipe(r.switchMap((function(r){var i=a.getParsedResult(r.translations,t,e);return"function"==typeof i.subscribe?i:n.of(i)}))))},t.prototype.stream=function(t,e){var a=this;if(!f(t)||!t.length)throw new Error('Parameter "key" required');return n.concat(n.defer((function(){return a.get(t,e)})),this.onLangChange.pipe(r.switchMap((function(r){var i=a.getParsedResult(r.translations,t,e);return n.isObservable(i)?i:n.of(i)}))))},t.prototype.instant=function(t,e){if(!f(t)||!t.length)throw new Error('Parameter "key" required');var r=this.getParsedResult(this.translations[this.currentLang],t,e);if(n.isObservable(r)){if(t instanceof Array){var a={};return t.forEach((function(e,n){a[t[n]]=t[n]})),a}return t}return r},t.prototype.set=function(t,e,n){void 0===n&&(n=this.currentLang),this.translations[n][t]=this.compiler.compile(e,n),this.updateLangs(),this.onTranslationChange.emit({lang:n,translations:this.translations[n]})},t.prototype.changeLang=function(t){this.currentLang=t,this.onLangChange.emit({lang:t,translations:this.translations[t]}),null==this.defaultLang&&this.changeDefaultLang(t)},t.prototype.changeDefaultLang=function(t){this.defaultLang=t,this.onDefaultLangChange.emit({lang:t,translations:this.translations[t]})},t.prototype.reloadLang=function(t){return this.resetLang(t),this.getTranslation(t)},t.prototype.resetLang=function(t){this._translationRequests[t]=void 0,this.translations[t]=void 0},t.prototype.getBrowserLang=function(){if("undefined"!=typeof window&&void 0!==window.navigator){var t=window.navigator.languages?window.navigator.languages[0]:null;if(void 0!==(t=t||window.navigator.language||window.navigator.browserLanguage||window.navigator.userLanguage))return-1!==t.indexOf("-")&&(t=t.split("-")[0]),-1!==t.indexOf("_")&&(t=t.split("_")[0]),t}},t.prototype.getBrowserCultureLang=function(){if("undefined"!=typeof window&&void 0!==window.navigator){var t=window.navigator.languages?window.navigator.languages[0]:null;return t=t||window.navigator.language||window.navigator.browserLanguage||window.navigator.userLanguage}},t.decorators=[{type:e.Injectable}],t.ctorParameters=function(){return[{type:L},{type:u},{type:v},{type:d},{type:h},{type:Boolean,decorators:[{type:e.Inject,args:[m]}]},{type:Boolean,decorators:[{type:e.Inject,args:[C]}]},{type:Boolean,decorators:[{type:e.Inject,args:[w]}]},{type:String,decorators:[{type:e.Inject,args:[T]}]}]},t}();var P=function(){function t(t,e,n){var r=this;this.translateService=t,this.element=e,this._ref=n,this.onTranslationChangeSub||(this.onTranslationChangeSub=this.translateService.onTranslationChange.subscribe((function(t){t.lang===r.translateService.currentLang&&r.checkNodes(!0,t.translations)}))),this.onLangChangeSub||(this.onLangChangeSub=this.translateService.onLangChange.subscribe((function(t){r.checkNodes(!0,t.translations)}))),this.onDefaultLangChangeSub||(this.onDefaultLangChangeSub=this.translateService.onDefaultLangChange.subscribe((function(t){r.checkNodes(!0)})))}return Object.defineProperty(t.prototype,"translate",{set:function(t){t&&(this.key=t,this.checkNodes())},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translateParams",{set:function(t){c(this.currentParams,t)||(this.currentParams=t,this.checkNodes(!0))},enumerable:!0,configurable:!0}),t.prototype.ngAfterViewChecked=function(){this.checkNodes()},t.prototype.checkNodes=function(t,e){void 0===t&&(t=!1);var n=this.element.nativeElement.childNodes;n.length||(this.setContent(this.element.nativeElement,this.key),n=this.element.nativeElement.childNodes);for(var r=0;r<n.length;++r){var a=n[r];if(3===a.nodeType){var i=void 0;if(t&&(a.lastKey=null),f(a.lookupKey))i=a.lookupKey;else if(this.key)i=this.key;else{var s=this.getContent(a),o=s.trim();o.length&&(a.lookupKey=o,s!==a.currentValue?(i=o,a.originalContent=s||a.originalContent):a.originalContent?i=a.originalContent.trim():s!==a.currentValue&&(i=o,a.originalContent=s||a.originalContent))}this.updateValue(i,a,e)}}},t.prototype.updateValue=function(t,e,r){var a=this;if(t){if(e.lastKey===t&&this.lastParams===this.currentParams)return;this.lastParams=this.currentParams;var i=function(n){n!==t&&(e.lastKey=t),e.originalContent||(e.originalContent=a.getContent(e)),e.currentValue=f(n)?n:e.originalContent||t,a.setContent(e,a.key?e.currentValue:e.originalContent.replace(t,e.currentValue)),a._ref.markForCheck()};if(f(r)){var s=this.translateService.getParsedResult(r,t,this.currentParams);n.isObservable(s)?s.subscribe(i):i(s)}else this.translateService.get(t,this.currentParams).subscribe(i)}},t.prototype.getContent=function(t){return f(t.textContent)?t.textContent:t.data},t.prototype.setContent=function(t,e){f(t.textContent)?t.textContent=e:t.data=e},t.prototype.ngOnDestroy=function(){this.onLangChangeSub&&this.onLangChangeSub.unsubscribe(),this.onDefaultLangChangeSub&&this.onDefaultLangChangeSub.unsubscribe(),this.onTranslationChangeSub&&this.onTranslationChangeSub.unsubscribe()},t.decorators=[{type:e.Directive,args:[{selector:"[translate],[ngx-translate]"}]}],t.ctorParameters=function(){return[{type:_},{type:e.ElementRef},{type:e.ChangeDetectorRef}]},t.propDecorators={translate:[{type:e.Input}],translateParams:[{type:e.Input}]},t}();var j=function(){function t(t,e){this.translate=t,this._ref=e,this.value=""}return t.prototype.updateValue=function(t,e,r){var a=this,i=function(e){a.value=void 0!==e?e:t,a.lastKey=t,a._ref.markForCheck()};if(r){var s=this.translate.getParsedResult(r,t,e);n.isObservable(s.subscribe)?s.subscribe(i):i(s)}this.translate.get(t,e).subscribe(i)},t.prototype.transform=function(t){for(var e,n=this,r=[],a=1;a<arguments.length;a++)r[a-1]=arguments[a];if(!t||!t.length)return t;if(c(t,this.lastKey)&&c(r,this.lastParams))return this.value;if(f(r[0])&&r.length)if("string"==typeof r[0]&&r[0].length){var i=r[0].replace(/(\')?([a-zA-Z0-9_]+)(\')?(\s)?:/g,'"$2":').replace(/:(\s)?(\')(.*?)(\')/g,':"$3"');try{e=JSON.parse(i)}catch(t){throw new SyntaxError("Wrong parameter in TranslatePipe. Expected a valid Object, received: "+r[0])}}else"object"!=typeof r[0]||Array.isArray(r[0])||(e=r[0]);return this.lastKey=t,this.lastParams=r,this.updateValue(t,e),this._dispose(),this.onTranslationChange||(this.onTranslationChange=this.translate.onTranslationChange.subscribe((function(r){n.lastKey&&r.lang===n.translate.currentLang&&(n.lastKey=null,n.updateValue(t,e,r.translations))}))),this.onLangChange||(this.onLangChange=this.translate.onLangChange.subscribe((function(r){n.lastKey&&(n.lastKey=null,n.updateValue(t,e,r.translations))}))),this.onDefaultLangChange||(this.onDefaultLangChange=this.translate.onDefaultLangChange.subscribe((function(){n.lastKey&&(n.lastKey=null,n.updateValue(t,e))}))),this.value},t.prototype._dispose=function(){void 0!==this.onTranslationChange&&(this.onTranslationChange.unsubscribe(),this.onTranslationChange=void 0),void 0!==this.onLangChange&&(this.onLangChange.unsubscribe(),this.onLangChange=void 0),void 0!==this.onDefaultLangChange&&(this.onDefaultLangChange.unsubscribe(),this.onDefaultLangChange=void 0)},t.prototype.ngOnDestroy=function(){this._dispose()},t.decorators=[{type:e.Injectable},{type:e.Pipe,args:[{name:"translate",pure:!1}]}],t.ctorParameters=function(){return[{type:_},{type:e.ChangeDetectorRef}]},t}();var E=function(){function t(){}return t.forRoot=function(e){return void 0===e&&(e={}),{ngModule:t,providers:[e.loader||{provide:u,useClass:l},e.compiler||{provide:v,useClass:b},e.parser||{provide:d,useClass:y},e.missingTranslationHandler||{provide:h,useClass:g},L,{provide:C,useValue:e.isolate},{provide:m,useValue:e.useDefaultLang},{provide:w,useValue:e.extend},{provide:T,useValue:e.defaultLanguage},_]}},t.forChild=function(e){return void 0===e&&(e={}),{ngModule:t,providers:[e.loader||{provide:u,useClass:l},e.compiler||{provide:v,useClass:b},e.parser||{provide:d,useClass:y},e.missingTranslationHandler||{provide:h,useClass:g},{provide:C,useValue:e.isolate},{provide:m,useValue:e.useDefaultLang},{provide:w,useValue:e.extend},{provide:T,useValue:e.defaultLanguage},_]}},t.decorators=[{type:e.NgModule,args:[{declarations:[j,P],exports:[j,P]}]}],t}();t.DEFAULT_LANGUAGE=T,t.FakeMissingTranslationHandler=g,t.MissingTranslationHandler=h,t.TranslateCompiler=v,t.TranslateDefaultParser=y,t.TranslateDirective=P,t.TranslateFakeCompiler=b,t.TranslateFakeLoader=l,t.TranslateLoader=u,t.TranslateModule=E,t.TranslateParser=d,t.TranslatePipe=j,t.TranslateService=_,t.TranslateStore=L,t.USE_DEFAULT_LANG=m,t.USE_EXTEND=w,t.USE_STORE=C,Object.defineProperty(t,"__esModule",{value:!0})}));
16//# sourceMappingURL=ngx-translate-core.umd.min.js.map
\No newline at end of file