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

new-i18n

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

new-i18n - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

i18n.js

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 @@ {

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