New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

vanilla-i18n

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vanilla-i18n - npm Package Compare versions

Comparing version 1.0.5 to 1.0.6

2

dist/vanilla-i18n.js

@@ -1,2 +0,2 @@

module.exports=function(t){function e(n){if(r[n])return r[n].exports;var a=r[n]={i:n,l:!1,exports:{}};return t[n].call(a.exports,a,a.exports,e),a.l=!0,a.exports}var r={};return e.m=t,e.c=r,e.d=function(t,r,n){e.o(t,r)||Object.defineProperty(t,r,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(r,"a",r),r},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0});var n="pt",a={};e.setDefaultLanguage=function(t){n=t};Object.assign(String.prototype,{translate:function(t){var e=null;t||(t=n);var r=a[t]||{};if(null===(e=r.hasOwnProperty(this)?r[this]:null)){var u=this,c=u.match(/(\[\d+])/g),o=u.match(/(\[\w+])/g);c&&(u=u.replace(/(\[\d+])/g,"[:num]")),o&&(u=u.replace(/(\[\w+])/g,"[:str]")),e=r.hasOwnProperty(u)?r[u]:null,null!==e&&(c.forEach(function(t,r){e=e.replace("{$"+(r+1)+"+2}",parseInt(t.match(/\d+/g),10)+2),e=e.replace("{$"+(r+1)+"+1}",parseInt(t.match(/\d+/g),10)+1),e=e.replace("$"+(r+1),t.match(/\d+/g))}),o.forEach(function(t,r){var n=new RegExp("$"+r,"g");e=e.replace(n,t.match(/\w+/g))}))}return null===e?this:e}});e.setTranslate=function(t,e){e||(e=n),a[e]||(a[e]={}),Object.assign(a[e],t)}}]);
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)}}]);
//# 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' })

