@ngx-translate/core
Version:
The internationalization (i18n) library for Angular
2 lines • 16.6 kB
JavaScript
!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["ngx-translate"]=t["ngx-translate"]||{},t["ngx-translate"].core={}),t.ng.core,t.rxjs,t.rxjs.operators)}(this,function(t,o,C,m){"use strict";var r=function(t,e){return(r=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 n(t,e){function n(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}function T(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 a=function P(){},i=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.prototype.getTranslation=function(t){return C.of({})},e.decorators=[{type:o.Injectable}],e}(a),s=function j(){},u=function(){function t(){}return t.prototype.handle=function(t){return t.key},t.decorators=[{type:o.Injectable}],t}(),l=function D(){},h=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return n(e,t),e.prototype.compile=function(t,e){return t},e.prototype.compileTranslations=function(t,e){return t},e.decorators=[{type:o.Injectable}],e}(l);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)&&"undefined"!=typeof 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 g(t){return null!=t}function f(t){return t&&"object"==typeof t&&!Array.isArray(t)}var p=function S(){},d=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.templateMatcher=/{{\s?([^{}\s]*)\s?}}/g,t}return n(t,e),t.prototype.interpolate=function(t,e){return"string"==typeof t?this.interpolateString(t,e):"function"==typeof t?this.interpolateFunction(t,e):t},t.prototype.getValue=function(t,e){var n=e.split(".");for(e="";e+=n.shift(),!g(t)||!g(t[e])||"object"!=typeof t[e]&&n.length?n.length?e+=".":t=undefined:(t=t[e],e=""),n.length;);return t},t.prototype.interpolateFunction=function(t,e){return t(e)},t.prototype.interpolateString=function(t,r){var a=this;return r?t.replace(this.templateMatcher,function(t,e){var n=a.getValue(r,e);return g(n)?n:t}):t},t.decorators=[{type:o.Injectable}],t}(p),y=function E(){this.currentLang=this.defaultLang,this.translations={},this.langs=[],this.onTranslationChange=new o.EventEmitter,this.onLangChange=new o.EventEmitter,this.onDefaultLangChange=new o.EventEmitter},v=new o.InjectionToken("USE_STORE"),b=new o.InjectionToken("USE_DEFAULT_LANG"),L=function(){function t(t,e,n,r,a,i,s){void 0===i&&(i=!0),void 0===s&&(s=!1),this.store=t,this.currentLoader=e,this.compiler=n,this.parser=r,this.missingTranslationHandler=a,this.useDefaultLang=i,this.isolate=s,this.pending=!1,this._onTranslationChange=new o.EventEmitter,this._onLangChange=new o.EventEmitter,this._onDefaultLangChange=new o.EventEmitter,this._langs=[],this._translations={},this._translationRequests={}}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(e){var n=this;if(e!==this.defaultLang){var t=this.retrieveTranslations(e);void 0!==t?(this.defaultLang||(this.defaultLang=e),t.pipe(m.take(1)).subscribe(function(t){n.changeDefaultLang(e)})):this.changeDefaultLang(e)}},t.prototype.getDefaultLang=function(){return this.defaultLang},t.prototype.use=function(e){var n=this;if(e===this.currentLang)return C.of(this.translations[e]);var t=this.retrieveTranslations(e);return void 0!==t?(this.currentLang||(this.currentLang=e),t.pipe(m.take(1)).subscribe(function(t){n.changeLang(e)}),t):(this.changeLang(e),C.of(this.translations[e]))},t.prototype.retrieveTranslations=function(t){var e;return"undefined"==typeof this.translations[t]&&(this._translationRequests[t]=this._translationRequests[t]||this.getTranslation(t),e=this._translationRequests[t]),e},t.prototype.getTranslation=function(e){var n=this;return this.pending=!0,this.loadingTranslations=this.currentLoader.getTranslation(e).pipe(m.share()),this.loadingTranslations.pipe(m.take(1)).subscribe(function(t){n.translations[e]=n.compiler.compileTranslations(t,e),n.updateLangs(),n.pending=!1},function(t){n.pending=!1}),this.loadingTranslations},t.prototype.setTranslation=function(t,e,n){void 0===n&&(n=!1),e=this.compiler.compileTranslations(e,t),n&&this.translations[t]?this.translations[t]=function s(r,a){var i=Object.assign({},r);return f(r)&&f(a)&&Object.keys(a).forEach(function(t){var e,n;f(a[t])?t in r?i[t]=s(r[t],a[t]):Object.assign(i,((e={})[t]=a[t],e)):Object.assign(i,((n={})[t]=a[t],n))}),i}(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,r,e){var n,a,i,s,o;if(r instanceof Array){var u={},l=!1;try{for(var h=T(r),c=h.next();!c.done;c=h.next()){u[d=c.value]=this.getParsedResult(t,d,e),"function"==typeof u[d].subscribe&&(l=!0)}}catch(b){n={error:b}}finally{try{c&&!c.done&&(a=h["return"])&&a.call(h)}finally{if(n)throw n.error}}if(l){var g=void 0;try{for(var f=T(r),p=f.next();!p.done;p=f.next()){var d,y="function"==typeof u[d=p.value].subscribe?u[d]:C.of(u[d]);g=void 0===g?y:C.merge(g,y)}}catch(L){i={error:L}}finally{try{p&&!p.done&&(s=f["return"])&&s.call(f)}finally{if(i)throw i.error}}return g.pipe(m.toArray(),m.map(function(t){var n={};return t.forEach(function(t,e){n[r[e]]=t}),n}))}return u}if(t&&(o=this.parser.interpolate(this.parser.getValue(t,r),e)),void 0===o&&this.defaultLang&&this.defaultLang!==this.currentLang&&this.useDefaultLang&&(o=this.parser.interpolate(this.parser.getValue(this.translations[this.defaultLang],r),e)),void 0===o){var v={key:r,translateService:this};void 0!==e&&(v.interpolateParams=e),o=this.missingTranslationHandler.handle(v)}return void 0!==o?o:r},t.prototype.get=function(a,i){var s=this;if(!g(a)||!a.length)throw new Error('Parameter "key" required');if(this.pending)return C.Observable.create(function(e){var n=function(t){e.next(t),e.complete()},r=function(t){e.error(t)};s.loadingTranslations.subscribe(function(t){"function"==typeof(t=s.getParsedResult(s.compiler.compileTranslations(t,s.currentLang),a,i)).subscribe?t.subscribe(n,r):n(t)},r)});var t=this.getParsedResult(this.translations[this.currentLang],a,i);return"function"==typeof t.subscribe?t:C.of(t)},t.prototype.stream=function(n,r){var a=this;if(!g(n)||!n.length)throw new Error('Parameter "key" required');return C.concat(this.get(n,r),this.onLangChange.pipe(m.switchMap(function(t){var e=a.getParsedResult(t.translations,n,r);return"function"==typeof e.subscribe?e:C.of(e)})))},t.prototype.instant=function(n,t){if(!g(n)||!n.length)throw new Error('Parameter "key" required');var e=this.getParsedResult(this.translations[this.currentLang],n,t);if("undefined"==typeof e.subscribe)return e;if(n instanceof Array){var r={};return n.forEach(function(t,e){r[n[e]]=n[e]}),r}return n},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]}),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]=undefined,this.translations[t]=undefined},t.prototype.getBrowserLang=function(){if("undefined"==typeof window||"undefined"==typeof window.navigator)return undefined;var t=window.navigator.languages?window.navigator.languages[0]:null;return-1!==(t=t||window.navigator.language||window.navigator.browserLanguage||window.navigator.userLanguage).indexOf("-")&&(t=t.split("-")[0]),-1!==t.indexOf("_")&&(t=t.split("_")[0]),t},t.prototype.getBrowserCultureLang=function(){if("undefined"==typeof window||"undefined"==typeof window.navigator)return undefined;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:o.Injectable}],t.ctorParameters=function(){return[{type:y},{type:a},{type:l},{type:p},{type:s},{type:Boolean,decorators:[{type:o.Inject,args:[b]}]},{type:Boolean,decorators:[{type:o.Inject,args:[v]}]}]},t}(),w=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(this.key)i=this.key,t&&(a.lastKey=null);else{var s=this.getContent(a),o=s.trim();o.length&&(s!==a.currentValue?(i=o,a.originalContent=this.getContent(a)):a.originalContent&&t&&(a.lastKey=null,i=a.originalContent.trim()))}this.updateValue(i,a,e)}}},t.prototype.updateValue=function(e,n,t){var r=this;if(e){if(n.lastKey===e&&this.lastParams===this.currentParams)return;this.lastParams=this.currentParams;var a=function(t){t!==e&&(n.lastKey=e),n.originalContent||(n.originalContent=r.getContent(n)),n.currentValue=g(t)?t:n.originalContent||e,r.setContent(n,r.key?n.currentValue:n.originalContent.replace(e,n.currentValue)),r._ref.markForCheck()};if(g(t)){var i=this.translateService.getParsedResult(t,e,this.currentParams);"function"==typeof i.subscribe?i.subscribe(a):a(i)}else this.translateService.get(e,this.currentParams).subscribe(a)}},t.prototype.getContent=function(t){return g(t.textContent)?t.textContent:t.data},t.prototype.setContent=function(t,e){g(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:o.Directive,args:[{selector:"[translate],[ngx-translate]"}]}],t.ctorParameters=function(){return[{type:L},{type:o.ElementRef},{type:o.ChangeDetectorRef}]},t.propDecorators={translate:[{type:o.Input}],translateParams:[{type:o.Input}]},t}(),_=function(){function t(t,e){this.translate=t,this._ref=e,this.value=""}return t.prototype.updateValue=function(e,t,n){var r=this,a=function(t){r.value=t!==undefined?t:e,r.lastKey=e,r._ref.markForCheck()};if(n){var i=this.translate.getParsedResult(n,e,t);"function"==typeof i.subscribe?i.subscribe(a):a(i)}this.translate.get(e,t).subscribe(a)},t.prototype.transform=function(e){for(var n,r=this,t=[],a=1;a<arguments.length;a++)t[a-1]=arguments[a];if(!e||0===e.length)return e;if(c(e,this.lastKey)&&c(t,this.lastParams))return this.value;if(g(t[0])&&t.length)if("string"==typeof t[0]&&t[0].length){var i=t[0].replace(/(\')?([a-zA-Z0-9_]+)(\')?(\s)?:/g,'"$2":').replace(/:(\s)?(\')(.*?)(\')/g,':"$3"');try{n=JSON.parse(i)}catch(s){throw new SyntaxError("Wrong parameter in TranslatePipe. Expected a valid Object, received: "+t[0])}}else"object"!=typeof t[0]||Array.isArray(t[0])||(n=t[0]);return this.lastKey=e,this.lastParams=t,this.updateValue(e,n),this._dispose(),this.onTranslationChange||(this.onTranslationChange=this.translate.onTranslationChange.subscribe(function(t){r.lastKey&&t.lang===r.translate.currentLang&&(r.lastKey=null,r.updateValue(e,n,t.translations))})),this.onLangChange||(this.onLangChange=this.translate.onLangChange.subscribe(function(t){r.lastKey&&(r.lastKey=null,r.updateValue(e,n,t.translations))})),this.onDefaultLangChange||(this.onDefaultLangChange=this.translate.onDefaultLangChange.subscribe(function(){r.lastKey&&(r.lastKey=null,r.updateValue(e,n))})),this.value},t.prototype._dispose=function(){"undefined"!=typeof this.onTranslationChange&&(this.onTranslationChange.unsubscribe(),this.onTranslationChange=undefined),"undefined"!=typeof this.onLangChange&&(this.onLangChange.unsubscribe(),this.onLangChange=undefined),"undefined"!=typeof this.onDefaultLangChange&&(this.onDefaultLangChange.unsubscribe(),this.onDefaultLangChange=undefined)},t.prototype.ngOnDestroy=function(){this._dispose()},t.decorators=[{type:o.Injectable},{type:o.Pipe,args:[{name:"translate",pure:!1}]}],t.ctorParameters=function(){return[{type:L},{type:o.ChangeDetectorRef}]},t}(),e=function(){function e(){}return e.forRoot=function(t){return void 0===t&&(t={}),{ngModule:e,providers:[t.loader||{provide:a,useClass:i},t.compiler||{provide:l,useClass:h},t.parser||{provide:p,useClass:d},t.missingTranslationHandler||{provide:s,useClass:u},y,{provide:v,useValue:t.isolate},{provide:b,useValue:t.useDefaultLang},L]}},e.forChild=function(t){return void 0===t&&(t={}),{ngModule:e,providers:[t.loader||{provide:a,useClass:i},t.compiler||{provide:l,useClass:h},t.parser||{provide:p,useClass:d},t.missingTranslationHandler||{provide:s,useClass:u},{provide:v,useValue:t.isolate},{provide:b,useValue:t.useDefaultLang},L]}},e.decorators=[{type:o.NgModule,args:[{declarations:[_,w],exports:[_,w]}]}],e}();t.TranslateModule=e,t.TranslateLoader=a,t.TranslateFakeLoader=i,t.USE_STORE=v,t.USE_DEFAULT_LANG=b,t.TranslateService=L,t.MissingTranslationHandler=s,t.FakeMissingTranslationHandler=u,t.TranslateParser=p,t.TranslateDefaultParser=d,t.TranslateCompiler=l,t.TranslateFakeCompiler=h,t.TranslateDirective=w,t.TranslatePipe=_,t.TranslateStore=y,Object.defineProperty(t,"__esModule",{value:!0})});
//# sourceMappingURL=ngx-translate-core.umd.min.js.map