diff --git a/package.json b/package.json index 3a45edc..bcaf47a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-intl-gettext", - "version": "5.0.1", + "version": "5.1.0", "description": "utilities to integrate react-intl with gettext", "main": "dist/index.js", "scripts": { diff --git a/src/cli.js b/src/cli.js index afc6604..6597188 100755 --- a/src/cli.js +++ b/src/cli.js @@ -5,7 +5,10 @@ import program from 'commander' import headerFormatter from './json2po/headerFormatter' import potFormatter from './json2po/potFormatter' import messageReader from './json2po/jsonMessageReader' -import poMessageReader from './po2json/poMessageReader' +import poMessageReader, { + defaultPattern, + defaultNameMatcherPatternString, +} from './po2json/poMessageReader' import { getUserPackageJson, buildVersionFromPackageJson, @@ -61,24 +64,34 @@ program program .command('po2json ') .description('converts po files to json') - .option('-p, --pattern [pattern]', 'glob pattern used to find the src files [**/*.po]', '**/*.po') + .option( + '-p, --pattern [pattern]', + `glob pattern used to find the src files [${defaultPattern}]`, + defaultPattern + ) .option('--pretty', 'pretty print json') .option( '-i, --ignore ', 'add a pattern or an array of glob patterns to exclude matches', list ) - .action((src, dest, { pattern, pretty, ignore }) => { + .option( + '--lang-matcher-pattern [lang-matcher-pattern]', + `pattern used to match the language from the file name [${defaultNameMatcherPatternString}]`, + defaultNameMatcherPatternString + ) + .action((src, dest, { pattern, pretty, ignore, langMatcherPattern }) => { writeFileSync( dest, JSON.stringify( poMessageReader({ cwd: src, messagesPattern: pattern, + langMatcherPattern, ignore, }), null, - pretty ? '\t' : undefined + pretty ? ' ' : undefined ) ) }) diff --git a/src/po2json/poMessageReader.js b/src/po2json/poMessageReader.js index 25ad253..2890535 100644 --- a/src/po2json/poMessageReader.js +++ b/src/po2json/poMessageReader.js @@ -3,12 +3,16 @@ import { join } from 'path' import { readFileSync } from 'fs' import { po } from 'gettext-parser' -const defaultNameMatcher = filename => filename.match(/.*[-](.*)\.po$/)[1] +const newNameMatcher = regex => filename => filename.match(regex)[1] + +export const defaultNameMatcherPatternString = '.*-(.*)\\.po$' +export const defaultPattern = '**/*.po' export default ({ - messagesPattern = '**/*.po', + messagesPattern = defaultPattern, cwd = process.cwd(), - langMatcher = defaultNameMatcher, + langMatcherPattern = defaultNameMatcherPatternString, + langMatcher = newNameMatcher(langMatcherPattern), ignore, }) => { const translations = globSync(messagesPattern, { cwd, ignore }).map(filename => {