@@ -8,3 +9,4 @@ console.log({

pt: myFirstNamePtBr,
default: myFirstNameDefault
default: myFirstNameDefault,
street: street
})
const i18n = require('../dist/vanilla-i18n')
i18n.setTranslate({
'myfirstname': 'My First name',
'mymiddlename': 'My middle name',
'mylastname': 'My Last name',
'myfullname': 'My full name',
myfirstname: 'My First name',
mymiddlename: 'My middle name',
mylastname: 'My Last name',
myfullname: 'My full name',
address: {
street: '{ streetname } is my street'
},
'other i18n': 'Other translation',
'somebody text': 'Other somebody texto'
}, 'en')
const i18n = require('../dist/vanilla-i18n')
i18n.setTranslate({
'myfirstname': 'Agrupador',
'mymiddlename': 'Opção de resposta',
'mylastname': 'Opção única',
'myfullname': 'Opções múltiplas',
myfirstname: 'Agrupador',
mymiddlename: 'Opção de resposta',
mylastname: 'Opção única',
myfullname: 'Opções múltiplas',
address: {
street: '{ streetname } é minha rua'
},
'othen i18n': 'Área de texto',

@@ -9,0 +12,0 @@ 'somebody text': 'Texto resumido'

let defaultLanguage = 'pt'
let translate = {}
const translate = {}

@@ -8,9 +8,42 @@ export const setDefaultLanguage = (lang) => {

const isObject = (value) => value !== null && typeof value === 'object'
const isString = (value) => value !== null && typeof value === 'string'
const hasAttr = (obj, attr) => Object.prototype.hasOwnProperty.call(obj, attr)
const get = (obj, path, defaultValue) => {
let value, patharr, k
if (path) {
if (!isNaN(parseInt(path))) {
return path
}
patharr = path.trim().split('.')
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
}
}
value = k
}
}
return value || defaultValue
}
Object.assign(String.prototype, {
translate: function (lang) {
translate: function (...args) {
let lang = null
let values = null
let i18n = null
if (args.length > 0) {
if (args[0] && isString(args[0])) lang = args[0]
if (args[0] && isObject(args[0])) values = args[0]
if (args[1] && isObject(args[1])) values = args[1]
}
if (!lang) lang = defaultLanguage
const languages = translate[lang] || {}
i18n = languages.hasOwnProperty(this) ? languages[this] : null
i18n = hasAttr(languages, this) ? languages[this] : null
const emptyI18N = i18n === null

@@ -25,19 +58,30 @@

i18n = languages.hasOwnProperty(t) ? languages[t] : null
i18n = get(languages, this, '')
const hasI18N = i18n !== null
if (hasI18N) {
withVarNum.forEach((val, index) => {
i18n = i18n.replace(`{$${index + 1}+2}`, parseInt(val.match(/\d+/g), 10) + 2)
i18n = i18n.replace(`{$${index + 1}+1}`, parseInt(val.match(/\d+/g), 10) + 1)
i18n = i18n.replace(`$${index + 1}`, val.match(/\d+/g))
})
if (withVarNum) {
withVarNum.forEach((val, index) => {
i18n = i18n.replace(`{$${index + 1}+2}`, parseInt(val.match(/\d+/g), 10) + 2)
i18n = i18n.replace(`{$${index + 1}+1}`, parseInt(val.match(/\d+/g), 10) + 1)
i18n = i18n.replace(`$${index + 1}`, val.match(/\d+/g))
})
}
withVarStr.forEach((val, index) => {
const rg = new RegExp(`$${index}`, 'g')
i18n = i18n.replace(rg, val.match(/\w+/g))
})
if (withVarStr) {
withVarStr.forEach((val, index) => {
const rg = new RegExp(`$${index}`, 'g')
i18n = i18n.replace(rg, val.match(/\w+/g))
})
}
}
}
if (values) {
i18n = i18n.replace(/\{\s?([\w.]+)\s?\}/g, function (_, variable) {
const prop = variable.trim()
return values[prop] || prop
})
}
return i18n === null ? this : i18n

@@ -44,0 +88,0 @@ }

{
"name": "vanilla-i18n",
"version": "1.0.5",
"version": "1.0.6",
"description": "Translating text in a simple and easy way.",

@@ -10,3 +10,3 @@ "main": "dist/vanilla-i18n.js",

"scripts": {
"build": "NODE_ENV=production BABEL_ENV=production webpack --progress"
"build": "webpack --progress"
},

@@ -31,17 +31,17 @@ "repository": {

"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-eslint": "^8.0.2",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"eslint": "^4.10.0",
"eslint-config-standard": "^10.2.1",
"eslint-loader": "^1.9.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"webpack": "^3.8.1"
"@babel/core": "^7.5.5",
"@babel/preset-env": "^7.5.5",
"babel-eslint": "^10.0.2",
"babel-loader": "^8.0.6",
"eslint": "^6.1.0",
"eslint-config-standard": "^13.0.1",
"eslint-loader": "^2.2.1",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"terser-webpack-plugin": "^1.4.1",
"webpack": "^4.39.1",
"webpack-cli": "^3.3.6"
}
}

@@ -57,1 +57,41 @@ vanilla-i18n

```
using nested objects
```
i18n.setTranslate({
'name': {
firstname: 'my first name',
middlename: 'my middle name',
lastname: 'my last name'
},
'other i18n': 'Other translation',
'somebody text': 'Other somebody texto'
}, 'en')
'name.firstname'.translate()
'name.middlename'.translate()
'name.lastname'.translate()
```
using placeholders and passing values to translate function
```
i18n.setTranslate({
'name': {
firstname: '{firstname} is my first name',
middlename: 'my middle name',
lastname: 'my last name'
},
'other i18n': 'Other translation',
'somebody text': 'Other somebody texto'
}, 'en')
'name.firstname'.translate({firstname: 'George'})
```
passing language along with the parameters
```
'name.firstname'.translate('pt',{firstname: 'George'})
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc