Repository: i18next/i18next-parser
Pull request #297 (2021-04-27 05:03:29)

Bump i18next from 20.2.1 to 20.2.2
Bumps [i18next](https://github.com/i18next/i18next) from 20.2.1 to 20.2.2.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/i18next/i18next/blob/master/CHANGELOG.md">i18next's changelog</a>.</em></p>
<blockquote>
<h3>20.2.2</h3>
<ul>
<li>fix plural equations for Kazakh <a href="https://github-redirect.dependabot.com/i18next/i18next/pull/1608">1608</a></li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/i18next/i18next/commit/44f26a6a6dfed51bb93cd2a176f15c5ae334a7c9"><code>44f26a6</code></a> 20.2.2</li>
<li><a href="https://github.com/i18next/i18next/commit/16e16af5cb84abe3d8122b14c30936dc95b5c6f3"><code>16e16af</code></a> prepare version</li>
<li><a href="https://github.com/i18next/i18next/commit/5e6780c6da9a97f261b5b25bc890d39bda71c7c0"><code>5e6780c</code></a> fix plural equations for Kazakh (<a href="https://github-redirect.dependabot.com/i18next/i18next/issues/1608">#1608</a>)</li>
<li>See full diff in <a href="https://github.com/i18next/i18next/compare/v20.2.1...v20.2.2">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=i18next&package-manager=npm_and_yarn&previous-version=20.2.1&new-version=20.2.2)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>


Patch:
diff --git a/.gitignore b/.gitignore
index f01319f..761e45c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,6 +19,7 @@ Thumbs.db
 *.swp
 /sftp-config.json
 /.vscode
+.history
 
 # node files
 node_modules/
diff --git a/README.md b/README.md
index 75dad2a..cde484a 100644
--- a/README.md
+++ b/README.md
@@ -126,6 +126,7 @@ module.exports = {
 
   defaultValue: '',
   // Default value to give to empty keys
+  // You may also specify a function accepting the locale, namespace, and key as arguments
 
   indentation: 2,
   // Indentation of the catalog files
@@ -177,11 +178,13 @@ module.exports = {
   // Whether or not to sort the catalog
 
   skipDefaultValues: false,
-  // Whether to ignore default values.
+  // 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/dist/helpers.js b/dist/helpers.js
index 7152d30..a288b29 100644
--- a/dist/helpers.js
+++ b/dist/helpers.js
@@ -18,21 +18,39 @@ function dotPathToHash(entry) {var target = arguments.length > 1 && arguments[1]
   }
 
   var separator = options.separator || '.';
+
+  var key = entry.keyWithNamespace.substring(
+  entry.keyWithNamespace.indexOf(separator) + separator.length,
+  entry.keyWithNamespace.length);
+
+
+  var defaultValue =
+  typeof options.value === 'function' ?
+  options.value(options.locale, entry.namespace, key) :
+  options.value;
+
+  var skipDefaultValues =
+  typeof options.skipDefaultValues === 'function' ?
+  options.skipDefaultValues(options.locale, entry.namespace) :
+  options.skipDefaultValues;
+
+  var useKeysAsDefaultValue =
+  typeof options.useKeysAsDefaultValue === 'function' ?
+  options.useKeysAsDefaultValue(options.locale, entry.namespace) :
+  options.useKeysAsDefaultValue;
+
   var newValue =
   entry["defaultValue_".concat(options.suffix)] ||
   entry.defaultValue ||
-  options.value ||
+  defaultValue ||
   '';
 
-  if (options.skipDefaultValues) {
+  if (skipDefaultValues) {
     newValue = '';
   }
 
-  if (options.useKeysAsDefaultValue) {
-    newValue = entry.keyWithNamespace.substring(
-    entry.keyWithNamespace.indexOf(separator) + separator.length,
-    entry.keyWithNamespace.length);
-
+  if (useKeysAsDefaultValue) {
+    newValue = key;
   }
 
   if (path.endsWith(separator)) {
diff --git a/dist/transform.js b/dist/transform.js
index 43f7317..05e4fee 100644
--- a/dist/transform.js
+++ b/dist/transform.js
@@ -140,6 +140,7 @@ i18nTransform = /*#__PURE__*/function (_Transform) {_inherits(i18nTransform, _Tr
           var transformEntry = function transformEntry(entry, suffix) {
             var _dotPathToHash = (0, _helpers.dotPathToHash)(entry, catalog, {
               suffix: suffix,
+              locale: locale,
               separator: _this2.options.keySeparator,
               value: _this2.options.defaultValue,
               useKeysAsDefaultValue: _this2.options.useKeysAsDefaultValue,
diff --git a/src/helpers.js b/src/helpers.js
index 5a04803..c1aa922 100644
--- a/src/helpers.js
+++ b/src/helpers.js
@@ -18,21 +18,39 @@ function dotPathToHash(entry, target = {}, options = {}) {
   }
 
   const separator = options.separator || '.'
+
+  const key = entry.keyWithNamespace.substring(
+    entry.keyWithNamespace.indexOf(separator) + separator.length,
+    entry.keyWithNamespace.length
+  )
+
+  const defaultValue =
+    typeof options.value === 'function'
+      ? options.value(options.locale, entry.namespace, key)
+      : 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 =
     entry[`defaultValue_${options.suffix}`] ||
     entry.defaultValue ||
-    options.value ||
+    defaultValue ||
     ''
 
-  if (options.skipDefaultValues) {
+  if (skipDefaultValues) {
     newValue = ''
   }
 
-  if (options.useKeysAsDefaultValue) {
-    newValue = entry.keyWithNamespace.substring(
-      entry.keyWithNamespace.indexOf(separator) + separator.length,
-      entry.keyWithNamespace.length
-    )
+  if (useKeysAsDefaultValue) {
+    newValue = key
   }
 
   if (path.endsWith(separator)) {
diff --git a/src/transform.js b/src/transform.js
index e67531a..926f43e 100644
--- a/src/transform.js
+++ b/src/transform.js
@@ -140,6 +140,7 @@ export default class i18nTransform extends Transform {
       const transformEntry = (entry, suffix) => {
         const { duplicate, conflict } = dotPathToHash(entry, catalog, {
           suffix,
+          locale,
           separator: this.options.keySeparator,
           value: this.options.defaultValue,
           useKeysAsDefaultValue: this.options.useKeysAsDefaultValue,
diff --git a/yarn.lock b/yarn.lock
index 677da64..2443540 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -827,14 +827,7 @@
     pirates "^4.0.0"
     source-map-support "^0.5.16"
 
-"@babel/runtime@^7.12.0":
-  version "7.13.10"
-  resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.13.10.tgz#47d42a57b6095f4468da440388fdbad8bebf0d7d"
-  integrity sha512-4QPkjJq6Ns3V/RgpEahRk+AGfL0eO6RHHtTWoNNr5mO49G6B5+X6d6THgWEAvTrznU5xYpbAlVKRYcsCgh/Akw==
-  dependencies:
-    regenerator-runtime "^0.13.4"
-
-"@babel/runtime@^7.8.4":
+"@babel/runtime@^7.12.0", "@babel/runtime@^7.8.4":
   version "7.13.17"
   resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.13.17.tgz#8966d1fc9593bf848602f0662d6b4d0069e3a7ec"
   integrity sha512-NCdgJEelPTSh+FEFylhnP1ylq848l1z9t9N0j1Lfbcw0+KXGjsTvUmkxy+voLLXB5SOKMbLLx4jxYliGrYQseA==
@@ -2839,9 +2832,9 @@ https@^1.0.0:
   integrity sha1-PDfHrhqO65ZpBKKtHpdaGUt+06Q=
 
 i18next@^20.2.1:
-  version "20.2.1"
-  resolved "https://registry.yarnpkg.com/i18next/-/i18next-20.2.1.tgz#8aedfdc596f7d54b9d3f6d87bcb89214467cb785"
-  integrity sha512-JLruWDEQ3T6tKT6P7u+DsNtToMHUwUcQIYOMRcnNBdUhSfKkoIDUKdVDKgGtmqr//LrirxjADUdr3d5Gwbow6g==
+  version "20.2.2"
+  resolved "https://registry.yarnpkg.com/i18next/-/i18next-20.2.2.tgz#175644829dcf35546ba8c174583acfe449e4ef1a"
+  integrity sha512-uWCv9LzKpe+OwvnKKrb8CbJwgAhasQofD58cB0PQ6bTPXEl5PlItl5C4esmY8HtriLu9nrjc2Hi0IfYv3Fy8BQ==
   dependencies:
     "@babel/runtime" "^7.12.0"
 


Test patch:
diff --git a/test/parser.test.js b/test/parser.test.js
index d2470ee..874ef3c 100644
--- a/test/parser.test.js
+++ b/test/parser.test.js
@@ -721,6 +721,86 @@ describe('parser', () => {
       i18nextParser.end(fakeFile)
     })
 
+    it('supports a defaultValue function', (done) => {
+      let result
+      const i18nextParser = new i18nTransform({
+        defaultValue: (locale, namespace, key) =>
+          `${locale}:${namespace}:${key}`,
+      })
+      const fakeFile = new Vinyl({
+        contents: Buffer.from("t('first')"),
+        path: 'file.js',
+      })
+
+      i18nextParser.on('data', (file) => {
+        if (file.relative.endsWith(enLibraryPath)) {
+          result = JSON.parse(file.contents)
+        }
+      })
+      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' })
+        done()
+      })
+
+      i18nextParser.end(fakeFile)
+    })
+
     it('supports a lineEnding', (done) => {
       let result
       const i18nextParser = new i18nTransform({