Skip to content

Instantly share code, notes, and snippets.

@paulofph
Created July 5, 2019 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulofph/3ade5b5827fb357dc1ed5174a12e71ce to your computer and use it in GitHub Desktop.
Save paulofph/3ade5b5827fb357dc1ed5174a12e71ce to your computer and use it in GitHub Desktop.
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