Skip to content

Commit

Permalink
Remove extension and filename options in favor all path config in…
Browse files Browse the repository at this point in the history
… `output` (#113)

* remove 'extension' and 'filename' option in favor of shoving it all in 'output'

* updated documentation to reflect removal of filename and extension
fixed bug that prevented old catalogs from being generated correctly
added full paths to the test configs

* added filename and extension deprecation notes to migration.md
  • Loading branch information
MynockSpit authored and karellm committed Aug 1, 2018
1 parent 13e2d9b commit ae18bfb
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 72 deletions.
15 changes: 4 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ gulp.task('i18next', function() {
gulp.src('app/**')
.pipe(new i18nextParser({
locales: ['en', 'de'],
output: 'locales'
output: 'locales/$LOCALE/$NAMESPACE.json'
}))
.pipe(gulp.dest('./'));
});
Expand Down Expand Up @@ -92,7 +92,7 @@ let i18n = new Funnel(appRoot, {
})

i18n = new i18nextParser([i18n], {
output: 'broccoli/locales'
output: 'broccoli/locales/$LOCALE/$NAMESPACE.json'
})

module.exports = i18n
Expand All @@ -118,14 +118,6 @@ module.exports = {
"defaultValue": "",
// Default value to give to empty keys

"extension": ".json",
// Extension of the catalogs
// Supports $LOCALE and $NAMESPACE injection

"filename": "$NAMESPACE",
// Filename of the catalogs
// Supports $LOCALE and $NAMESPACE injection

"indentation": 2,
// Indentation of the catalog files

Expand Down Expand Up @@ -161,7 +153,8 @@ module.exports = {
// Namespace separator used in your translation keys
// If you want to use plain english keys, separators such as `.` and `:` will conflict. You might want to set `keySeparator: false` and `namespaceSeparator: false`. That way, `t('Status: Loading...')` will not think that there are a namespace and three separator dots for instance.

"output": "locales",
"output": "locales/$LOCALE/$NAMESPACE.json",
// Supports $LOCALE and $NAMESPACE injection
// Where to write the locale files relative to the base

"input": undefined,
Expand Down
4 changes: 2 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ var vfs = require('vinyl-fs')
program
.version(pkg.version)
.option('-c, --config <path>', 'Path to the config file (default: i18next-scanner.config.js)')
.option('-o, --output <path>', 'Path to the output directory (default: locales)')
.option('-o, --output <path>', 'Path to the output directory (default: locales/$LOCALE/$NAMESPACE.json)')
.option('-s, --silent', 'Disable logging to stdout')

program.on('--help', function() {
console.log(' Examples:')
console.log('')
console.log(' $ i18next "src/**/*.{js,jsx}"')
console.log(' $ i18next "/path/to/src/app.js" "/path/to/assets/index.html"')
console.log(' $ i18next --config i18next-parser.config.js --output /path/to/output \'src/**/*.{js,jsx}\'')
console.log(' $ i18next --config i18next-parser.config.js --output locales/$LOCALE/$NAMESPACE.json \'src/**/*.{js,jsx}\'')
console.log('')
})

Expand Down
22 changes: 7 additions & 15 deletions dist/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ i18nTransform = function (_Transform) {_inherits(i18nTransform, _Transform);
createOldCatalogs: true,
defaultNamespace: 'translation',
defaultValue: '',
extension: '.json',
filename: '$NAMESPACE',
indentation: 2,
keepRemoved: false,
keySeparator: '.',
lexers: {},
lineEnding: 'auto',
locales: ['en', 'fr'],
namespaceSeparator: ':',
output: 'locales',
output: 'locales/$LOCALE/$NAMESPACE.json',
reactNamespace: false,
sort: false,
verbose: false };
Expand Down Expand Up @@ -131,22 +129,16 @@ i18nTransform = function (_Transform) {_inherits(i18nTransform, _Transform);
}var _iteratorNormalCompletion3 = true;var _didIteratorError3 = false;var _iteratorError3 = undefined;try {

for (var _iterator3 = this.options.locales[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {var locale = _step3.value;
var outputPath = _path2.default.resolve(this.options.output, locale);
var outputPath = _path2.default.resolve(this.options.output);

for (var namespace in catalog) {
var filename = this.options.filename;
filename = filename.replace(this.localeRegex, locale);
filename = filename.replace(this.namespaceRegex, namespace);
var namespacePath = outputPath;
namespacePath = namespacePath.replace(this.localeRegex, locale);
namespacePath = namespacePath.replace(this.namespaceRegex, namespace);

var extension = this.options.extension;
extension = extension.replace(this.localeRegex, locale);
extension = extension.replace(this.namespaceRegex, namespace);
var parsedNamespacePath = _path2.default.parse(namespacePath);

var oldFilename = filename + '_old' + extension;
filename += extension;

var namespacePath = _path2.default.resolve(outputPath, filename);
var namespaceOldPath = _path2.default.resolve(outputPath, oldFilename);
var namespaceOldPath = _path2.default.join(parsedNamespacePath.dir, parsedNamespacePath.name + '_old' + parsedNamespacePath.ext);

var existingCatalog = this.getCatalog(namespacePath);
var existingOldCatalog = this.getCatalog(namespaceOldPath);
Expand Down
12 changes: 5 additions & 7 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

Command line (the options are identical):

`i18next /path/to/file/or/dir -o /output/directory`
`i18next /path/to/file/or/dir -o /translations/$LOCALE/$NAMESPACE.json`

`i18next /path/to/file/or/dir:/output/directory`
`i18next /path/to/file/or/dir:/translations/$LOCALE/$NAMESPACE.json`

Gulp:

`.pipe(i18next({output: 'translations'}))`
`.pipe(i18next({output: 'translations/$LOCALE/$NAMESPACE.json'}))`

It will create the file in the specified folder (in case of gulp it doesn't actually create the files until you call `dest()`):

```
/output/directory/en/translation.json
/translations/en/translation.json
...
```

Expand Down Expand Up @@ -148,12 +148,10 @@ In recursive mode, it will parse `*.hbs` and `*.js` files and skip `.git` folder
**Work with Meteor TAP-i18N (gulp)**

`.pipe(i18next({
output: "i18n",
output: "i18n/$LOCALE/$NAMESPACE.$LOCALE.i18n.json",
locales: ['en', 'de', 'fr', 'es'],
functions: ['_'],
namespace: 'client',
suffix: '.$LOCALE',
extension: ".i18n.json",
writeOld: false
}))`

Expand Down
8 changes: 5 additions & 3 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
- `ignoreVariables` was deprecated. Keys that are not string litterals now emit a warning
- `writeOld` was renamed `createOldLibraries`. It defaults to `true`.
- `namespace` was renamed `defaultNamespace`. It defaults to `translation`.
- `prefix` was deprecated. Use `filename`
- `suffix` was deprecated. Use `filename`
- `prefix` was deprecated. Use `output`
- `suffix` was deprecated. Use `output`
- `filename` was deprecated. Use `output`
- `extension` was deprecated. Use `output`
- catalogs are no longer sorted by default. Set `sort` to `true` to enable this.

## Improvements

- `defaultValue`: replace empty keys with the given value
- `filename` and `extension` support for `$NAMESPACE` and `$LOCALE` variables
- `output` support for `$NAMESPACE` and `$LOCALE` variables
- `indentation` let you control the indentation of the catalogs
- `lineEnding` let you control the line ending of the catalogs
- `sort` let you enable sorting.
Expand Down
2 changes: 1 addition & 1 deletion i18next-parser.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
output: 'test/manual'
output: 'test/manual/$LOCALE/$NAMESPACE.json'
};
2 changes: 1 addition & 1 deletion issue_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Here is my config file:
```
{
locales: ['en', 'de'],
output: '../locales'
output: 'locales/$LOCALE/$NAMESPACE.json'
}
```

Expand Down
22 changes: 7 additions & 15 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ export default class i18nTransform extends Transform {
createOldCatalogs: true,
defaultNamespace: 'translation',
defaultValue: '',
extension: '.json',
filename: '$NAMESPACE',
indentation: 2,
keepRemoved: false,
keySeparator: '.',
lexers: {},
lineEnding: 'auto',
locales: ['en', 'fr'],
namespaceSeparator: ':',
output: 'locales',
output: 'locales/$LOCALE/$NAMESPACE.json',
reactNamespace: false,
sort: false,
verbose: false
Expand Down Expand Up @@ -131,22 +129,16 @@ export default class i18nTransform extends Transform {
}

for (const locale of this.options.locales) {
const outputPath = path.resolve(this.options.output, locale)
const outputPath = path.resolve(this.options.output)

for (const namespace in catalog) {
let filename = this.options.filename
filename = filename.replace(this.localeRegex, locale)
filename = filename.replace(this.namespaceRegex, namespace)
let namespacePath = outputPath
namespacePath = namespacePath.replace(this.localeRegex, locale)
namespacePath = namespacePath.replace(this.namespaceRegex, namespace)

let extension = this.options.extension
extension = extension.replace(this.localeRegex, locale)
extension = extension.replace(this.namespaceRegex, namespace)
let parsedNamespacePath = path.parse(namespacePath)

const oldFilename = filename + '_old' + extension
filename += extension

const namespacePath = path.resolve(outputPath, filename)
const namespaceOldPath = path.resolve(outputPath, oldFilename)
const namespaceOldPath = path.join(parsedNamespacePath.dir, `${parsedNamespacePath.name}_old${parsedNamespacePath.ext}`)

let existingCatalog = this.getCatalog(namespacePath)
let existingOldCatalog = this.getCatalog(namespaceOldPath)
Expand Down
2 changes: 1 addition & 1 deletion test/Brocfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let i18n = new Funnel(appRoot, {
})

i18n = new i18nextParser([i18n], {
output: 'broccoli/locales'
output: 'broccoli/locales/$LOCALE/$NAMESPACE.json'
})

module.exports = i18n
2 changes: 1 addition & 1 deletion test/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ gulp.task('i18next', function() {
return gulp.src(['templating/*'])
.pipe(new i18next({
locales: ['en', 'fr'],
output: 'gulp/locales'
output: 'gulp/locales/$LOCALE/$NAMESPACE.json'
}))
.pipe(gulp.dest('./'));
});
2 changes: 1 addition & 1 deletion test/i18next-parser.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
output: 'manual'
output: 'manual/$LOCALE/$NAMESPACE.json'
};
27 changes: 13 additions & 14 deletions test/parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ describe('parser', () => {

it('retrieves values in existing catalog and creates old catalog', (done) => {
let result, resultOld
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from("t('test_merge:first'); t('test_merge:second')"),
path: 'file.js'
Expand All @@ -343,7 +343,7 @@ describe('parser', () => {
it('does not leak values between locales', (done) => {
let resultEN
let resultFR
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from("t('test_leak:first'); t('test_leak:second')"),
path: 'file.js'
Expand All @@ -368,7 +368,7 @@ describe('parser', () => {

it('retrieves context values in existing catalog', (done) => {
let result
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from("t('test_context:first')"),
path: 'file.js'
Expand All @@ -394,7 +394,7 @@ describe('parser', () => {
})

it('saves unused translations in the old catalog', (done) => {
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from("t('test_old:parent.third', 'third'), t('test_old:fourth', 'fourth')"),
path: 'file.js'
Expand Down Expand Up @@ -422,7 +422,7 @@ describe('parser', () => {
})

it('restores translations from the old catalog', (done) => {
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from("t('test_old:parent.some', 'random'), t('test_old:other', 'random')"),
path: 'file.js'
Expand Down Expand Up @@ -451,7 +451,7 @@ describe('parser', () => {

it('retrieves plural values in existing catalog', (done) => {
let result
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from(
"t('test_plural:first'); t('test_plural:second')"
Expand Down Expand Up @@ -482,7 +482,7 @@ describe('parser', () => {

it('retrieves plural and context values in existing catalog', (done) => {
let result
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from("t('test_context_plural:first')"),
path: 'file.js'
Expand All @@ -508,13 +508,12 @@ describe('parser', () => {
})

describe('options', () => {
it('handles filename and extension with $LOCALE and $NAMESPACE var', (done) => {
it('handles output with $LOCALE and $NAMESPACE var', (done) => {
let result
const i18nextParser = new i18nTransform({
locales: ['en'],
defaultNamespace: 'default',
filename: 'p-$LOCALE-$NAMESPACE',
extension: '.$LOCALE.i18n'
output: 'locales/$LOCALE/p-$LOCALE-$NAMESPACE.$LOCALE.i18n'
})
const fakeFile = new Vinyl({
contents: Buffer.from("t('fourth')"),
Expand Down Expand Up @@ -695,7 +694,7 @@ describe('parser', () => {
it('supports outputing to yml', (done) => {
let result
const i18nextParser = new i18nTransform({
extension: '.yml'
output: 'locales/$LOCALE/$NAMESPACE.yml'
})
const fakeFile = new Vinyl({
contents: Buffer.from("t('first')"),
Expand Down Expand Up @@ -878,7 +877,7 @@ describe('parser', () => {
})

it('emits a `error` event if the catalog is not valid json', (done) => {
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from("t('test_invalid:content')"),
path: 'file.js'
Expand Down Expand Up @@ -907,7 +906,7 @@ describe('parser', () => {
})

it('emits a `warning` event if a key contains a variable', (done) => {
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from('t(variable)'),
path: 'file.js'
Expand All @@ -921,7 +920,7 @@ describe('parser', () => {
})

it('emits a `warning` event if a react value contains two variables', (done) => {
const i18nextParser = new i18nTransform({ output: 'test/locales' })
const i18nextParser = new i18nTransform({ output: 'test/locales/$LOCALE/$NAMESPACE.json' })
const fakeFile = new Vinyl({
contents: Buffer.from('<Trans>{{ key1, key2 }}</Trans>'),
path: 'file.js'
Expand Down

0 comments on commit ae18bfb

Please sign in to comment.