vanilla-i18n
Advanced tools
Comparing version 1.0.6 to 1.0.7
@@ -1,2 +0,2 @@ | ||
module.exports=function(t){var n={};function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:r})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,n){if(1&n&&(t=e(t)),8&n)return t;if(4&n&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var o in t)e.d(r,o,function(n){return t[n]}.bind(null,o));return r},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},e.p="",e(e.s=0)}([function(t,n,e){"use strict";function r(t){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}e.r(n),e.d(n,"setDefaultLanguage",function(){return l}),e.d(n,"setTranslate",function(){return p});var o="pt",u={},l=function(t){o=t},i=function(t){return null!==t&&"object"===r(t)},c=function(t){return null!==t&&"string"==typeof t},a=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},f=function(t,n,e){var r,o,u;if(n){if(!isNaN(parseInt(n)))return n;if(o=n.trim().split("."),t){for(var l=0;l<o.length;l++)if((u=u?u[o[l]]:t[o[l]])&&!i(u))return r=u;r=u}}return r||e};Object.assign(String.prototype,{translate:function(){var t=null,n=null,e=null;arguments.length>0&&((arguments.length<=0?void 0:arguments[0])&&c(arguments.length<=0?void 0:arguments[0])&&(t=arguments.length<=0?void 0:arguments[0]),(arguments.length<=0?void 0:arguments[0])&&i(arguments.length<=0?void 0:arguments[0])&&(n=arguments.length<=0?void 0:arguments[0]),(arguments.length<=1?void 0:arguments[1])&&i(arguments.length<=1?void 0:arguments[1])&&(n=arguments.length<=1?void 0:arguments[1])),t||(t=o);var r=u[t]||{},l=null===(e=a(r,this)?r[this]:null);if(l){var p=this,s=p.match(/(\[\d+])/g),g=p.match(/(\[\w+])/g);s&&(p=p.replace(/(\[\d+])/g,"[:num]")),g&&(p=p.replace(/(\[\w+])/g,"[:str]"));var d=null!==(e=f(r,this,""));d&&(s&&s.forEach(function(t,n){e=(e=(e=e.replace("{$".concat(n+1,"+2}"),parseInt(t.match(/\d+/g),10)+2)).replace("{$".concat(n+1,"+1}"),parseInt(t.match(/\d+/g),10)+1)).replace("$".concat(n+1),t.match(/\d+/g))}),g&&g.forEach(function(t,n){var r=new RegExp("$".concat(n),"g");e=e.replace(r,t.match(/\w+/g))}))}return n&&(e=e.replace(/\{\s?([\w.]+)\s?\}/g,function(t,e){var r=e.trim();return n[r]||r})),null===e?this:e}});var p=function(t,n){n||(n=o),u[n]||(u[n]={}),Object.assign(u[n],t)}}]); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("lib",[],e):"object"==typeof exports?exports.lib=e():t.lib=e()}("undefined"!=typeof self?self:this,function(){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},n.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},n.t=function(t,e){if(1&e&&(t=n(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)n.d(r,o,function(e){return t[e]}.bind(null,o));return r},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){"use strict";function r(t){return(r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}n.r(e),n.d(e,"setDefaultLanguage",function(){return u}),n.d(e,"setTranslate",function(){return p});var o="pt",i={},u=function(t){o=t},c=function(t){return null!==t&&"object"===r(t)},f=function(t){return null!==t&&"string"==typeof t},l=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},a=function(t,e,n){var r;if(e){if(!isNaN(parseInt(e)))return e;if(t){for(var o,i=e.trim().split("."),u=0,f=i.length;u<f;u++)if((o=o?o[i[u]]:t[i[u]])&&!c(o))return o;r=o}}return r||n};Object.assign(String.prototype,{translate:function(){var t,e,n;arguments.length>0&&((arguments.length<=0?void 0:arguments[0])&&f(arguments.length<=0?void 0:arguments[0])&&(e=arguments.length<=0?void 0:arguments[0]),(arguments.length<=0?void 0:arguments[0])&&c(arguments.length<=0?void 0:arguments[0])&&(n=arguments.length<=0?void 0:arguments[0]),(arguments.length<=1?void 0:arguments[1])&&c(arguments.length<=1?void 0:arguments[1])&&(n=arguments.length<=1?void 0:arguments[1])),e||(e=o);var r=i[e]||{};l(r,this)&&(t=r[this]);var u=!t;if(u){var p=this,s=p.match(/(\[\d+])/g),d=p.match(/(\[\w+])/g);s&&(p=p.replace(/(\[\d+])/g,"[:num]")),d&&(p=p.replace(/(\[\w+])/g,"[:str]")),(t=a(r,this,""))&&(s&&s.forEach(function(e,n){t=(t=(t=t.replace("{$".concat(n+1,"+2}"),parseInt(e.match(/\d+/g),10)+2)).replace("{$".concat(n+1,"+1}"),parseInt(e.match(/\d+/g),10)+1)).replace("$".concat(n+1),e.match(/\d+/g))}),d&&d.forEach(function(e,n){var r=new RegExp("$".concat(n),"g");t=t.replace(r,e.match(/\w+/g))}))}return n&&(t=t.replace(/\{\s?([\w.]+)\s?\}/g,function(t,e){var r=e.trim();return n[r]||r})),t||this}});var p=function(t,e){e||(e=o),i[e]||(i[e]={}),Object.assign(i[e],t)}}])}); | ||
//# sourceMappingURL=vanilla-i18n.js.map |
const myFirstNameEn = 'myfirstname'.translate('en') | ||
const myFirstNamePtBr = 'myfirstname'.translate('pt') | ||
const myFirstNameDefault = 'myfirstname'.translate() | ||
const street = 'address.street'.translate({ streetname: 'wall street' }) | ||
const street = 'address.street'.translate({ | ||
streetname: 'wall street' | ||
}) | ||
@@ -6,0 +8,0 @@ console.log({ |
40
index.js
@@ -11,21 +11,23 @@ let defaultLanguage = 'pt' | ||
const hasAttr = (obj, attr) => Object.prototype.hasOwnProperty.call(obj, attr) | ||
const isNum = (val) => !isNaN(parseInt(val)) | ||
const get = (obj, path, defaultValue) => { | ||
let value, patharr, k | ||
let value | ||
if (path) { | ||
if (!isNaN(parseInt(path))) { | ||
return path | ||
} | ||
patharr = path.trim().split('.') | ||
if (isNum(path)) return path | ||
if (obj) { | ||
for (let i = 0; i < patharr.length; i++) { | ||
k = k ? k[patharr[i]] : obj[patharr[i]] | ||
if (k && !isObject(k)) { | ||
value = k | ||
return value | ||
} | ||
let k | ||
const pathArr = path.trim().split('.') | ||
for (let i = 0, c = pathArr.length; i < c; i++) { | ||
k = k ? k[pathArr[i]] : obj[pathArr[i]] | ||
if (k && !isObject(k)) return k | ||
} | ||
value = k | ||
} | ||
} | ||
return value || defaultValue | ||
@@ -36,5 +38,4 @@ } | ||
translate: function (...args) { | ||
let lang = null | ||
let values = null | ||
let i18n = null | ||
let i18n, lang, values | ||
if (args.length > 0) { | ||
@@ -49,4 +50,4 @@ if (args[0] && isString(args[0])) lang = args[0] | ||
i18n = hasAttr(languages, this) ? languages[this] : null | ||
const emptyI18N = i18n === null | ||
if (hasAttr(languages, this)) i18n = languages[this] | ||
const emptyI18N = !i18n | ||
@@ -61,5 +62,4 @@ if (emptyI18N) { | ||
i18n = get(languages, this, '') | ||
const hasI18N = i18n !== null | ||
if (hasI18N) { | ||
if (i18n) { | ||
if (withVarNum) { | ||
@@ -83,3 +83,3 @@ withVarNum.forEach((val, index) => { | ||
if (values) { | ||
i18n = i18n.replace(/\{\s?([\w.]+)\s?\}/g, function (_, variable) { | ||
i18n = i18n.replace(/\{\s?([\w.]+)\s?\}/g, (_, variable) => { | ||
const prop = variable.trim() | ||
@@ -90,3 +90,3 @@ return values[prop] || prop | ||
return i18n === null ? this : i18n | ||
return i18n || this | ||
} | ||
@@ -93,0 +93,0 @@ }) |
{ | ||
"name": "vanilla-i18n", | ||
"version": "1.0.6", | ||
"version": "1.0.7", | ||
"description": "Translating text in a simple and easy way.", | ||
@@ -5,0 +5,0 @@ "main": "dist/vanilla-i18n.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11327
126