Comparing version 1.3.0 to 1.4.0
40
index.js
`use strict`; | ||
module.exports = (folder, langs, fallback = null) => { | ||
if (!langs || (Array.isArray(langs) && langs.length === 0)) { | ||
throw new Error(`You need to add at least one language.`); | ||
} | ||
const I18n = require(`./i18n`); | ||
const languages = {}; | ||
langs.forEach(lang => (languages[lang] = require(`${folder}/${lang}.json`))); | ||
const i18n = function (lang, keyword, variables = {}) { | ||
if (!languages[lang]) { | ||
return keyword; | ||
} | ||
const value = keyword | ||
.split(`.`) | ||
.reduce( | ||
(res, key) => res && res[key], | ||
languages[lang] | ||
); | ||
if (!value) { | ||
if (fallback && lang !== fallback) { | ||
return i18n(fallback, keyword, variables); | ||
} else { | ||
return keyword; | ||
} | ||
} | ||
return value.replace( | ||
/\{{2}(.+?)\}{2}/g, | ||
(_, variable) => variables[variable] || variable | ||
); | ||
}; | ||
i18n.languages = Object.keys(languages); | ||
return i18n; | ||
}; | ||
module.exports = (folder, languages, fallback = null) => | ||
new I18n(folder, languages, fallback); |
{ | ||
"name": "new-i18n", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "new-i18n is a simple and easy to use internationalization library.", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"test": "mocha", | ||
"lint": "eslint index.js test/test.js --fix" | ||
"lint": "eslint index.js i18n.js test/test.js" | ||
}, | ||
@@ -18,5 +18,5 @@ "repository": { | ||
"devDependencies": { | ||
"eslint": "^5.16.0", | ||
"eslint": "^6.7.2", | ||
"mocha": "^5.2.0" | ||
} | ||
} |
# new-i18n | ||
new-i18n is a simple and easy to use internationalization library. | ||
# Installation | ||
```sh | ||
@@ -10,5 +12,7 @@ npm install new-i18n | ||
# Example | ||
> There's a full working example [here](https://gist.github.com/YouTwitFaceTG/618298c5ef179eebc511ca8c8a82eb76) | ||
> There's a full working example [here](https://gist.github.com/YouTwitFace♠/618298c5ef179eebc511ca8c8a82eb76) | ||
`localization/en.json`: | ||
```json | ||
@@ -21,2 +25,3 @@ { | ||
`localization/pt.json`: | ||
```json | ||
@@ -40,3 +45,4 @@ { | ||
[//]: # ({% raw %}) | ||
[//]: # '{% raw %}' | ||
```json | ||
@@ -47,11 +53,24 @@ { | ||
``` | ||
[//]: # ({% endraw %}) | ||
[//]: # '{% endraw %}' | ||
```js | ||
console.log(i18n('en', 'hi', { name: '...' })); // 'Hi ...' | ||
console.log(i18n('en', 'hi', { name: '...' })); // 'Hi ...!' | ||
``` | ||
# Updating Varaibles | ||
[//]: # '{% raw %}' | ||
```js | ||
i18n.update('en', { hi: 'Hello {{name}}!' }); | ||
console.log(i18n('en', 'hi', { name: '...' })); // 'Hhello ...!' | ||
``` | ||
[//]: # '{% endraw %}' | ||
# Nesting | ||
`localization/en.json`: | ||
```json | ||
@@ -66,2 +85,3 @@ { | ||
`localization/pt.json`: | ||
```json | ||
@@ -68,0 +88,0 @@ { |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
5942
5
77
108
1
2