grunt-nks-i18n
Version:
Builder translate you project
8 lines (7 loc) • 4.28 kB
JavaScript
/*
name: nks-i18n
version: 1.9.0
author: Konstantin Nizhinskiy
date: 2019-09-06 10:09:10
*/
!function(e,t){"function"==typeof define&&define.amd?define([],function(){return e.i18n=t()}):"object"==typeof module&&module.exports?module.exports=e.i18n=t():e.i18n=t()}(this,function(){function e(){this.setProperty({locale:"ua",localeDefault:"ua",versionJson:+new Date})}var t="ua",f="ua",l="",s={},u={};_modulePrefix={},e.prototype.changeLocale=function(e,t){e=e.toLowerCase();var o=[],a=this;for(var n in u)!0!==u[n][e]&&o.push(n);if(0<o.length){function l(){0===--r&&("function"==typeof t&&t(),a.trigger("changeLocale",e))}var r=o.length;c(e),o.forEach(function(e){a.load(e,l)})}else c(e),"function"==typeof t&&t(),this.trigger("changeLocale",e)};var n={};e.prototype.on=function(e,t){void 0===n[e]&&(n[e]={callback:[],callbackOnce:[]}),n[e].callback.push(t)},e.prototype.once=function(e,t){void 0===n[e]&&(n[e]={callback:[],callbackOnce:[]}),n[e].callbackOnce.push(t)},e.prototype.off=function(e,t){var o;if("function"==typeof t){if(n[e].callback&&0<n[e].callback.length){o=[];for(var a=0;a<n[e].callback.length;a++)"function"==typeof n[e].callback[a]&&n[e].callback[a]!==t&&o.push(n[e].callback[a]);n[e].callback=o}if(n[e].callbackOnce&&0<n[e].callbackOnce.length){o=[];for(a=0;a<n[e].callbackOnce.length;a++)"function"==typeof n[e].callbackOnce[a]&&n[e].callbackOnce[a]!==t&&o.push(n[e].callbackOnce[a]);n[e].callbackOnce=o}}else delete n[e]},e.prototype.trigger=function(e){if(n[e]){var t=[];for(var o in arguments)0!=o&&t.push(arguments[o]);if(n[e].callback&&0<n[e].callback.length)for(var a=0;a<n[e].callback.length;a++)"function"==typeof n[e].callback[a]&&n[e].callback[a].apply(this,t);if(n[e].callbackOnce&&0<n[e].callbackOnce.length){for(a=0;a<n[e].callbackOnce.length;a++)"function"==typeof n[e].callbackOnce[a]&&n[e].callbackOnce[a].apply(this,t);n[e].callbackOnce=[]}}},e.prototype.get=function(e,t,o){var a;if(s[this.getLocale()]&&s[this.getLocale()][e]){if(a=s[this.getLocale()][e],"object"==typeof t)for(var n in t)a=a.replace(new RegExp("{{[ ]{0,}"+n+"[ ]{0,}}}","g"),t[n]);return a}if(this.trigger("error","error:translation:key",e,this.getLocale()),this.trigger("error:translation:key",e,this.getLocale()),f&&s[f]&&s[f][e]){if(a=s[f][e],"object"==typeof t)for(var n in t)a=a.replace(new RegExp("{{[ ]{0,}"+n+"[ ]{0,}}}","g"),t[n]);return a}return o&&o.defaultValue?o.defaultValue:l||e},e.prototype.getByData=function(e,t){return e&&(e[this.getLocale()]||e[this.getLocale().toUpperCase()]||e[f])?e[this.getLocale()]||e[this.getLocale().toUpperCase()]||e[f]:t&&t.defaultValue?t.defaultValue:e},e.prototype.getLocale=function(){return t},e.prototype.load=function(e,t,l){var o,r,c=this.getLocale(),i=this;l=l||{},"string"==typeof e&&(e=[e]),o=e.length,"function"==typeof t&&(r=function(){0===--o&&t()}),e.forEach(function(o){if(l.modulePrefix&&_modulePrefix[l.modulePrefix]&&(o=_modulePrefix[l.modulePrefix]+o),void 0===u[o]&&(u[o]={}),void 0===u[o][f]&&c!==f){var a=new XMLHttpRequest;a.open("GET",o+"."+f.toLowerCase()+".json?"+i._versionJson,!0),a.send(),a.onreadystatechange=function(){if(4==a.readyState)if(200!=a.status)i.trigger("error","error:load",a.statusText,a.status,a),i.trigger("error:load",a.statusText,a.status,a);else{var e=JSON.parse(a.responseText);if(u[o][f]=!0,void 0===s[f])s[f]=e;else for(var t in e)s[f][t]||(s[f][t]=e[t])}}}if(void 0===u[o][c]){var n=new XMLHttpRequest;n.open("GET",o+"."+c.toLowerCase()+".json?"+i._versionJson,!0),n.send(),n.onreadystatechange=function(){if(4==n.readyState)if(200!=n.status)i.trigger("error","error:load",n.statusText,n.status,n),i.trigger("error:load",n.statusText,n.status,n),"function"==typeof r&&r();else{var e=JSON.parse(n.responseText);if(u[o][c]=!0,void 0===s[c])s[c]=e;else for(var t in e)s[c][t]||(s[c][t]=e[t]);i.trigger("load",o,u[o][c]),"function"==typeof r&&r()}}}else"function"==typeof r&&r()})};var c=function(e){t=e};return e.prototype.setProperty=function(e){e.locale&&(t=e.locale.toLowerCase()),e.localeDefault&&(f=e.localeDefault.toLowerCase()),e.versionJson&&(this._versionJson=e.versionJson),e.defaultValue&&(l=e.defaultValue),e.modulePrefix&&(_modulePrefix=e.modulePrefix)},e.prototype.setTranslation=function(e,t){if(s[t])for(var o in e)s[t][o]||(s[t][o]=e[o]);else s[t]=e},new e});