From 76ee3f31f97f3a6a289775c9177348c59e12bdf8 Mon Sep 17 00:00:00 2001 From: Jonathan Sun Date: Mon, 7 Nov 2022 22:26:19 -0800 Subject: [PATCH 1/6] Replace useKeysAsDefaultValue and skipDefaultValues with a function that takes defaultValue as a parameter --- README.md | 9 -- src/helpers.js | 22 +---- src/transform.js | 4 - test/helpers/dotPathToHash.test.js | 10 --- test/parser.test.js | 133 ++--------------------------- 5 files changed, 10 insertions(+), 168 deletions(-) diff --git a/README.md b/README.md index ee452730..a3ad34b8 100644 --- a/README.md +++ b/README.md @@ -183,15 +183,6 @@ export default { sort: false, // Whether or not to sort the catalog. Can also be a [compareFunction](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters) - skipDefaultValues: false, - // Whether to ignore default values - // You may also specify a function accepting the locale and namespace as arguments - - useKeysAsDefaultValue: false, - // Whether to use the keys as the default value; ex. "Hello": "Hello", "World": "World" - // This option takes precedence over the `defaultValue` and `skipDefaultValues` options - // You may also specify a function accepting the locale and namespace as arguments - verbose: false, // Display info about the parsing including some stats diff --git a/src/helpers.js b/src/helpers.js index e53412bd..acc1ed95 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -36,33 +36,15 @@ function dotPathToHash(entry, target = {}, options = {}) { const defaultValue = typeof options.value === 'function' - ? options.value(options.locale, entry.namespace, key) + ? options.value(options.locale, entry.namespace, key, entry.defaultValue) : options.value - const skipDefaultValues = - typeof options.skipDefaultValues === 'function' - ? options.skipDefaultValues(options.locale, entry.namespace) - : options.skipDefaultValues - - const useKeysAsDefaultValue = - typeof options.useKeysAsDefaultValue === 'function' - ? options.useKeysAsDefaultValue(options.locale, entry.namespace) - : options.useKeysAsDefaultValue - let newValue = + defaultValue || entry[`defaultValue${options.suffix}`] || entry.defaultValue || - defaultValue || '' - if (skipDefaultValues) { - newValue = '' - } - - if (useKeysAsDefaultValue) { - newValue = key - } - if (path.endsWith(separator)) { path = path.slice(0, -separator.length) } diff --git a/src/transform.js b/src/transform.js index 07ba0c76..3f692a68 100644 --- a/src/transform.js +++ b/src/transform.js @@ -36,9 +36,7 @@ export default class i18nTransform extends Transform { output: 'locales/$LOCALE/$NAMESPACE.json', resetDefaultValueLocale: null, sort: false, - useKeysAsDefaultValue: false, verbose: false, - skipDefaultValues: false, customValueTemplate: null, failOnWarnings: false, yamlOptions: null, @@ -171,8 +169,6 @@ export default class i18nTransform extends Transform { separator: this.options.keySeparator, pluralSeparator: this.options.pluralSeparator, value: this.options.defaultValue, - useKeysAsDefaultValue: this.options.useKeysAsDefaultValue, - skipDefaultValues: this.options.skipDefaultValues, customValueTemplate: this.options.customValueTemplate, }) diff --git a/test/helpers/dotPathToHash.test.js b/test/helpers/dotPathToHash.test.js index 0ed2a1f5..559c6d4a 100644 --- a/test/helpers/dotPathToHash.test.js +++ b/test/helpers/dotPathToHash.test.js @@ -35,16 +35,6 @@ describe('dotPathToHash helper function', () => { done() }) - it('supports custom separator when `useKeysAsDefaultValue` is true', (done) => { - const { target } = dotPathToHash( - { keyWithNamespace: 'namespace-two-three' }, - {}, - { separator: '-', useKeysAsDefaultValue: true } - ) - assert.deepEqual(target, { namespace: { two: { three: 'two-three' } } }) - done() - }) - it('handles an empty namespace', (done) => { const { target, duplicate } = dotPathToHash({ keyWithNamespace: 'ns.', diff --git a/test/parser.test.js b/test/parser.test.js index 130251b6..6ffa95b3 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -1085,11 +1085,11 @@ describe('parser', () => { it('supports a defaultValue function', (done) => { let result const i18nextParser = new i18nTransform({ - defaultValue: (locale, namespace, key) => - `${locale}:${namespace}:${key}`, + defaultValue: (locale, namespace, key, defaultValue) => + `${locale}:${namespace}:${key}:${defaultValue}`, }) const fakeFile = new Vinyl({ - contents: Buffer.from("t('first')"), + contents: Buffer.from("t('first', 'myDefault')"), path: 'file.js', }) @@ -1099,63 +1099,7 @@ describe('parser', () => { } }) i18nextParser.once('end', () => { - assert.deepEqual(result, { first: 'en:translation:first' }) - done() - }) - - i18nextParser.end(fakeFile) - }) - - it('supports a useKeysAsDefaultValue function', (done) => { - let enResult - let arResult - const i18nextParser = new i18nTransform({ - locales: ['en', 'ar'], - useKeysAsDefaultValue: (locale, namespace) => locale === 'en', - }) - const fakeFile = new Vinyl({ - contents: Buffer.from("t('first')"), - path: 'file.js', - }) - - i18nextParser.on('data', (file) => { - if (file.relative.endsWith(enLibraryPath)) { - enResult = JSON.parse(file.contents) - } else if (file.relative.endsWith(arLibraryPath)) { - arResult = JSON.parse(file.contents) - } - }) - i18nextParser.once('end', () => { - assert.deepEqual(enResult, { first: 'first' }) - assert.deepEqual(arResult, { first: '' }) - done() - }) - - i18nextParser.end(fakeFile) - }) - - it('supports a skipDefaultValues function', (done) => { - let enResult - let arResult - const i18nextParser = new i18nTransform({ - locales: ['en', 'ar'], - skipDefaultValues: (locale, namespace) => locale === 'en', - }) - const fakeFile = new Vinyl({ - contents: Buffer.from("t('first', { defaultValue: 'default' })"), - path: 'file.js', - }) - - i18nextParser.on('data', (file) => { - if (file.relative.endsWith(enLibraryPath)) { - enResult = JSON.parse(file.contents) - } else if (file.relative.endsWith(arLibraryPath)) { - arResult = JSON.parse(file.contents) - } - }) - i18nextParser.once('end', () => { - assert.deepEqual(enResult, { first: '' }) - assert.deepEqual(arResult, { first: 'default' }) + assert.deepEqual(result, { first: 'en:translation:first:myDefault' }) done() }) @@ -1527,37 +1471,6 @@ describe('parser', () => { i18nextParser.end(fakeFile) }) - it('supports useKeysAsDefaultValue', (done) => { - let result - const i18nextParser = new i18nTransform({ - useKeysAsDefaultValue: true, - }) - const fakeFile = new Vinyl({ - contents: Buffer.from( - "t('first'); \n t('second and third'); t('$fourth %fifth%'); t('six.seven');" - ), - path: 'file.js', - }) - - i18nextParser.once('data', (file) => { - if (file.relative.endsWith(enLibraryPath)) { - result = JSON.parse(file.contents) - } - }) - i18nextParser.on('end', () => { - assert.deepEqual(result, { - first: 'first', - 'second and third': 'second and third', - '$fourth %fifth%': '$fourth %fifth%', - six: { - seven: 'six.seven', - }, - }) - done() - }) - i18nextParser.end(fakeFile) - }) - it('generates plurals', (done) => { let result const i18nextParser = new i18nTransform() @@ -1719,10 +1632,10 @@ describe('parser', () => { i18nextParser.end(fakeFile) }) - it('generates plurals with key as value', (done) => { + it('generates plurals for defaultValue function', (done) => { let result const i18nextParser = new i18nTransform({ - useKeysAsDefaultValue: true, + defaultValue: (locale, namespace, key, defaultValue) => `${key}`, }) const fakeFile = new Vinyl({ contents: Buffer.from("t('test {{count}}', { count: 1 })"), @@ -1835,10 +1748,10 @@ describe('parser', () => { i18nextParser.end(fakeFile) }) - it('generates plurals with key as value for languages with multiple plural forms', (done) => { + it('generates plurals for defaultValue function for languages with multiple plural forms', (done) => { let result const i18nextParser = new i18nTransform({ - useKeysAsDefaultValue: true, + defaultValue: (locale, namespace, key, defaultValue) => `${key}`, locales: ['ar'], }) const fakeFile = new Vinyl({ @@ -1894,36 +1807,6 @@ describe('parser', () => { i18nextParser.end(fakeFile) }) - it('supports skipDefaultValues option', (done) => { - let result - const i18nextParser = new i18nTransform({ - skipDefaultValues: true, - }) - - const fakeFile = new Vinyl({ - contents: Buffer.from( - "t('headline1', 'There will be a headline here.') \n" + - "t('headline2', {defaultValue: 'Another Headline here'}})" - ), - path: 'file.js', - }) - - i18nextParser.on('data', (file) => { - result = JSON.parse(file.contents) - }) - - i18nextParser.on('end', () => { - assert.deepEqual(result, { - headline1: '', - headline2: '', - }) - - done() - }) - - i18nextParser.end(fakeFile) - }) - it('supports customValueTemplate option', (done) => { let result const i18nextParser = new i18nTransform({ From 30a61f2ff5557c067cf0df13079d8592e6cea8ef Mon Sep 17 00:00:00 2001 From: Jonathan Sun Date: Mon, 7 Nov 2022 22:33:52 -0800 Subject: [PATCH 2/6] Update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a3ad34b8..8d8118e6 100644 --- a/README.md +++ b/README.md @@ -127,8 +127,8 @@ export default { // Default namespace used in your i18next config defaultValue: '', - // Default value to give to empty keys - // You may also specify a function accepting the locale, namespace, and key as arguments + // Default value to give to keys with no value + // You may also specify a function accepting the locale, namespace, and key, and value as arguments indentation: 2, // Indentation of the catalog files From 0d24717eabe90991491c6d94719f44c4a160bffe Mon Sep 17 00:00:00 2001 From: Jonathan Sun Date: Mon, 7 Nov 2022 22:36:52 -0800 Subject: [PATCH 3/6] Simplify test args --- test/parser.test.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/parser.test.js b/test/parser.test.js index 6ffa95b3..b931cc5f 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -1085,8 +1085,8 @@ describe('parser', () => { it('supports a defaultValue function', (done) => { let result const i18nextParser = new i18nTransform({ - defaultValue: (locale, namespace, key, defaultValue) => - `${locale}:${namespace}:${key}:${defaultValue}`, + defaultValue: (locale, namespace, key, value) => + `${locale}:${namespace}:${key}:${value}`, }) const fakeFile = new Vinyl({ contents: Buffer.from("t('first', 'myDefault')"), @@ -1635,7 +1635,7 @@ describe('parser', () => { it('generates plurals for defaultValue function', (done) => { let result const i18nextParser = new i18nTransform({ - defaultValue: (locale, namespace, key, defaultValue) => `${key}`, + defaultValue: (locale, namespace, key, value) => `${key}`, }) const fakeFile = new Vinyl({ contents: Buffer.from("t('test {{count}}', { count: 1 })"), @@ -1751,7 +1751,7 @@ describe('parser', () => { it('generates plurals for defaultValue function for languages with multiple plural forms', (done) => { let result const i18nextParser = new i18nTransform({ - defaultValue: (locale, namespace, key, defaultValue) => `${key}`, + defaultValue: (locale, namespace, key, value) => `${key}`, locales: ['ar'], }) const fakeFile = new Vinyl({ From 3ea0f4c5611407ec47a4acb03333f308df4f4d30 Mon Sep 17 00:00:00 2001 From: Jonathan Sun Date: Mon, 7 Nov 2022 22:49:38 -0800 Subject: [PATCH 4/6] Add conditional test --- test/parser.test.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/test/parser.test.js b/test/parser.test.js index b931cc5f..8db54ca4 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -1106,6 +1106,33 @@ describe('parser', () => { i18nextParser.end(fakeFile) }) + it('supports a defaultValue function with conditionals', (done) => { + let result + const i18nextParser = new i18nTransform({ + defaultValue: (locale, namespace, key, value) => + value ? value : `${key}`, + }) + const fakeFile = new Vinyl({ + contents: Buffer.from("t('first', 'myDefault'); t('second')"), + path: 'file.js', + }) + + i18nextParser.on('data', (file) => { + if (file.relative.endsWith(enLibraryPath)) { + result = JSON.parse(file.contents) + } + }) + i18nextParser.once('end', () => { + assert.deepEqual(result, { + first: 'myDefault', + second: 'second', + }) + done() + }) + + i18nextParser.end(fakeFile) + }) + it('supports a lineEnding', (done) => { let result const i18nextParser = new i18nTransform({ From 8966ee4a3262ed9c4299ece48cf6dbb860091bd9 Mon Sep 17 00:00:00 2001 From: Jonathan Sun Date: Mon, 7 Nov 2022 23:16:36 -0800 Subject: [PATCH 5/6] Handle skipping default values --- src/helpers.js | 11 ++++------- test/parser.test.js | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index acc1ed95..570bad45 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -35,15 +35,12 @@ function dotPathToHash(entry, target = {}, options = {}) { } const defaultValue = - typeof options.value === 'function' - ? options.value(options.locale, entry.namespace, key, entry.defaultValue) - : options.value + entry[`defaultValue${options.suffix}`] || entry.defaultValue || '' let newValue = - defaultValue || - entry[`defaultValue${options.suffix}`] || - entry.defaultValue || - '' + typeof options.value === 'function' + ? options.value(options.locale, entry.namespace, key, defaultValue) + : options.value || defaultValue if (path.endsWith(separator)) { path = path.slice(0, -separator.length) diff --git a/test/parser.test.js b/test/parser.test.js index 8db54ca4..089b2c03 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -1106,6 +1106,29 @@ describe('parser', () => { i18nextParser.end(fakeFile) }) + it('supports a defaultValue function with empty result', (done) => { + let result + const i18nextParser = new i18nTransform({ + defaultValue: (locale, namespace, key, value) => '', + }) + const fakeFile = new Vinyl({ + contents: Buffer.from("t('first', 'myDefault')"), + path: 'file.js', + }) + + i18nextParser.on('data', (file) => { + if (file.relative.endsWith(enLibraryPath)) { + result = JSON.parse(file.contents) + } + }) + i18nextParser.once('end', () => { + assert.deepEqual(result, { first: '' }) + done() + }) + + i18nextParser.end(fakeFile) + }) + it('supports a defaultValue function with conditionals', (done) => { let result const i18nextParser = new i18nTransform({ From c9eb316db4368f44f7ad613367d155641d3cf1c7 Mon Sep 17 00:00:00 2001 From: Jonathan Sun Date: Tue, 8 Nov 2022 21:47:05 -0800 Subject: [PATCH 6/6] PR comments --- README.md | 2 +- test/parser.test.js | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8d8118e6..4aaa0129 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ export default { defaultValue: '', // Default value to give to keys with no value - // You may also specify a function accepting the locale, namespace, and key, and value as arguments + // You may also specify a function accepting the locale, namespace, key, and value as arguments indentation: 2, // Indentation of the catalog files diff --git a/test/parser.test.js b/test/parser.test.js index 089b2c03..a6b02334 100644 --- a/test/parser.test.js +++ b/test/parser.test.js @@ -1083,10 +1083,12 @@ describe('parser', () => { }) it('supports a defaultValue function', (done) => { - let result + let enResult + let arResult const i18nextParser = new i18nTransform({ defaultValue: (locale, namespace, key, value) => `${locale}:${namespace}:${key}:${value}`, + locales: ['en', 'ar'], }) const fakeFile = new Vinyl({ contents: Buffer.from("t('first', 'myDefault')"), @@ -1095,11 +1097,14 @@ describe('parser', () => { i18nextParser.on('data', (file) => { if (file.relative.endsWith(enLibraryPath)) { - result = JSON.parse(file.contents) + enResult = JSON.parse(file.contents) + } else if (file.relative.endsWith(arLibraryPath)) { + arResult = JSON.parse(file.contents) } }) i18nextParser.once('end', () => { - assert.deepEqual(result, { first: 'en:translation:first:myDefault' }) + assert.deepEqual(enResult, { first: 'en:translation:first:myDefault' }) + assert.deepEqual(arResult, { first: 'ar:translation:first:myDefault' }) done() })