@pluginjs/i18n
Advanced tools
Comparing version 0.2.11 to 0.2.13
@@ -1,2 +0,2 @@ | ||
import { deepMerge, getValueByPath } from '@pluginjs/utils'; | ||
import { getValueByPath, deepMerge } from '@pluginjs/utils'; | ||
import template from '@pluginjs/template'; | ||
@@ -3,0 +3,0 @@ |
385
dist/i18n.js
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@pluginjs/utils'), require('@pluginjs/template')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@pluginjs/utils', '@pluginjs/template'], factory) : | ||
(factory((global['@pluginjs/i18n'] = {}),global['@pluginjs/utils'],global['@pluginjs/template'])); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@pluginjs/utils'), require('@pluginjs/template')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@pluginjs/utils', '@pluginjs/template'], factory) : | ||
(factory((global['@pluginjs/i18n'] = {}),global['@pluginjs/utils'],global['@pluginjs/template'])); | ||
}(this, (function (exports,utils,template) { 'use strict'; | ||
template = template && template.hasOwnProperty('default') ? template['default'] : template; | ||
template = template && template.hasOwnProperty('default') ? template['default'] : template; | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
} | ||
}(); | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
var I18N = function () { | ||
function I18N(defaults$$1, translations) { | ||
classCallCheck(this, I18N); | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
this.defaults = utils.deepMerge({}, I18N.defaults, defaults$$1); | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
this.translations = translations ? translations : {}; | ||
} | ||
}; | ||
}(); | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var I18N = function () { | ||
function I18N(defaults$$1, translations) { | ||
classCallCheck(this, I18N); | ||
this.defaults = utils.deepMerge({}, I18N.defaults, defaults$$1); | ||
this.translations = translations ? translations : {}; | ||
} | ||
createClass(I18N, [{ | ||
key: 'hasTranslation', | ||
value: function hasTranslation(locale) { | ||
return locale in this.translations; | ||
} | ||
}, { | ||
key: 'addTranslation', | ||
value: function addTranslation(locale, translation) { | ||
if (this.translations[locale]) { | ||
Object.assign(this.translations[locale], translation); | ||
} else { | ||
this.translations[locale] = translation; | ||
createClass(I18N, [{ | ||
key: 'hasTranslation', | ||
value: function hasTranslation(locale) { | ||
return locale in this.translations; | ||
} | ||
} | ||
}, { | ||
key: 'getTranslation', | ||
value: function getTranslation(locale) { | ||
if (this.translations[locale]) { | ||
return this.translations[locale]; | ||
}, { | ||
key: 'addTranslation', | ||
value: function addTranslation(locale, translation) { | ||
if (this.translations[locale]) { | ||
Object.assign(this.translations[locale], translation); | ||
} else { | ||
this.translations[locale] = translation; | ||
} | ||
} | ||
return {}; | ||
} | ||
}, { | ||
key: 'instance', | ||
value: function instance() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
}, { | ||
key: 'getTranslation', | ||
value: function getTranslation(locale) { | ||
if (this.translations[locale]) { | ||
return this.translations[locale]; | ||
} | ||
return {}; | ||
} | ||
}, { | ||
key: 'instance', | ||
value: function instance() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var that = this; | ||
var that = this; | ||
var _options = Object.assign({}, that.defaults, options); | ||
var _locale = _options.locale; | ||
var _options = Object.assign({}, that.defaults, options); | ||
var _locale = _options.locale; | ||
function getMessage(key, locale) { | ||
var translation = that.getTranslation(locale); | ||
var message = utils.getValueByPath(translation, key); | ||
function getMessage(key, locale) { | ||
var translation = that.getTranslation(locale); | ||
var message = utils.getValueByPath(translation, key); | ||
return message; | ||
} | ||
return message; | ||
} | ||
return { | ||
translate: function translate(key) { | ||
var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _locale; | ||
return { | ||
translate: function translate(key) { | ||
var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _locale; | ||
var message = getMessage(key, locale); | ||
var message = getMessage(key, locale); | ||
if (message === undefined && _options.fallbacks) { | ||
var locales = locale.split('-'); | ||
if (locales.length > 1 && that.hasTranslation(locales[0])) { | ||
message = getMessage(key, locales[0]); | ||
} | ||
if (message === undefined && _options.fallbacks) { | ||
var locales = locale.split('-'); | ||
if (locales.length > 1 && that.hasTranslation(locales[0])) { | ||
message = getMessage(key, locales[0]); | ||
} | ||
if (message === undefined) { | ||
var fallbackLocale = void 0; | ||
if (_options.fallbacks !== true && that.hasTranslation(_options.fallbacks)) { | ||
fallbackLocale = _options.fallbacks; | ||
} else { | ||
fallbackLocale = that.defaults.locale; | ||
if (message === undefined) { | ||
var fallbackLocale = void 0; | ||
if (_options.fallbacks !== true && that.hasTranslation(_options.fallbacks)) { | ||
fallbackLocale = _options.fallbacks; | ||
} else { | ||
fallbackLocale = that.defaults.locale; | ||
} | ||
message = getMessage(key, fallbackLocale); | ||
} | ||
message = getMessage(key, fallbackLocale); | ||
} | ||
} | ||
if (Object.prototype.toString.call(message) === '[object Array]' && message.length >= 2) { | ||
if (typeof args._number === 'string') { | ||
if (typeof args[args._number] !== 'undefined') { | ||
var _number = parseInt(args[args._number], 10); | ||
if (Object.prototype.toString.call(message) === '[object Array]' && message.length >= 2) { | ||
if (typeof args._number === 'string') { | ||
if (typeof args[args._number] !== 'undefined') { | ||
var _number = parseInt(args[args._number], 10); | ||
if (_number === 1) { | ||
message = message[0]; | ||
} else if (_number > 1) { | ||
message = message[1]; | ||
} else if (_number === 0 && message.length >= 3) { | ||
message = message[2]; | ||
if (_number === 1) { | ||
message = message[0]; | ||
} else if (_number > 1) { | ||
message = message[1]; | ||
} else if (_number === 0 && message.length >= 3) { | ||
message = message[2]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (typeof message === 'string') { | ||
var parsed = template.parse(message); | ||
if (!parsed) { | ||
return message; | ||
} | ||
var _key = void 0; | ||
for (var i = 0; i < parsed.length; i++) { | ||
_key = parsed[i]; | ||
if (typeof args[_key] === 'undefined') { | ||
args[_key] = _options.missingPlaceholder(_key); | ||
} else if (args[_key] === null) { | ||
args[_key] = _options.nullPlaceholder(_key); | ||
if (typeof message === 'string') { | ||
var parsed = template.parse(message); | ||
if (!parsed) { | ||
return message; | ||
} | ||
var _key = void 0; | ||
for (var i = 0; i < parsed.length; i++) { | ||
_key = parsed[i]; | ||
if (typeof args[_key] === 'undefined') { | ||
args[_key] = _options.missingPlaceholder(_key); | ||
} else if (args[_key] === null) { | ||
args[_key] = _options.nullPlaceholder(_key); | ||
} | ||
} | ||
return template.render(message, args); | ||
} | ||
return template.render(message, args); | ||
} | ||
if (Object(message) === message) { | ||
return message; | ||
if (Object(message) === message) { | ||
return message; | ||
} | ||
return '[missing "' + locale + '.' + key + '" translation]'; | ||
}, | ||
setLocale: function setLocale(locale) { | ||
_locale = locale; | ||
}, | ||
getLocale: function getLocale() { | ||
return _locale; | ||
} | ||
}; | ||
} | ||
}, { | ||
key: 'setTranslations', | ||
value: function setTranslations(translations) { | ||
this.translations = translations; | ||
} | ||
}]); | ||
return I18N; | ||
}(); | ||
return '[missing "' + locale + '.' + key + '" translation]'; | ||
}, | ||
setLocale: function setLocale(locale) { | ||
_locale = locale; | ||
}, | ||
getLocale: function getLocale() { | ||
return _locale; | ||
} | ||
}; | ||
I18N.defaults = { | ||
locale: 'en', | ||
fallbacks: true, | ||
nullPlaceholder: function nullPlaceholder(key) { | ||
return '[missing {{' + key + '}} value]'; | ||
}, | ||
missingPlaceholder: function missingPlaceholder(key) { | ||
return '[missing {{' + key + '}} value]'; | ||
} | ||
}, { | ||
key: 'setTranslations', | ||
value: function setTranslations(translations) { | ||
this.translations = translations; | ||
} | ||
}]); | ||
return I18N; | ||
}(); | ||
}; | ||
I18N.defaults = { | ||
locale: 'en', | ||
fallbacks: true, | ||
nullPlaceholder: function nullPlaceholder(key) { | ||
return '[missing {{' + key + '}} value]'; | ||
}, | ||
missingPlaceholder: function missingPlaceholder(key) { | ||
return '[missing {{' + key + '}} value]'; | ||
} | ||
}; | ||
exports.default = I18N; | ||
exports['default'] = I18N; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
@@ -1,1 +0,1 @@ | ||
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("@pluginjs/utils"),require("@pluginjs/template")):"function"==typeof define&&define.amd?define(["exports","@pluginjs/utils","@pluginjs/template"],n):n(t["@pluginjs/i18n"]={},t["@pluginjs/utils"],t["@pluginjs/template"])}(this,function(t,n,e){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;!function(){function t(t){this.value=t}function n(n){function e(i,a){try{var o=n[i](a),l=o.value;l instanceof t?Promise.resolve(l.value).then(function(t){e("next",t)},function(t){e("throw",t)}):r(o.done?"return":"normal",o.value)}catch(t){r("throw",t)}}function r(t,n){switch(t){case"return":i.resolve({value:n,done:!0});break;case"throw":i.reject(n);break;default:i.resolve({value:n,done:!1})}(i=i.next)?e(i.key,i.arg):a=null}var i,a;this._invoke=function(t,n){return new Promise(function(r,o){var l={key:t,arg:n,resolve:r,reject:o,next:null};a?a=a.next=l:(i=a=l,e(t,n))})},"function"!=typeof n.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(n.prototype[Symbol.asyncIterator]=function(){return this}),n.prototype.next=function(t){return this._invoke("next",t)},n.prototype.throw=function(t){return this._invoke("throw",t)},n.prototype.return=function(t){return this._invoke("return",t)}}();var r=function(t,n){if(!(t instanceof n))throw new TypeError("Cannot call a class as a function")},i=function(){function t(t,n){for(var e=0;e<n.length;e++){var r=n[e];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(n,e,r){return e&&t(n.prototype,e),r&&t(n,r),n}}(),a=function(){function t(e,i){r(this,t),this.defaults=n.deepMerge({},t.defaults,e),this.translations=i||{}}return i(t,[{key:"hasTranslation",value:function(t){return t in this.translations}},{key:"addTranslation",value:function(t,n){this.translations[t]?Object.assign(this.translations[t],n):this.translations[t]=n}},{key:"getTranslation",value:function(t){return this.translations[t]?this.translations[t]:{}}},{key:"instance",value:function(){function t(t,e){var r=i.getTranslation(e);return n.getValueByPath(r,t)}var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},i=this,a=Object.assign({},i.defaults,r),o=a.locale;return{translate:function(n){var r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},l=arguments.length>2&&void 0!==arguments[2]?arguments[2]:o,s=t(n,l);if(void 0===s&&a.fallbacks){var u=l.split("-");if(u.length>1&&i.hasTranslation(u[0])&&(s=t(n,u[0])),void 0===s){s=t(n,!0!==a.fallbacks&&i.hasTranslation(a.fallbacks)?a.fallbacks:i.defaults.locale)}}if("[object Array]"===Object.prototype.toString.call(s)&&s.length>=2&&"string"==typeof r._number&&void 0!==r[r._number]){var c=parseInt(r[r._number],10);1===c?s=s[0]:c>1?s=s[1]:0===c&&s.length>=3&&(s=s[2])}if("string"==typeof s){var f=e.parse(s);if(!f)return s;for(var h=void 0,v=0;v<f.length;v++)void 0===r[h=f[v]]?r[h]=a.missingPlaceholder(h):null===r[h]&&(r[h]=a.nullPlaceholder(h));return e.render(s,r)}return Object(s)===s?s:'[missing "'+l+"."+n+'" translation]'},setLocale:function(t){o=t},getLocale:function(){return o}}}},{key:"setTranslations",value:function(t){this.translations=t}}]),t}();a.defaults={locale:"en",fallbacks:!0,nullPlaceholder:function(t){return"[missing {{"+t+"}} value]"},missingPlaceholder:function(t){return"[missing {{"+t+"}} value]"}},t.default=a,Object.defineProperty(t,"__esModule",{value:!0})}); | ||
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@pluginjs/utils"),require("@pluginjs/template")):"function"==typeof define&&define.amd?define(["exports","@pluginjs/utils","@pluginjs/template"],t):t(n["@pluginjs/i18n"]={},n["@pluginjs/utils"],n["@pluginjs/template"])}(this,function(n,t,e){"use strict";e=e&&e.hasOwnProperty("default")?e.default:e;var a=function(n,t){if(!(n instanceof t))throw new TypeError("Cannot call a class as a function")},i=function(){function n(n,t){for(var e=0;e<t.length;e++){var a=t[e];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(n,a.key,a)}}return function(t,e,a){return e&&n(t.prototype,e),a&&n(t,a),t}}(),l=function(){function n(e,i){a(this,n),this.defaults=t.deepMerge({},n.defaults,e),this.translations=i||{}}return i(n,[{key:"hasTranslation",value:function(n){return n in this.translations}},{key:"addTranslation",value:function(n,t){this.translations[n]?Object.assign(this.translations[n],t):this.translations[n]=t}},{key:"getTranslation",value:function(n){return this.translations[n]?this.translations[n]:{}}},{key:"instance",value:function(){var n=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},a=this,i=Object.assign({},a.defaults,n),l=i.locale;function r(n,e){var i=a.getTranslation(e);return t.getValueByPath(i,n)}return{translate:function(n){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},s=arguments.length>2&&void 0!==arguments[2]?arguments[2]:l,o=r(n,s);if(void 0===o&&i.fallbacks){var u=s.split("-");if(u.length>1&&a.hasTranslation(u[0])&&(o=r(n,u[0])),void 0===o){o=r(n,!0!==i.fallbacks&&a.hasTranslation(i.fallbacks)?i.fallbacks:a.defaults.locale)}}if("[object Array]"===Object.prototype.toString.call(o)&&o.length>=2&&"string"==typeof t._number&&void 0!==t[t._number]){var f=parseInt(t[t._number],10);1===f?o=o[0]:f>1?o=o[1]:0===f&&o.length>=3&&(o=o[2])}if("string"==typeof o){var c=e.parse(o);if(!c)return o;for(var d=void 0,g=0;g<c.length;g++)void 0===t[d=c[g]]?t[d]=i.missingPlaceholder(d):null===t[d]&&(t[d]=i.nullPlaceholder(d));return e.render(o,t)}return Object(o)===o?o:'[missing "'+s+"."+n+'" translation]'},setLocale:function(n){l=n},getLocale:function(){return l}}}},{key:"setTranslations",value:function(n){this.translations=n}}]),n}();l.defaults={locale:"en",fallbacks:!0,nullPlaceholder:function(n){return"[missing {{"+n+"}} value]"},missingPlaceholder:function(n){return"[missing {{"+n+"}} value]"}},n.default=l,Object.defineProperty(n,"__esModule",{value:!0})}); |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global['@pluginjs/i18n'] = {}))); | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global['@pluginjs/i18n'] = {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { | ||
return typeof obj; | ||
} : function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
var defineProperty = function (obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
var asyncGenerator$1 = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
return obj; | ||
}; | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
var slicedToArray = function () { | ||
function sliceIterator(arr, i) { | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"]) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
} | ||
return _arr; | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
return function (arr, i) { | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} else if (Symbol.iterator in Object(arr)) { | ||
return sliceIterator(arr, i); | ||
} else { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
} | ||
}; | ||
}(); | ||
front = front.next; | ||
var _extends$1 = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
this._invoke = send; | ||
return target; | ||
}; | ||
function nub(arr) { | ||
return Array.from(new Set(arr)); | ||
} | ||
function isPlainObject(data) { | ||
if ((typeof data === 'undefined' ? 'undefined' : _typeof(data)) !== 'object') { | ||
return false; | ||
} | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
if (data === null) { | ||
return false; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
} | ||
if (data instanceof Set || data instanceof Map) { | ||
return false; | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function (fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function (value) { | ||
return new AwaitValue(value); | ||
if (Array.isArray(data)) { | ||
return false; | ||
} | ||
}; | ||
}(); | ||
var classCallCheck = function (instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
return true; | ||
} | ||
}; | ||
var createClass = function () { | ||
function defineProperties(target, props) { | ||
for (var i = 0; i < props.length; i++) { | ||
var descriptor = props[i]; | ||
descriptor.enumerable = descriptor.enumerable || false; | ||
descriptor.configurable = true; | ||
if ("value" in descriptor) descriptor.writable = true; | ||
Object.defineProperty(target, descriptor.key, descriptor); | ||
function deepMergeTwo(x, y) { | ||
if (isPlainObject(y) && isPlainObject(x) || isPlainObject(x) && Array.isArray(y)) { | ||
return fromPairs$1(nub(Object.keys(x).concat(Object.keys(y))).map(function (key) { | ||
return [key, deepMergeTwo(x[key], y[key])]; | ||
})); | ||
} | ||
} | ||
return function (Constructor, protoProps, staticProps) { | ||
if (protoProps) defineProperties(Constructor.prototype, protoProps); | ||
if (staticProps) defineProperties(Constructor, staticProps); | ||
return Constructor; | ||
}; | ||
}(); | ||
if (isPlainObject(y) && typeof x === 'function') { | ||
return Object.assign(function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
var defineProperty = function (obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
}; | ||
var slicedToArray = function () { | ||
function sliceIterator(arr, i) { | ||
var _arr = []; | ||
var _n = true; | ||
var _d = false; | ||
var _e = undefined; | ||
try { | ||
for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { | ||
_arr.push(_s.value); | ||
if (i && _arr.length === i) break; | ||
} | ||
} catch (err) { | ||
_d = true; | ||
_e = err; | ||
} finally { | ||
try { | ||
if (!_n && _i["return"]) _i["return"](); | ||
} finally { | ||
if (_d) throw _e; | ||
} | ||
return x.apply(this, args); | ||
}, y); | ||
} | ||
return _arr; | ||
} | ||
return function (arr, i) { | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} else if (Symbol.iterator in Object(arr)) { | ||
return sliceIterator(arr, i); | ||
} else { | ||
throw new TypeError("Invalid attempt to destructure non-iterable instance"); | ||
if (isPlainObject(y) && Array.isArray(x)) { | ||
return Object.assign([], x, y); | ||
} | ||
}; | ||
}(); | ||
var asyncGenerator = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
}; | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
if (isPlainObject(x) && typeof y === 'function') { | ||
return Object.assign(function () { | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
return y.apply(this, args); | ||
}, x); | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
if (Array.isArray(y) && Array.isArray(x)) { | ||
// return x.concat(y) | ||
return nub(Object.keys(y).concat(Object.keys(x))).map(function (index) { | ||
return deepMergeTwo(x[index], y[index]); | ||
}); | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
if (typeof y === 'undefined') { | ||
return x; | ||
} | ||
return y; | ||
} | ||
function isObject(obj) { | ||
return Object(obj) === obj; | ||
} | ||
function deepMerge() { | ||
for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { | ||
args[_key4] = arguments[_key4]; | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
return args.filter(isObject).reduce(deepMergeTwo); | ||
} | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
/* Credit to https://github.com/jonschlinkert/get-value MIT */ | ||
function getValueByPath(obj, path) { | ||
if (Object(obj) !== obj || typeof path === 'undefined') { | ||
return obj; | ||
} | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
if (path in obj) { | ||
return obj[path]; | ||
} | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function wrap(fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function _await(value) { | ||
return new AwaitValue(value); | ||
var segs = path.split('.'); | ||
var length = segs.length; | ||
if (!length) { | ||
return undefined; | ||
} | ||
}; | ||
}(); | ||
var i = -1; | ||
var _extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
while (obj && ++i < length) { | ||
var key = segs[i]; | ||
while (key[key.length - 1] === '\\') { | ||
key = key.slice(0, -1) + '.' + segs[++i]; | ||
} | ||
obj = obj[key]; | ||
} | ||
return obj; | ||
} | ||
return target; | ||
}; | ||
function fromPairs$1(arr) { | ||
return arr.reduce(function (r, _ref10) { | ||
var _ref11 = slicedToArray(_ref10, 2), | ||
k = _ref11[0], | ||
v = _ref11[1]; | ||
function nub(arr) { | ||
return Array.from(new Set(arr)); | ||
} | ||
function isPaintObject(data) { | ||
if ((typeof data === "undefined" ? "undefined" : _typeof(data)) !== 'object') { | ||
return false; | ||
return _extends$1({}, r, defineProperty({}, k, v)); | ||
}, {}); | ||
} | ||
if (data === null) { | ||
return false; | ||
} | ||
/* Credit to https://github.com/jonschlinkert/get-value MIT */ | ||
function getValueByPath$1(obj, path) { | ||
if (Object(obj) !== obj || typeof path === 'undefined') { | ||
return obj; | ||
} | ||
if (data instanceof Set || data instanceof Map) { | ||
return false; | ||
} | ||
if (path in obj) { | ||
return obj[path]; | ||
} | ||
if (Array.isArray(data)) { | ||
return false; | ||
} | ||
var segs = path.split('.'); | ||
var length = segs.length; | ||
if (!length) { | ||
return undefined; | ||
} | ||
var i = -1; | ||
return true; | ||
} | ||
function deepMergeTwo(x, y) { | ||
if (isPaintObject(y) && isPaintObject(x) || isPaintObject(x) && Array.isArray(y)) { | ||
return fromPairs(nub(Object.keys(x).concat(Object.keys(y))).map(function (key) { | ||
return [key, deepMergeTwo(x[key], y[key])]; | ||
})); | ||
} | ||
if (isPaintObject(y) && typeof x === 'function') { | ||
return Object.assign(function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
while (obj && ++i < length) { | ||
var key = segs[i]; | ||
while (key[key.length - 1] === '\\') { | ||
key = key.slice(0, -1) + '.' + segs[++i]; | ||
} | ||
return x.apply(this, args); | ||
}, y); | ||
obj = obj[key]; | ||
} | ||
return obj; | ||
} | ||
if (isPaintObject(y) && Array.isArray(x)) { | ||
return Object.assign([], x, y); | ||
} | ||
/* Credit to https://github.com/Matt-Esch/string-template MIT */ | ||
var template = function () { | ||
var pattern = /\{\s*([.0-9a-zA-Z_]+)\s*\}/g; | ||
if (isPaintObject(x) && typeof y === 'function') { | ||
return Object.assign(function () { | ||
for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
args[_key3] = arguments[_key3]; | ||
function render(string) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
return y.apply(this, args); | ||
}, x); | ||
} | ||
if (args.length === 1 && _typeof(args[0]) === 'object') { | ||
args = args[0]; | ||
} | ||
if (Array.isArray(y) && Array.isArray(x)) { | ||
// return x.concat(y) | ||
return nub(Object.keys(y).concat(Object.keys(x))).map(function (index) { | ||
return deepMergeTwo(x[index], y[index]); | ||
}); | ||
} | ||
if (!args || !args.hasOwnProperty) { | ||
args = {}; | ||
} | ||
if (typeof y === 'undefined') { | ||
return x; | ||
} | ||
return y; | ||
} | ||
function isObject(obj) { | ||
return Object(obj) === obj; | ||
} | ||
function deepMerge() { | ||
for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { | ||
args[_key4] = arguments[_key4]; | ||
} | ||
return string.replace(pattern, function (match, i, index) { | ||
var result = null; | ||
return args.filter(isObject).reduce(deepMergeTwo); | ||
} | ||
if (string[index - 1] === '{' && string[index + match.length] === '}') { | ||
return i; | ||
} | ||
/* Credit to https://github.com/jonschlinkert/get-value MIT */ | ||
function getValueByPath(obj, path) { | ||
if (Object(obj) !== obj || typeof path === 'undefined') { | ||
return obj; | ||
} | ||
if (args.hasOwnProperty(i)) { | ||
result = args[i]; | ||
} else if (i.indexOf('.') !== -1) { | ||
result = getValueByPath$1(args, i); | ||
} | ||
if (path in obj) { | ||
return obj[path]; | ||
} | ||
if (result === null || result === undefined) { | ||
return ''; | ||
} | ||
var segs = path.split('.'); | ||
var length = segs.length; | ||
if (!length) { | ||
return undefined; | ||
} | ||
var i = -1; | ||
while (obj && ++i < length) { | ||
var key = segs[i]; | ||
while (key[key.length - 1] === '\\') { | ||
key = key.slice(0, -1) + "." + segs[++i]; | ||
return result; | ||
}); | ||
} | ||
obj = obj[key]; | ||
} | ||
return obj; | ||
} | ||
function fromPairs(arr) { | ||
return arr.reduce(function (r, _ref10) { | ||
var _ref11 = slicedToArray(_ref10, 2), | ||
k = _ref11[0], | ||
v = _ref11[1]; | ||
return { | ||
render: render, | ||
compile: function compile(str) { | ||
return function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
} | ||
return _extends({}, r, defineProperty({}, k, v)); | ||
}, {}); | ||
} | ||
var asyncGenerator$2 = function () { | ||
function AwaitValue(value) { | ||
this.value = value; | ||
} | ||
function AsyncGenerator(gen) { | ||
var front, back; | ||
function send(key, arg) { | ||
return new Promise(function (resolve, reject) { | ||
var request = { | ||
key: key, | ||
arg: arg, | ||
resolve: resolve, | ||
reject: reject, | ||
next: null | ||
return render.apply(undefined, [str].concat(args)); | ||
}; | ||
}, | ||
parse: function parse(str) { | ||
var matches = str.match(pattern); | ||
if (back) { | ||
back = back.next = request; | ||
} else { | ||
front = back = request; | ||
resume(key, arg); | ||
if (matches === null) { | ||
return false; | ||
} | ||
}); | ||
} | ||
function resume(key, arg) { | ||
try { | ||
var result = gen[key](arg); | ||
var value = result.value; | ||
if (value instanceof AwaitValue) { | ||
Promise.resolve(value.value).then(function (arg) { | ||
resume("next", arg); | ||
}, function (arg) { | ||
resume("throw", arg); | ||
}); | ||
} else { | ||
settle(result.done ? "return" : "normal", result.value); | ||
var parsed = []; | ||
for (var i = 0; i < matches.length; i++) { | ||
if (!matches[i].match(/^\{\{.+\}\}$/g)) { | ||
parsed.push(matches[i].substring(1, matches[i].length - 1).trim()); | ||
} | ||
} | ||
} catch (err) { | ||
settle("throw", err); | ||
} | ||
} | ||
function settle(type, value) { | ||
switch (type) { | ||
case "return": | ||
front.resolve({ | ||
value: value, | ||
done: true | ||
}); | ||
break; | ||
case "throw": | ||
front.reject(value); | ||
break; | ||
default: | ||
front.resolve({ | ||
value: value, | ||
done: false | ||
}); | ||
break; | ||
return parsed; | ||
} | ||
front = front.next; | ||
if (front) { | ||
resume(front.key, front.arg); | ||
} else { | ||
back = null; | ||
} | ||
} | ||
this._invoke = send; | ||
if (typeof gen.return !== "function") { | ||
this.return = undefined; | ||
} | ||
} | ||
if (typeof Symbol === "function" && Symbol.asyncIterator) { | ||
AsyncGenerator.prototype[Symbol.asyncIterator] = function () { | ||
return this; | ||
}; | ||
} | ||
}(); | ||
AsyncGenerator.prototype.next = function (arg) { | ||
return this._invoke("next", arg); | ||
}; | ||
var I18N = function () { | ||
function I18N(defaults$$1, translations) { | ||
classCallCheck(this, I18N); | ||
AsyncGenerator.prototype.throw = function (arg) { | ||
return this._invoke("throw", arg); | ||
}; | ||
this.defaults = deepMerge({}, I18N.defaults, defaults$$1); | ||
AsyncGenerator.prototype.return = function (arg) { | ||
return this._invoke("return", arg); | ||
}; | ||
return { | ||
wrap: function wrap(fn) { | ||
return function () { | ||
return new AsyncGenerator(fn.apply(this, arguments)); | ||
}; | ||
}, | ||
await: function _await(value) { | ||
return new AwaitValue(value); | ||
this.translations = translations ? translations : {}; | ||
} | ||
}; | ||
}(); | ||
/* Credit to https://github.com/jonschlinkert/get-value MIT */ | ||
function getValueByPath$1(obj, path) { | ||
if (Object(obj) !== obj || typeof path === 'undefined') { | ||
return obj; | ||
} | ||
if (path in obj) { | ||
return obj[path]; | ||
} | ||
var segs = path.split('.'); | ||
var length = segs.length; | ||
if (!length) { | ||
return undefined; | ||
} | ||
var i = -1; | ||
while (obj && ++i < length) { | ||
var key = segs[i]; | ||
while (key[key.length - 1] === '\\') { | ||
key = key.slice(0, -1) + "." + segs[++i]; | ||
} | ||
obj = obj[key]; | ||
} | ||
return obj; | ||
} | ||
/* Credit to https://github.com/Matt-Esch/string-template MIT */ | ||
var template = function () { | ||
var pattern = /\{\s*([.0-9a-zA-Z_]+)\s*\}/g; | ||
function render(string) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
if (args.length === 1 && _typeof(args[0]) === 'object') { | ||
args = args[0]; | ||
} | ||
if (!args || !args.hasOwnProperty) { | ||
args = {}; | ||
} | ||
return string.replace(pattern, function (match, i, index) { | ||
var result = null; | ||
if (string[index - 1] === '{' && string[index + match.length] === '}') { | ||
return i; | ||
createClass(I18N, [{ | ||
key: 'hasTranslation', | ||
value: function hasTranslation(locale) { | ||
return locale in this.translations; | ||
} | ||
if (args.hasOwnProperty(i)) { | ||
result = args[i]; | ||
} else if (i.indexOf('.') !== -1) { | ||
result = getValueByPath$1(args, i); | ||
} | ||
if (result === null || result === undefined) { | ||
return ''; | ||
} | ||
return result; | ||
}); | ||
} | ||
return { | ||
render: render, | ||
compile: function compile(str) { | ||
return function () { | ||
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
args[_key2] = arguments[_key2]; | ||
}, { | ||
key: 'addTranslation', | ||
value: function addTranslation(locale, translation) { | ||
if (this.translations[locale]) { | ||
Object.assign(this.translations[locale], translation); | ||
} else { | ||
this.translations[locale] = translation; | ||
} | ||
return render.apply(undefined, [str].concat(args)); | ||
}; | ||
}, | ||
parse: function parse(str) { | ||
var matches = str.match(pattern); | ||
if (matches === null) { | ||
return false; | ||
} | ||
var parsed = []; | ||
for (var i = 0; i < matches.length; i++) { | ||
if (!matches[i].match(/^\{\{.+\}\}$/g)) { | ||
parsed.push(matches[i].substring(1, matches[i].length - 1).trim()); | ||
}, { | ||
key: 'getTranslation', | ||
value: function getTranslation(locale) { | ||
if (this.translations[locale]) { | ||
return this.translations[locale]; | ||
} | ||
return {}; | ||
} | ||
}, { | ||
key: 'instance', | ||
value: function instance() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return parsed; | ||
} | ||
}; | ||
}(); | ||
var that = this; | ||
var I18N = function () { | ||
function I18N(defaults$$1, translations) { | ||
classCallCheck(this, I18N); | ||
var _options = Object.assign({}, that.defaults, options); | ||
var _locale = _options.locale; | ||
this.defaults = deepMerge({}, I18N.defaults, defaults$$1); | ||
function getMessage(key, locale) { | ||
var translation = that.getTranslation(locale); | ||
var message = getValueByPath(translation, key); | ||
this.translations = translations ? translations : {}; | ||
} | ||
return message; | ||
} | ||
createClass(I18N, [{ | ||
key: 'hasTranslation', | ||
value: function hasTranslation(locale) { | ||
return locale in this.translations; | ||
} | ||
}, { | ||
key: 'addTranslation', | ||
value: function addTranslation(locale, translation) { | ||
if (this.translations[locale]) { | ||
Object.assign(this.translations[locale], translation); | ||
} else { | ||
this.translations[locale] = translation; | ||
} | ||
} | ||
}, { | ||
key: 'getTranslation', | ||
value: function getTranslation(locale) { | ||
if (this.translations[locale]) { | ||
return this.translations[locale]; | ||
} | ||
return {}; | ||
} | ||
}, { | ||
key: 'instance', | ||
value: function instance() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return { | ||
translate: function translate(key) { | ||
var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _locale; | ||
var that = this; | ||
var message = getMessage(key, locale); | ||
var _options = Object.assign({}, that.defaults, options); | ||
var _locale = _options.locale; | ||
if (message === undefined && _options.fallbacks) { | ||
var locales = locale.split('-'); | ||
if (locales.length > 1 && that.hasTranslation(locales[0])) { | ||
message = getMessage(key, locales[0]); | ||
} | ||
function getMessage(key, locale) { | ||
var translation = that.getTranslation(locale); | ||
var message = getValueByPath(translation, key); | ||
if (message === undefined) { | ||
var fallbackLocale = void 0; | ||
if (_options.fallbacks !== true && that.hasTranslation(_options.fallbacks)) { | ||
fallbackLocale = _options.fallbacks; | ||
} else { | ||
fallbackLocale = that.defaults.locale; | ||
} | ||
return message; | ||
} | ||
message = getMessage(key, fallbackLocale); | ||
} | ||
} | ||
return { | ||
translate: function translate(key) { | ||
var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var locale = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _locale; | ||
if (Object.prototype.toString.call(message) === '[object Array]' && message.length >= 2) { | ||
if (typeof args._number === 'string') { | ||
if (typeof args[args._number] !== 'undefined') { | ||
var _number = parseInt(args[args._number], 10); | ||
var message = getMessage(key, locale); | ||
if (message === undefined && _options.fallbacks) { | ||
var locales = locale.split('-'); | ||
if (locales.length > 1 && that.hasTranslation(locales[0])) { | ||
message = getMessage(key, locales[0]); | ||
if (_number === 1) { | ||
message = message[0]; | ||
} else if (_number > 1) { | ||
message = message[1]; | ||
} else if (_number === 0 && message.length >= 3) { | ||
message = message[2]; | ||
} | ||
} | ||
} | ||
} | ||
if (message === undefined) { | ||
var fallbackLocale = void 0; | ||
if (_options.fallbacks !== true && that.hasTranslation(_options.fallbacks)) { | ||
fallbackLocale = _options.fallbacks; | ||
} else { | ||
fallbackLocale = that.defaults.locale; | ||
if (typeof message === 'string') { | ||
var parsed = template.parse(message); | ||
if (!parsed) { | ||
return message; | ||
} | ||
message = getMessage(key, fallbackLocale); | ||
} | ||
} | ||
if (Object.prototype.toString.call(message) === '[object Array]' && message.length >= 2) { | ||
if (typeof args._number === 'string') { | ||
if (typeof args[args._number] !== 'undefined') { | ||
var _number = parseInt(args[args._number], 10); | ||
if (_number === 1) { | ||
message = message[0]; | ||
} else if (_number > 1) { | ||
message = message[1]; | ||
} else if (_number === 0 && message.length >= 3) { | ||
message = message[2]; | ||
var _key = void 0; | ||
for (var i = 0; i < parsed.length; i++) { | ||
_key = parsed[i]; | ||
if (typeof args[_key] === 'undefined') { | ||
args[_key] = _options.missingPlaceholder(_key); | ||
} else if (args[_key] === null) { | ||
args[_key] = _options.nullPlaceholder(_key); | ||
} | ||
} | ||
return template.render(message, args); | ||
} | ||
} | ||
if (typeof message === 'string') { | ||
var parsed = template.parse(message); | ||
if (!parsed) { | ||
if (Object(message) === message) { | ||
return message; | ||
} | ||
var _key = void 0; | ||
for (var i = 0; i < parsed.length; i++) { | ||
_key = parsed[i]; | ||
if (typeof args[_key] === 'undefined') { | ||
args[_key] = _options.missingPlaceholder(_key); | ||
} else if (args[_key] === null) { | ||
args[_key] = _options.nullPlaceholder(_key); | ||
} | ||
} | ||
return template.render(message, args); | ||
} | ||
if (Object(message) === message) { | ||
return message; | ||
return '[missing "' + locale + '.' + key + '" translation]'; | ||
}, | ||
setLocale: function setLocale(locale) { | ||
_locale = locale; | ||
}, | ||
getLocale: function getLocale() { | ||
return _locale; | ||
} | ||
}; | ||
} | ||
}, { | ||
key: 'setTranslations', | ||
value: function setTranslations(translations) { | ||
this.translations = translations; | ||
} | ||
}]); | ||
return I18N; | ||
}(); | ||
return '[missing "' + locale + '.' + key + '" translation]'; | ||
}, | ||
setLocale: function setLocale(locale) { | ||
_locale = locale; | ||
}, | ||
getLocale: function getLocale() { | ||
return _locale; | ||
} | ||
}; | ||
I18N.defaults = { | ||
locale: 'en', | ||
fallbacks: true, | ||
nullPlaceholder: function nullPlaceholder(key) { | ||
return '[missing {{' + key + '}} value]'; | ||
}, | ||
missingPlaceholder: function missingPlaceholder(key) { | ||
return '[missing {{' + key + '}} value]'; | ||
} | ||
}, { | ||
key: 'setTranslations', | ||
value: function setTranslations(translations) { | ||
this.translations = translations; | ||
} | ||
}]); | ||
return I18N; | ||
}(); | ||
}; | ||
I18N.defaults = { | ||
locale: 'en', | ||
fallbacks: true, | ||
nullPlaceholder: function nullPlaceholder(key) { | ||
return '[missing {{' + key + '}} value]'; | ||
}, | ||
missingPlaceholder: function missingPlaceholder(key) { | ||
return '[missing {{' + key + '}} value]'; | ||
} | ||
}; | ||
exports.default = I18N; | ||
exports['default'] = I18N; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
}))); |
{ | ||
"name": "@pluginjs/i18n", | ||
"title": "Plugin", | ||
"version": "0.2.11", | ||
"version": "0.2.13", | ||
"description": "A workflow for modern frontend development.", | ||
@@ -34,4 +34,4 @@ "author": "Creation Studio Limited", | ||
"dependencies": { | ||
"@pluginjs/template": "^0.2.11", | ||
"@pluginjs/utils": "^0.2.11" | ||
"@pluginjs/template": "^0.2.13", | ||
"@pluginjs/utils": "^0.2.13" | ||
}, | ||
@@ -38,0 +38,0 @@ "category": "core", |
@@ -6,3 +6,3 @@ import { getValueByPath, deepMerge } from '@pluginjs/utils' | ||
class I18N { | ||
constructor (defaults, translations) { | ||
constructor(defaults, translations) { | ||
this.defaults = deepMerge({}, I18N.defaults, defaults) | ||
@@ -13,7 +13,7 @@ | ||
hasTranslation (locale) { | ||
hasTranslation(locale) { | ||
return locale in this.translations | ||
} | ||
addTranslation (locale, translation) { | ||
addTranslation(locale, translation) { | ||
if (this.translations[locale]) { | ||
@@ -26,3 +26,3 @@ Object.assign(this.translations[locale], translation) | ||
getTranslation (locale) { | ||
getTranslation(locale) { | ||
if (this.translations[locale]) { | ||
@@ -34,3 +34,3 @@ return this.translations[locale] | ||
instance (options = {}) { | ||
instance(options = {}) { | ||
const that = this | ||
@@ -41,3 +41,3 @@ | ||
function getMessage (key, locale) { | ||
function getMessage(key, locale) { | ||
const translation = that.getTranslation(locale) | ||
@@ -50,3 +50,3 @@ const message = getValueByPath(translation, key) | ||
return { | ||
translate (key, args = {}, locale = _locale) { | ||
translate(key, args = {}, locale = _locale) { | ||
let message = getMessage(key, locale) | ||
@@ -118,7 +118,7 @@ | ||
setLocale (locale) { | ||
setLocale(locale) { | ||
_locale = locale | ||
}, | ||
getLocale () { | ||
getLocale() { | ||
return _locale | ||
@@ -129,3 +129,3 @@ } | ||
setTranslations (translations) { | ||
setTranslations(translations) { | ||
this.translations = translations | ||
@@ -138,6 +138,6 @@ } | ||
fallbacks: true, | ||
nullPlaceholder (key) { | ||
nullPlaceholder(key) { | ||
return `[missing {{${key}}} value]` | ||
}, | ||
missingPlaceholder (key) { | ||
missingPlaceholder(key) { | ||
return `[missing {{${key}}} value]` | ||
@@ -144,0 +144,0 @@ } |
29200
766
Updated@pluginjs/template@^0.2.13
Updated@pluginjs/utils@^0.2.13