i18n-node-angular
Version:
express routes for i18n-node-angular
2 lines • 2.8 kB
JavaScript
/*! i18n-node-angular 2017-07-18 20:03:55 */
!function(){"use strict";var a=angular.module("i18n",[]);a.provider("i18n",function(){var a=this;a.objectNotation=".",a.setObjectNotation=function(b){a.objectNotation=b},a.$get=["$rootScope","$http","$q",function(b,c,d){var e=function(){this._localeLoadedDeferred=d.defer(),this._deferredStack=[],this.loaded=!1,this.init=function(a){if(a!=this.userLanguage){this._localeLoadedDeferred&&this._deferredStack.push(this._localeLoadedDeferred),this._localeLoadedDeferred=d.defer(),this.loaded=!1,this.userLanguage=a;var e=this;c({method:"get",url:"/i18n/"+a,cache:!0}).then(function(a){for(b.i18n=a.data,e.loaded=!0,e._localeLoadedDeferred.resolve(b.i18n);e._deferredStack.length;)e._deferredStack.pop().resolve(b.i18n);b.$broadcast("LOCALE_UPDATED")}).catch(function(a){for(e._localeLoadedDeferred.reject(a);e._deferredStack.length;)e._deferredStack.pop().reject(a)})}return this._localeLoadedDeferred.promise},this.i18n=function(){var a=d.defer(),b=this;return this.ensureLocaleIsLoaded().then(function(){a.resolve(b)}),a.promise},this.ensureLocaleIsLoaded=function(){return this._localeLoadedDeferred.promise},this.getTranslationObject=function(c){var d=c.split(a.objectNotation).reduce(function(a,b){return a&&a.hasOwnProperty(b)?a[b]:null},b.i18n);return d},this.__=function(a){if(!b.i18n)return a;var d=b.i18n[a]||this.getTranslationObject(a);return d||(d=a,b.i18n[a]=d,c.get("/i18n/"+this.userLanguage+"/"+encodeURIComponent(a)).then(function(c){b.i18n[a]=c.data})),arguments.length>1&&"function"==typeof vsprintf&&(d=vsprintf(d,Array.prototype.slice.call(arguments,1))),d},this.__n=function(a,d,e){if(!b.i18n)return d;var f=b.i18n[d]||this.getTranslationObject(d);if(!f){e||(e=d),f={one:d,other:e},b.i18n[d]=f;var g="/i18n/"+this.userLanguage+"/"+encodeURIComponent(d)+"?plural="+encodeURIComponent(e)+"&count="+encodeURIComponent(a);c.get(g).then(function(a){b.i18n[d]=a.data})}return f=1==a?f.one:f.other,"function"==typeof vsprintf&&(f=vsprintf(f,[a])),f}};return new e}]}),a.directive("i18n",["i18n","$rootScope",function(a,b){return{restrict:"A",link:function(c,d,e){function f(b,c){b=b||e.i18n,c=c||g(),d.text(void 0===c?a.__(b):a.__n(c,b))}function g(){var a=e.count;return a&&parseInt(a)}e.$observe("i18n",function(a){f(a)}),b.$on("LOCALE_UPDATED",function(){f()})}}}]),a.directive("i18nLocale",["i18n","$rootScope",function(a,b){return{restrict:"A",link:function(c,d,e){e.$observe("i18nLocale",function(b){a.init(b)}),b.$watch("i18nLocale",function(b,c){b&&b!=c&&a.init(b)})}}}]),a.filter("i18n",["i18n",function(a){function b(a){return!isNaN(parseFloat(a))&&isFinite(a)}var c=function(c){return arguments&&arguments.length&&"undefined"!=typeof arguments[0]?b(c)?a.__n.apply(a,arguments):a.__.apply(a,arguments):""};return c.$stateful=!0,c}])}();