From a4fcfcdf9306498986c20354919f5ccf43a337d7 Mon Sep 17 00:00:00 2001 From: Victor Hallberg Date: Mon, 1 Feb 2016 11:44:21 +0100 Subject: [PATCH] Add --keep-removed option (cli & gulp) + allow writeOld to be disabled from cli --- README.md | 3 +++ bin/cli.js | 3 +++ index.js | 3 ++- src/helpers.js | 11 +++++++---- test/helpers.js | 10 ++++++++++ 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3cdd040c..b2856ff8 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,8 @@ Thanks a lot to all the previous [contributors](https://github.com/i18next/i18ne - **-l, --locales **: The locales in your applications. Defaults to `en,fr` - **--directoryFilter**: Globs of folders to filter - **--fileFilter**: Globs of files to filter +- **--keep-removed**: Prevent keys no longer found from being removed +- **--write-old false**: Avoid saving the \_old files --- @@ -96,6 +98,7 @@ gulp.task('i18next', function() { - **prefix**: Add a custom prefix in front of the file name. - **suffix**: Add a custom suffix at the end of the file name. - **extension**: Edit the extension of the files. Defaults to `.json` +- **keepRemoved**: Prevent keys no longer found from being removed You can inject the locale tag in either the prefix, suffix or extension using the `$LOCALE` variable. diff --git a/bin/cli.js b/bin/cli.js index 1ad763ad..d74d57cb 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -26,6 +26,8 @@ program .option( '-l, --locales ' , 'The locales in your application' ) .option( '--directoryFilter ' , 'Filter directories' ) .option( '--fileFilter ' , 'Filter files' ) + .option( '--keep-removed' , 'Prevent keys no longer found from being removed' ) + .option( '--write-old ' , 'Save (or don\'t if false) _old files' ) .parse( process.argv ); @@ -64,6 +66,7 @@ if ( ! fs.existsSync(input) ) { // =================== program.locales = program.locales && program.locales.split(','); program.functions = program.functions && program.functions.split(','); +program.writeOld = program.writeOld !== 'false'; program.directoryFilter = program.directoryFilter && program.directoryFilter.split(','); program.fileFilter = program.fileFilter && program.fileFilter.split(','); program.output = path.resolve(process.cwd(), output); diff --git a/index.js b/index.js index cbb903b9..c01c2c5d 100755 --- a/index.js +++ b/index.js @@ -33,6 +33,7 @@ function Parser(options, transformConfig) { this.suffix = options.suffix || ''; this.prefix = options.prefix || ''; this.writeOld = options.writeOld !== false; + this.keepRemoved = options.keepRemoved; ['functions', 'locales'].forEach(function( attr ) { if ( (typeof self[ attr ] !== 'object') || ! self[ attr ].length ) { @@ -231,7 +232,7 @@ Parser.prototype._flush = function(done) { // merges existing translations with the new ones - mergedTranslations = helpers.mergeHash( currentTranslations, translationsHash[namespace] ); + mergedTranslations = helpers.mergeHash( currentTranslations, translationsHash[namespace], null, this.keepRemoved ); // restore old translations if the key is empty mergedTranslations.new = helpers.replaceEmpty( oldTranslations, mergedTranslations.new ); diff --git a/src/helpers.js b/src/helpers.js index f565421c..4a34f380 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -28,16 +28,16 @@ function hashFromString(path, separator, hash) { // Takes a `source` hash and make sure its value // are pasted in the `target` hash, if the target -// hash has the corresponding key. If not, the -// value is added to an `old` hash. -function mergeHash(source, target, old) { +// hash has the corresponding key (or if keepRemoved is true). +// If not, the value is added to an `old` hash. +function mergeHash(source, target, old, keepRemoved) { target = target || {}; old = old || {}; Object.keys(source).forEach(function (key) { if ( target[key] !== undefined ) { if (typeof source[key] === 'object' && source[key].constructor !== Array) { - var nested = mergeHash( source[key], target[key], old[key] ); + var nested = mergeHash( source[key], target[key], old[key], keepRemoved ); target[key] = nested.new; old[key] = nested.old; } @@ -61,6 +61,9 @@ function mergeHash(source, target, old) { target[key] = source[key]; } else { + if (keepRemoved) { + target[key] = source[key]; + } old[key] = source[key]; } } diff --git a/test/helpers.js b/test/helpers.js index 2bca745e..d6d07a1e 100644 --- a/test/helpers.js +++ b/test/helpers.js @@ -29,6 +29,16 @@ describe('mergeHash helper function', function () { done(); }); + it('copies `source` keys to `target` regardless of presence when keepRemoved is enabled', function (done) { + var source = { key1: 'value1', key2: 'value2' }; + var target = { key1: '', key3: '' }; + var res = mergeHash(source, target, null, true); + + assert.deepEqual(res.new, { key1: 'value1', key2: 'value2', key3: '' }); + assert.deepEqual(res.old, { key2: 'value2' }); + done(); + }); + it('restores plural keys when the singular one exists', function (done) { var source = { key1: '', key1_plural: 'value1' }; var target = { key1: '' };