Created
July 5, 2019 14:59
-
-
Save paulofph/3ade5b5827fb357dc1ed5174a12e71ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs') | |
var chalk = require('chalk') | |
module.exports = { | |
input: [ | |
'src/**/*.{js,jsx}', | |
// Use ! to filter out files or directories | |
'!src/**/*.spec.{js,jsx}', | |
'!src/i18n/**', | |
'!**/node_modules/**' | |
], | |
output: './', | |
options: { | |
debug: true, | |
func: { | |
list: ['i18next.t', 'i18n.t'], | |
extensions: ['.js', '.jsx'] | |
}, | |
trans: { | |
component: 'Trans', | |
i18nKey: 'i18nKey', | |
defaultsKey: 'defaults', | |
extensions: ['.js', '.jsx'], | |
fallbackKey: function(ns, value) { | |
return value | |
}, | |
acorn: { | |
ecmaVersion: 10, // defaults to 10 | |
sourceType: 'module' // defaults to 'module' | |
// Check out https://github.com/acornjs/acorn/tree/master/acorn#interface for additional options | |
} | |
}, | |
lngs: ['en-US', 'de', 'it', 'fr', 'pt-PT', 'pt-BR'], | |
ns: ['locale', 'resource'], | |
defaultLng: 'en-US', | |
defaultNs: 'resource', | |
resource: { | |
loadPath: 'i18n/{{lng}}/{{ns}}.json', | |
savePath: 'i18n/{{lng}}/{{ns}}.json', | |
jsonIndent: 2, | |
lineEnding: '\n' | |
}, | |
nsSeparator: false, // namespace separator | |
keySeparator: false, // key separator | |
interpolation: { | |
prefix: '{{', | |
suffix: '}}' | |
}, | |
context: false, | |
plural: false | |
}, | |
transform: function customTransform(file, enc, done) { | |
'use strict' | |
const parser = this.parser | |
const content = fs.readFileSync(file.path, enc) | |
let count = 0 | |
parser.parseFuncFromString( | |
content, | |
(key, options) => { | |
if (options.count) { | |
options.defaultValue = `{count, plural, one {1 ${key}} other {{count} ${key}}}` // use key as the value | |
} | |
parser.set(key, options); | |
++count | |
}, | |
{ list: ['i18next._', 'i18next.__'] } | |
) | |
if (count > 0) { | |
console.log( | |
`i18next-scanner: count=${chalk.cyan(count)}, file=${chalk.yellow( | |
JSON.stringify(file.relative) | |
)}` | |
) | |
} | |
done() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment