Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

i18n-ini-loader

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

i18n-ini-loader - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

8

example/index.js

@@ -1,8 +0,6 @@

const { welcome, niceDay, asList } = require('./messages.ini')
const { hello, niceDay } = require('./messages.ini')
console.log(
asList(
welcome('Mark'),
niceDay
)
hello('Mark'),
niceDay
)

@@ -5,3 +5,4 @@ {

"scripts": {
"test": "webpack && node bundle"
"build": "webpack",
"start": "node bundle"
},

@@ -12,5 +13,4 @@ "devDependencies": {

"babel-preset-env": "^1.5.2",
"i18n-ini-loader": "^0.1.0",
"webpack": "^3.0.0"
}
}

@@ -1,3 +0,10 @@

```
npm i && npm t
```sh
# install dependencies
npm install
# run webpack build
npm run build
# execute build result
npm start
```

@@ -8,6 +8,8 @@ const babelLoader = {

const i18nIniLoader = {
loader: 'i18n-ini-loader',
const i18nIniLoader = { /*
loader: 'i18n-ini-loader', */
loader: '../index.js',
options: {
language: 'de'
language: 'de',
failOnMissingTranslation: true
}

@@ -14,0 +16,0 @@ }

const ini = require('ini')
const loaderUtils = require('loader-utils')
const transform = string => {
const regex = /\$\{(.*?)\}/g
const collectMatchingGroups = (regex, string) => {
const parameters = []
let result
while (result = regex.exec(string))
parameters.push(result[1])
const groups = []
return parameters.length
? `(${parameters.join(', ')}) => \`${string}\``
: `'${string}'`
let match
while (match = regex.exec(string))
groups.push(match[1])
return groups
}
module.exports = function I18nIniLoader(source) {
const { language } = Object.assign(
{ language: 'en' },
loaderUtils.getOptions(this)
)
const transform = message => {
try {
const messages = Object.entries(
ini.parse(source)
)
.map(([key, value]) => ` ${key}: ${transform(value[language])}`)
.join(',\n')
const findParameters = /\$\{(.*?)\}/g
const parameters = collectMatchingGroups(findParameters, message)
const hasParameters = parameters.length > 0
return `module.exports = {\n${messages}\n}`
return hasParameters
? `(${parameters.join(', ')}) => \`${message}\``
: `'${message}'`
}
} catch (e) {
throw new Error('An unexpected error occured:', e)
}
const getMessage = ({ section, messages, options, loader }) => {
const message = messages[options.language]
if (message !== undefined)
return message
if (!options.failOnMissingTranslation)
return ''
throw new Error(`Missing translation for language '${options.language}' `
+ `in section '${section}': ${loader.request}`)
}
module.exports = function I18nIniLoader(source) {
const options = Object.assign(
{
language: 'en',
failOnMissingTranslation: true
},
this.query
)
const messages = Object.entries(
ini.parse(source)
)
.map(entry => {
const [ section, messages ] = entry
const message = getMessage({ section, messages, options, loader: this })
const messageOrFunction = transform(message)
return ` ${section}: ${messageOrFunction}`
})
.join(',\n')
return `module.exports = {\n${messages}\n}`
}
{
"name": "i18n-ini-loader",
"version": "0.1.1",
"version": "0.2.0",
"description": "Webpack I18n loader based on .ini files.",

@@ -16,4 +16,3 @@ "main": "index.js",

"dependencies": {
"ini": "^1.3.4",
"loader-utils": "^1.1.0"
"ini": "^1.3.4"
},

@@ -20,0 +19,0 @@ "devDependencies": {},

@@ -17,9 +17,9 @@ # i18n-ini-loader

```ini
[welcome]
en=Welcome, ${user}!
de=Willkommen, ${user}!
[hello]
en=Hello, ${name}!
de=Hallo, ${name}!
[niceDay]
en=Have a nice day!
de=Hab einen schönen Tag!
en=Have a nice day.
de=Hab einen schönen Tag.
```

@@ -40,3 +40,4 @@

options: {
language: 'de'
language: 'de',
failOnMissingTranslation: true
}

@@ -50,6 +51,6 @@ }

// Note: Since the resulting output of the `i18n-ini-loader`
// uses ES6 template strings and arrow functions (see below),
// you may want to chain the `babel-loader` for ES5 support.
// Check out the `/example` directory for an example config.
// Note: Since the output of the `i18n-ini-loader` uses ES6 template strings
// and arrow functions (see below), you may want to chain the `babel-loader`
// for ES5 support.
// Check out the `/example` directory for a simple example config.
```

@@ -61,4 +62,4 @@

module.exports = {
welcome: (user) => `Willkommen, ${user}!`,
 niceDay: 'Hab einen schönen Tag!'
welcome: (name) => `Hallo, ${name}!`,
 niceDay: 'Hab einen schönen Tag.'
}

@@ -71,8 +72,8 @@ ```

```jsx
import { welcome, niceDay } from './messages.ini'
import { hello, niceDay } from './messages.ini'
export default function Welcome({ user }) {
export default function Welcome({ name }) {
 return (
<div>
<h1>{welcome(user)}</h1>
<h1>{hello(name)}</h1>
<span>{niceDay}</span>

@@ -82,4 +83,14 @@ </div>

}
// <div>
// <h1>Hallo, Mark!</h1>
// <span>Hab einen schönen Tag.</span>
// </div>
```
## Options
- **`language`**: the language key used for translation (*default*: `en`)
- **`failOnMissingTranslation`**: whether an `Error` should be thrown if missing translations are found (*default*: `true`); if `false`, missing translations will be handled gracefully by returning an empty `String` (`''`)
## License

@@ -86,0 +97,0 @@

Sorry, the diff of this file is not supported yet

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