i18next-cli 1.80.3 → 1.80.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/cli.js CHANGED
@@ -39,7 +39,7 @@ const program = new commander.Command();
39
39
  program
40
40
  .name('i18next-cli')
41
41
  .description('A unified, high-performance i18next CLI.')
42
- .version('1.80.3') // This string is replaced with the actual version at build time by rollup
42
+ .version('1.80.5') // This string is replaced with the actual version at build time by rollup
43
43
  .addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
44
44
  // new: global config override option
45
45
  program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
@@ -289,6 +289,23 @@ async function processFile(file, plugins, astVisitors, pluginContext, config, lo
289
289
  catch (err2) {
290
290
  throw new validation.ExtractorError('Failed to process file', file, err2);
291
291
  }
292
+ // Fallback for plugin output that is valid TypeScript but not valid TSX,
293
+ // e.g. a generic arrow function `<T>(x: T) => x` from `<script lang="ts">`
294
+ }
295
+ else if (isTSX && !isNativeExt && wasTransformedByPlugin) {
296
+ try {
297
+ ast = await core.parse(code, {
298
+ syntax: 'typescript',
299
+ tsx: false,
300
+ decorators: true,
301
+ dynamicImport: true,
302
+ comments: true,
303
+ });
304
+ logger$1.info?.(`Parsed ${file} using TypeScript fallback`);
305
+ }
306
+ catch (err2) {
307
+ throw new validation.ExtractorError('Failed to process file', file, err2);
308
+ }
292
309
  }
293
310
  else {
294
311
  throw new validation.ExtractorError('Failed to process file', file, err);
@@ -182,6 +182,10 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
182
182
  };
183
183
  // Prepare regex for natural language detection
184
184
  const naturalLanguageRegex = nestedObject.naturalLanguageRegexFor(nsSep, keySeparator ?? '.');
185
+ // Serialized Trans components, natural-language keys and keys with empty segments
186
+ // (e.g. "Loading..." would become { "Loading": { "": { "": "…" } } }) are stored flat.
187
+ // Every write goes through this, so plural and context variants land next to their base key.
188
+ const separatorFor = (key) => nestedObject.storedKeySeparator(key, keySeparator ?? '.', naturalLanguageRegex);
185
189
  // Build a set of base keys that accept context (only if preserveContextVariants is enabled)
186
190
  // These are keys that were called with a context parameter in the source code
187
191
  const keysAcceptingContext = new Set();
@@ -480,7 +484,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
480
484
  const isContextVariant = !shouldPreserve && contextVariants.isContextVariantOfAcceptingKey(existingKey, keysAcceptingContext, pluralSeparator, contextSeparator);
481
485
  if (shouldPreserve || (preserveContextVariants && isContextVariant)) {
482
486
  const value = nestedObject.getNestedValue(existingTranslations, existingKey, keySeparator ?? '.');
483
- nestedObject.setNestedValue(newTranslations, existingKey, value, keySeparator ?? '.');
487
+ nestedObject.setNestedValue(newTranslations, existingKey, value, separatorFor(existingKey));
484
488
  }
485
489
  }
486
490
  // PROPAGATE CONTEXT VARIANTS FROM PRIMARY TO SECONDARY (issue #242):
@@ -499,7 +503,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
499
503
  const isContextVariant = contextVariants.isContextVariantOfAcceptingKey(primaryKey, keysAcceptingContext, pluralSeparator, contextSeparator);
500
504
  if (!isContextVariant)
501
505
  continue;
502
- const separator = primaryKey.startsWith('<') ? false : (keySeparator ?? '.');
506
+ const separator = separatorFor(primaryKey);
503
507
  const alreadySet = nestedObject.getNestedValue(newTranslations, primaryKey, separator);
504
508
  if (alreadySet !== undefined)
505
509
  continue;
@@ -549,7 +553,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
549
553
  if (!isAlreadySet) {
550
554
  // This plural form is NOT being generated by our code, so preserve it
551
555
  const value = nestedObject.getNestedValue(existingTranslations, existingKey, keySeparator ?? '.');
552
- nestedObject.setNestedValue(newTranslations, existingKey, value, keySeparator ?? '.');
556
+ nestedObject.setNestedValue(newTranslations, existingKey, value, separatorFor(existingKey));
553
557
  }
554
558
  }
555
559
  }
@@ -574,7 +578,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
574
578
  if (hasBaseInExtracted) {
575
579
  // Preserve the existing _zero form
576
580
  const value = nestedObject.getNestedValue(existingTranslations, existingKey, keySeparator ?? '.');
577
- nestedObject.setNestedValue(newTranslations, existingKey, value, keySeparator ?? '.');
581
+ nestedObject.setNestedValue(newTranslations, existingKey, value, separatorFor(existingKey));
578
582
  }
579
583
  }
580
584
  }
@@ -637,7 +641,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
637
641
  const finalKey = isOrdinal
638
642
  ? `${base}${pluralSeparator}${form}`
639
643
  : `${base}${pluralSeparator}${form}`;
640
- const separator = finalKey.startsWith('<') ? false : (keySeparator ?? '.');
644
+ const separator = separatorFor(finalKey);
641
645
  const existingVariantValue = nestedObject.getNestedValue(existingTranslations, finalKey, separator);
642
646
  if (existingVariantValue === undefined) {
643
647
  // Use the default value for secondary locale forms
@@ -660,7 +664,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
660
664
  const finalKey = isOrdinal
661
665
  ? `${base}${pluralSeparator}${form}`
662
666
  : `${base}${pluralSeparator}${form}`;
663
- const separator = finalKey.startsWith('<') ? false : (keySeparator ?? '.');
667
+ const separator = separatorFor(finalKey);
664
668
  const existingVariantValue = nestedObject.getNestedValue(existingTranslations, finalKey, separator);
665
669
  if (existingVariantValue !== undefined) {
666
670
  // Preserve manually-added forms
@@ -683,7 +687,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
683
687
  const finalKey = isOrdinal
684
688
  ? `${base}${pluralSeparator}${form}`
685
689
  : `${base}${pluralSeparator}${form}`;
686
- const separator = finalKey.startsWith('<') ? false : (keySeparator ?? '.');
690
+ const separator = separatorFor(finalKey);
687
691
  const existingVariantValue = nestedObject.getNestedValue(existingTranslations, finalKey, separator);
688
692
  if (existingVariantValue === undefined) {
689
693
  // Prefer explicit defaultValue extracted for this key; fall back to configured defaultValue
@@ -711,9 +715,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
711
715
  }
712
716
  }
713
717
  }
714
- // Serialized Trans components, natural-language keys and keys with empty segments
715
- // (e.g. "Loading..." would become { "Loading": { "": { "": "…" } } }) are stored flat
716
- const separator = nestedObject.storedKeySeparator(key, keySeparator ?? '.', naturalLanguageRegex);
718
+ const separator = separatorFor(key);
717
719
  const existingValue = nestedObject.getNestedValue(existingTranslations, key, separator);
718
720
  const primaryExistingValue = locale === primaryLanguage
719
721
  ? existingValue
@@ -864,7 +866,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
864
866
  for (const base of expandedBases) {
865
867
  for (const form of allLocalesCardinalCategories) {
866
868
  const finalKey = `${base}${pluralSeparator}${form}`;
867
- const separator = finalKey.startsWith('<') ? false : (keySeparator ?? '.');
869
+ const separator = separatorFor(finalKey);
868
870
  const existingInNew = nestedObject.getNestedValue(newTranslations, finalKey, separator);
869
871
  if (existingInNew === undefined) {
870
872
  const existingVariantValue = nestedObject.getNestedValue(existingTranslations, finalKey, separator);
@@ -885,6 +887,10 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
885
887
  const reorderToMatch = (newObj, refObj) => {
886
888
  if (typeof newObj !== 'object' || newObj === null || typeof refObj !== 'object' || refObj === null)
887
889
  return newObj;
890
+ // Arrays (e.g. values read with returnObjects) keep their own order;
891
+ // rebuilding them into `{}` would turn them into objects keyed "0", "1", …
892
+ if (Array.isArray(newObj))
893
+ return newObj;
888
894
  const ordered = {};
889
895
  // First: keys from refObj in original order
890
896
  for (const key of Object.keys(refObj)) {
@@ -291,6 +291,9 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
291
291
  let newCode = code;
292
292
  const nsSeparator = config.extract.nsSeparator ?? ':';
293
293
  const configuredFunctions = config.extract.functions || ['t', '*.t'];
294
+ // An optional string default value after the key: t('key', 'Default') or t('key', 'Default', { ... })
295
+ const stringLiteral = ['\'', '"', '`'].map(q => `${q}(?:[^${q}\\\\]|\\\\.)*${q}`).join('|');
296
+ const defaultValueArg = `(?:\\s*,\\s*(?:${stringLiteral}))?`;
294
297
  // Helper to create function-prefix regex fragment
295
298
  const fnPrefixToRegex = (fnPattern) => {
296
299
  if (fnPattern.startsWith('*.')) {
@@ -338,16 +341,16 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
338
341
  oldParts.namespace !== newParts.namespace &&
339
342
  config.extract.defaultNS === newParts.namespace &&
340
343
  hasKeyInNamespace(oldParts.namespace)) {
341
- const nsRegexToDefault = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*,\\s*\\{([^}]*)\\bns\\s*:\\s*(['"\`])${escapeRegex(oldParts.namespace)}\\3([^}]*)\\}\\s*\\)`, 'g');
344
+ const nsRegexToDefault = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*,\\s*\\{([^}]*)\\bns\\s*:\\s*(['"\`])${escapeRegex(oldParts.namespace)}\\3([^}]*)\\}\\s*\\)`, 'g');
342
345
  newCode = newCode.replace(nsRegexToDefault, (match, keyQ, beforeNs, nsQ, afterNs) => {
343
346
  changes++;
344
347
  const obj = (beforeNs + afterNs).replace(/,?\s*$/, '').replace(/^\s*,?/, '').trim();
345
348
  let updated = match.replace(new RegExp(`(['"\`])${escapeRegex(oldParts.key)}\\1`), `${keyQ}${newParts.key}${keyQ}`);
346
349
  if (obj) {
347
- updated = updated.replace(/\{\s*([^}]*)\s*\}/, `{${obj}}`);
350
+ updated = updated.replace(/\{[^}]*\}(?=\s*\)$)/, `{${obj}}`);
348
351
  }
349
352
  else {
350
- updated = updated.replace(/\s*,\s*\{[^}]*\}\s*\)/, ')');
353
+ updated = updated.replace(/\s*,\s*\{[^}]*\}\s*\)$/, ')');
351
354
  }
352
355
  return updated;
353
356
  });
@@ -360,7 +363,7 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
360
363
  // Only run if old namespace actually contains the key (to avoid touching unrelated ns calls).
361
364
  //
362
365
  if (oldParts.namespace && newParts.namespace && hasKeyInNamespace(oldParts.namespace)) {
363
- const nsRegexFullKey = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*,\\s*\\{([^}]*)\\bns\\s*:\\s*(['"\`])${escapeRegex(oldParts.namespace)}\\3([^}]*)\\}\\s*\\)`, 'g');
366
+ const nsRegexFullKey = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*,\\s*\\{([^}]*)\\bns\\s*:\\s*(['"\`])${escapeRegex(oldParts.namespace)}\\3([^}]*)\\}\\s*\\)`, 'g');
364
367
  newCode = newCode.replace(nsRegexFullKey, (match, keyQ, beforeNs, nsQ, afterNs) => {
365
368
  changes++;
366
369
  // remaining props except ns
@@ -370,7 +373,7 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
370
373
  if (oldParts.namespace === newParts.namespace) {
371
374
  // same namespace: keep ns value untouched, but keep other props
372
375
  if (obj) {
373
- updated = updated.replace(/\{\s*([^}]*)\s*\}/, `{${obj}}`);
376
+ updated = updated.replace(/\{[^}]*\}(?=\s*\)$)/, `{${obj}}`);
374
377
  }
375
378
  }
376
379
  else {
@@ -378,10 +381,10 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
378
381
  if (config.extract.defaultNS === newParts.namespace) {
379
382
  // moving INTO the default namespace -> remove the ns property
380
383
  if (obj) {
381
- updated = updated.replace(/\{\s*([^}]*)\s*\}/, `{${obj}}`);
384
+ updated = updated.replace(/\{[^}]*\}(?=\s*\)$)/, `{${obj}}`);
382
385
  }
383
386
  else {
384
- updated = updated.replace(/\s*,\s*\{[^}]*\}\s*\)/, ')');
387
+ updated = updated.replace(/\s*,\s*\{[^}]*\}\s*\)$/, ')');
385
388
  }
386
389
  }
387
390
  else {
@@ -464,7 +467,7 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
464
467
  oldParts.namespace !== newParts.namespace &&
465
468
  config.extract.defaultNS === oldParts.namespace &&
466
469
  hasKeyInNamespace(oldParts.namespace)) {
467
- const regexOptionsNoNs = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*,\\s*\\{([^}]*)\\}\\s*\\)`, 'g');
470
+ const regexOptionsNoNs = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*,\\s*\\{([^}]*)\\}\\s*\\)`, 'g');
468
471
  newCode = newCode.replace(regexOptionsNoNs, (match, q, objContents) => {
469
472
  // If object already contains ns, skip (other branches handle it).
470
473
  if (/\bns\s*:\s*['"`]/.test(objContents))
@@ -475,13 +478,13 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
475
478
  // replace the key and the object contents (preserve spacing minimally)
476
479
  return match
477
480
  .replace(new RegExp(`(['"\`])${escapeRegex(oldParts.key)}\\1`), `${q}${newParts.key}${q}`)
478
- .replace(/\{\s*([^}]*)\s*\}/, `{ ${newObj} }`);
481
+ .replace(/\{[^}]*\}(?=\s*\)$)/, `{ ${newObj} }`);
479
482
  });
480
483
  }
481
484
  // 4.5b) Same-namespace rename where call already has options object (no ns):
482
485
  // t('key', { user: 'name' }) -> t('key2', { user: 'name' })
483
486
  if (oldParts.namespace === newParts.namespace && oldParts.namespace === effectiveDefaultNS) {
484
- const regexKeyWithOptions = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*,\\s*\\{([^}]*)\\}\\s*\\)`, 'g');
487
+ const regexKeyWithOptions = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*,\\s*\\{([^}]*)\\}\\s*\\)`, 'g');
485
488
  newCode = newCode.replace(regexKeyWithOptions, (match, q, objContents) => {
486
489
  // don't touch objects that already explicitly set ns (handled elsewhere)
487
490
  if (/\bns\s*:\s*['"`]/.test(objContents))
@@ -500,10 +503,12 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
500
503
  oldParts.namespace !== newParts.namespace &&
501
504
  config.extract.defaultNS === oldParts.namespace &&
502
505
  hasKeyInNamespace(oldParts.namespace)) {
503
- const regexKeyWithParen = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*\\)`, 'g');
506
+ const regexKeyWithParen = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*\\)`, 'g');
504
507
  newCode = newCode.replace(regexKeyWithParen, (match, quote) => {
505
508
  changes++;
506
- return match.replace(new RegExp(`(['"\`])${escapeRegex(oldParts.key)}\\1\\s*\\)`), `${quote}${newParts.key}${quote}, { ns: '${newParts.namespace}' })`);
509
+ return match
510
+ .replace(new RegExp(`(['"\`])${escapeRegex(oldParts.key)}\\1`), `${quote}${newParts.key}${quote}`)
511
+ .replace(/\s*\)$/, `, { ns: '${newParts.namespace}' })`);
507
512
  });
508
513
  }
509
514
  //
@@ -517,7 +522,7 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
517
522
  {
518
523
  const effectiveDefaultNS = config.extract.defaultNS ?? 'translation';
519
524
  if (oldParts.namespace === effectiveDefaultNS) {
520
- const regexKeyNoOptions = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*\\)`, 'g');
525
+ const regexKeyNoOptions = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*\\)`, 'g');
521
526
  newCode = newCode.replace(regexKeyNoOptions, (match, q) => {
522
527
  changes++;
523
528
  const replacementKey = newParts.key;
package/dist/esm/cli.js CHANGED
@@ -33,7 +33,7 @@ const program = new Command();
33
33
  program
34
34
  .name('i18next-cli')
35
35
  .description('A unified, high-performance i18next CLI.')
36
- .version('1.80.3') // This string is replaced with the actual version at build time by rollup
36
+ .version('1.80.5') // This string is replaced with the actual version at build time by rollup
37
37
  .addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
38
38
  // new: global config override option
39
39
  program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
@@ -287,6 +287,23 @@ async function processFile(file, plugins, astVisitors, pluginContext, config, lo
287
287
  catch (err2) {
288
288
  throw new ExtractorError('Failed to process file', file, err2);
289
289
  }
290
+ // Fallback for plugin output that is valid TypeScript but not valid TSX,
291
+ // e.g. a generic arrow function `<T>(x: T) => x` from `<script lang="ts">`
292
+ }
293
+ else if (isTSX && !isNativeExt && wasTransformedByPlugin) {
294
+ try {
295
+ ast = await parse(code, {
296
+ syntax: 'typescript',
297
+ tsx: false,
298
+ decorators: true,
299
+ dynamicImport: true,
300
+ comments: true,
301
+ });
302
+ logger.info?.(`Parsed ${file} using TypeScript fallback`);
303
+ }
304
+ catch (err2) {
305
+ throw new ExtractorError('Failed to process file', file, err2);
306
+ }
290
307
  }
291
308
  else {
292
309
  throw new ExtractorError('Failed to process file', file, err);
@@ -180,6 +180,10 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
180
180
  };
181
181
  // Prepare regex for natural language detection
182
182
  const naturalLanguageRegex = naturalLanguageRegexFor(nsSep, keySeparator ?? '.');
183
+ // Serialized Trans components, natural-language keys and keys with empty segments
184
+ // (e.g. "Loading..." would become { "Loading": { "": { "": "…" } } }) are stored flat.
185
+ // Every write goes through this, so plural and context variants land next to their base key.
186
+ const separatorFor = (key) => storedKeySeparator(key, keySeparator ?? '.', naturalLanguageRegex);
183
187
  // Build a set of base keys that accept context (only if preserveContextVariants is enabled)
184
188
  // These are keys that were called with a context parameter in the source code
185
189
  const keysAcceptingContext = new Set();
@@ -478,7 +482,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
478
482
  const isContextVariant = !shouldPreserve && isContextVariantOfAcceptingKey(existingKey, keysAcceptingContext, pluralSeparator, contextSeparator);
479
483
  if (shouldPreserve || (preserveContextVariants && isContextVariant)) {
480
484
  const value = getNestedValue(existingTranslations, existingKey, keySeparator ?? '.');
481
- setNestedValue(newTranslations, existingKey, value, keySeparator ?? '.');
485
+ setNestedValue(newTranslations, existingKey, value, separatorFor(existingKey));
482
486
  }
483
487
  }
484
488
  // PROPAGATE CONTEXT VARIANTS FROM PRIMARY TO SECONDARY (issue #242):
@@ -497,7 +501,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
497
501
  const isContextVariant = isContextVariantOfAcceptingKey(primaryKey, keysAcceptingContext, pluralSeparator, contextSeparator);
498
502
  if (!isContextVariant)
499
503
  continue;
500
- const separator = primaryKey.startsWith('<') ? false : (keySeparator ?? '.');
504
+ const separator = separatorFor(primaryKey);
501
505
  const alreadySet = getNestedValue(newTranslations, primaryKey, separator);
502
506
  if (alreadySet !== undefined)
503
507
  continue;
@@ -547,7 +551,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
547
551
  if (!isAlreadySet) {
548
552
  // This plural form is NOT being generated by our code, so preserve it
549
553
  const value = getNestedValue(existingTranslations, existingKey, keySeparator ?? '.');
550
- setNestedValue(newTranslations, existingKey, value, keySeparator ?? '.');
554
+ setNestedValue(newTranslations, existingKey, value, separatorFor(existingKey));
551
555
  }
552
556
  }
553
557
  }
@@ -572,7 +576,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
572
576
  if (hasBaseInExtracted) {
573
577
  // Preserve the existing _zero form
574
578
  const value = getNestedValue(existingTranslations, existingKey, keySeparator ?? '.');
575
- setNestedValue(newTranslations, existingKey, value, keySeparator ?? '.');
579
+ setNestedValue(newTranslations, existingKey, value, separatorFor(existingKey));
576
580
  }
577
581
  }
578
582
  }
@@ -635,7 +639,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
635
639
  const finalKey = isOrdinal
636
640
  ? `${base}${pluralSeparator}${form}`
637
641
  : `${base}${pluralSeparator}${form}`;
638
- const separator = finalKey.startsWith('<') ? false : (keySeparator ?? '.');
642
+ const separator = separatorFor(finalKey);
639
643
  const existingVariantValue = getNestedValue(existingTranslations, finalKey, separator);
640
644
  if (existingVariantValue === undefined) {
641
645
  // Use the default value for secondary locale forms
@@ -658,7 +662,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
658
662
  const finalKey = isOrdinal
659
663
  ? `${base}${pluralSeparator}${form}`
660
664
  : `${base}${pluralSeparator}${form}`;
661
- const separator = finalKey.startsWith('<') ? false : (keySeparator ?? '.');
665
+ const separator = separatorFor(finalKey);
662
666
  const existingVariantValue = getNestedValue(existingTranslations, finalKey, separator);
663
667
  if (existingVariantValue !== undefined) {
664
668
  // Preserve manually-added forms
@@ -681,7 +685,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
681
685
  const finalKey = isOrdinal
682
686
  ? `${base}${pluralSeparator}${form}`
683
687
  : `${base}${pluralSeparator}${form}`;
684
- const separator = finalKey.startsWith('<') ? false : (keySeparator ?? '.');
688
+ const separator = separatorFor(finalKey);
685
689
  const existingVariantValue = getNestedValue(existingTranslations, finalKey, separator);
686
690
  if (existingVariantValue === undefined) {
687
691
  // Prefer explicit defaultValue extracted for this key; fall back to configured defaultValue
@@ -709,9 +713,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
709
713
  }
710
714
  }
711
715
  }
712
- // Serialized Trans components, natural-language keys and keys with empty segments
713
- // (e.g. "Loading..." would become { "Loading": { "": { "": "…" } } }) are stored flat
714
- const separator = storedKeySeparator(key, keySeparator ?? '.', naturalLanguageRegex);
716
+ const separator = separatorFor(key);
715
717
  const existingValue = getNestedValue(existingTranslations, key, separator);
716
718
  const primaryExistingValue = locale === primaryLanguage
717
719
  ? existingValue
@@ -862,7 +864,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
862
864
  for (const base of expandedBases) {
863
865
  for (const form of allLocalesCardinalCategories) {
864
866
  const finalKey = `${base}${pluralSeparator}${form}`;
865
- const separator = finalKey.startsWith('<') ? false : (keySeparator ?? '.');
867
+ const separator = separatorFor(finalKey);
866
868
  const existingInNew = getNestedValue(newTranslations, finalKey, separator);
867
869
  if (existingInNew === undefined) {
868
870
  const existingVariantValue = getNestedValue(existingTranslations, finalKey, separator);
@@ -883,6 +885,10 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
883
885
  const reorderToMatch = (newObj, refObj) => {
884
886
  if (typeof newObj !== 'object' || newObj === null || typeof refObj !== 'object' || refObj === null)
885
887
  return newObj;
888
+ // Arrays (e.g. values read with returnObjects) keep their own order;
889
+ // rebuilding them into `{}` would turn them into objects keyed "0", "1", …
890
+ if (Array.isArray(newObj))
891
+ return newObj;
886
892
  const ordered = {};
887
893
  // First: keys from refObj in original order
888
894
  for (const key of Object.keys(refObj)) {
@@ -289,6 +289,9 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
289
289
  let newCode = code;
290
290
  const nsSeparator = config.extract.nsSeparator ?? ':';
291
291
  const configuredFunctions = config.extract.functions || ['t', '*.t'];
292
+ // An optional string default value after the key: t('key', 'Default') or t('key', 'Default', { ... })
293
+ const stringLiteral = ['\'', '"', '`'].map(q => `${q}(?:[^${q}\\\\]|\\\\.)*${q}`).join('|');
294
+ const defaultValueArg = `(?:\\s*,\\s*(?:${stringLiteral}))?`;
292
295
  // Helper to create function-prefix regex fragment
293
296
  const fnPrefixToRegex = (fnPattern) => {
294
297
  if (fnPattern.startsWith('*.')) {
@@ -336,16 +339,16 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
336
339
  oldParts.namespace !== newParts.namespace &&
337
340
  config.extract.defaultNS === newParts.namespace &&
338
341
  hasKeyInNamespace(oldParts.namespace)) {
339
- const nsRegexToDefault = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*,\\s*\\{([^}]*)\\bns\\s*:\\s*(['"\`])${escapeRegex(oldParts.namespace)}\\3([^}]*)\\}\\s*\\)`, 'g');
342
+ const nsRegexToDefault = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*,\\s*\\{([^}]*)\\bns\\s*:\\s*(['"\`])${escapeRegex(oldParts.namespace)}\\3([^}]*)\\}\\s*\\)`, 'g');
340
343
  newCode = newCode.replace(nsRegexToDefault, (match, keyQ, beforeNs, nsQ, afterNs) => {
341
344
  changes++;
342
345
  const obj = (beforeNs + afterNs).replace(/,?\s*$/, '').replace(/^\s*,?/, '').trim();
343
346
  let updated = match.replace(new RegExp(`(['"\`])${escapeRegex(oldParts.key)}\\1`), `${keyQ}${newParts.key}${keyQ}`);
344
347
  if (obj) {
345
- updated = updated.replace(/\{\s*([^}]*)\s*\}/, `{${obj}}`);
348
+ updated = updated.replace(/\{[^}]*\}(?=\s*\)$)/, `{${obj}}`);
346
349
  }
347
350
  else {
348
- updated = updated.replace(/\s*,\s*\{[^}]*\}\s*\)/, ')');
351
+ updated = updated.replace(/\s*,\s*\{[^}]*\}\s*\)$/, ')');
349
352
  }
350
353
  return updated;
351
354
  });
@@ -358,7 +361,7 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
358
361
  // Only run if old namespace actually contains the key (to avoid touching unrelated ns calls).
359
362
  //
360
363
  if (oldParts.namespace && newParts.namespace && hasKeyInNamespace(oldParts.namespace)) {
361
- const nsRegexFullKey = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*,\\s*\\{([^}]*)\\bns\\s*:\\s*(['"\`])${escapeRegex(oldParts.namespace)}\\3([^}]*)\\}\\s*\\)`, 'g');
364
+ const nsRegexFullKey = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*,\\s*\\{([^}]*)\\bns\\s*:\\s*(['"\`])${escapeRegex(oldParts.namespace)}\\3([^}]*)\\}\\s*\\)`, 'g');
362
365
  newCode = newCode.replace(nsRegexFullKey, (match, keyQ, beforeNs, nsQ, afterNs) => {
363
366
  changes++;
364
367
  // remaining props except ns
@@ -368,7 +371,7 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
368
371
  if (oldParts.namespace === newParts.namespace) {
369
372
  // same namespace: keep ns value untouched, but keep other props
370
373
  if (obj) {
371
- updated = updated.replace(/\{\s*([^}]*)\s*\}/, `{${obj}}`);
374
+ updated = updated.replace(/\{[^}]*\}(?=\s*\)$)/, `{${obj}}`);
372
375
  }
373
376
  }
374
377
  else {
@@ -376,10 +379,10 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
376
379
  if (config.extract.defaultNS === newParts.namespace) {
377
380
  // moving INTO the default namespace -> remove the ns property
378
381
  if (obj) {
379
- updated = updated.replace(/\{\s*([^}]*)\s*\}/, `{${obj}}`);
382
+ updated = updated.replace(/\{[^}]*\}(?=\s*\)$)/, `{${obj}}`);
380
383
  }
381
384
  else {
382
- updated = updated.replace(/\s*,\s*\{[^}]*\}\s*\)/, ')');
385
+ updated = updated.replace(/\s*,\s*\{[^}]*\}\s*\)$/, ')');
383
386
  }
384
387
  }
385
388
  else {
@@ -462,7 +465,7 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
462
465
  oldParts.namespace !== newParts.namespace &&
463
466
  config.extract.defaultNS === oldParts.namespace &&
464
467
  hasKeyInNamespace(oldParts.namespace)) {
465
- const regexOptionsNoNs = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*,\\s*\\{([^}]*)\\}\\s*\\)`, 'g');
468
+ const regexOptionsNoNs = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*,\\s*\\{([^}]*)\\}\\s*\\)`, 'g');
466
469
  newCode = newCode.replace(regexOptionsNoNs, (match, q, objContents) => {
467
470
  // If object already contains ns, skip (other branches handle it).
468
471
  if (/\bns\s*:\s*['"`]/.test(objContents))
@@ -473,13 +476,13 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
473
476
  // replace the key and the object contents (preserve spacing minimally)
474
477
  return match
475
478
  .replace(new RegExp(`(['"\`])${escapeRegex(oldParts.key)}\\1`), `${q}${newParts.key}${q}`)
476
- .replace(/\{\s*([^}]*)\s*\}/, `{ ${newObj} }`);
479
+ .replace(/\{[^}]*\}(?=\s*\)$)/, `{ ${newObj} }`);
477
480
  });
478
481
  }
479
482
  // 4.5b) Same-namespace rename where call already has options object (no ns):
480
483
  // t('key', { user: 'name' }) -> t('key2', { user: 'name' })
481
484
  if (oldParts.namespace === newParts.namespace && oldParts.namespace === effectiveDefaultNS) {
482
- const regexKeyWithOptions = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*,\\s*\\{([^}]*)\\}\\s*\\)`, 'g');
485
+ const regexKeyWithOptions = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*,\\s*\\{([^}]*)\\}\\s*\\)`, 'g');
483
486
  newCode = newCode.replace(regexKeyWithOptions, (match, q, objContents) => {
484
487
  // don't touch objects that already explicitly set ns (handled elsewhere)
485
488
  if (/\bns\s*:\s*['"`]/.test(objContents))
@@ -498,10 +501,12 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
498
501
  oldParts.namespace !== newParts.namespace &&
499
502
  config.extract.defaultNS === oldParts.namespace &&
500
503
  hasKeyInNamespace(oldParts.namespace)) {
501
- const regexKeyWithParen = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*\\)`, 'g');
504
+ const regexKeyWithParen = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*\\)`, 'g');
502
505
  newCode = newCode.replace(regexKeyWithParen, (match, quote) => {
503
506
  changes++;
504
- return match.replace(new RegExp(`(['"\`])${escapeRegex(oldParts.key)}\\1\\s*\\)`), `${quote}${newParts.key}${quote}, { ns: '${newParts.namespace}' })`);
507
+ return match
508
+ .replace(new RegExp(`(['"\`])${escapeRegex(oldParts.key)}\\1`), `${quote}${newParts.key}${quote}`)
509
+ .replace(/\s*\)$/, `, { ns: '${newParts.namespace}' })`);
505
510
  });
506
511
  }
507
512
  //
@@ -515,7 +520,7 @@ function replaceKeyWithRegex(code, oldParts, newParts, config, namespaceKeyMap)
515
520
  {
516
521
  const effectiveDefaultNS = config.extract.defaultNS ?? 'translation';
517
522
  if (oldParts.namespace === effectiveDefaultNS) {
518
- const regexKeyNoOptions = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1\\s*\\)`, 'g');
523
+ const regexKeyNoOptions = new RegExp(`${prefix}\\s*\\(\\s*(['"\`])${escapeRegex(oldParts.key)}\\1${defaultValueArg}\\s*\\)`, 'g');
519
524
  newCode = newCode.replace(regexKeyNoOptions, (match, q) => {
520
525
  changes++;
521
526
  const replacementKey = newParts.key;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-cli",
3
- "version": "1.80.3",
3
+ "version": "1.80.5",
4
4
  "description": "A unified, high-performance i18next CLI.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1 +1 @@
1
- {"version":3,"file":"extractor.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/extractor.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,EAAE,aAAa,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAA;AAO5G,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAQ/C;;;;;;;;;;;;;;;;;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S,CAAC,EAC7C,MAAM,GAAE,MAA4B,EACpC,UAAU,CAAC,EAAE,MAAM,EAAE,GACpB,OAAO,CAAC,IAAI,CAAC,CA4Df;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAsB,OAAO,CAAE,MAAM,EAAE,oBAAoB,EAAE,EAAE,uBAA+B,EAAE,GAAE;IAAE,uBAAuB,CAAC,EAAE,OAAO,CAAA;CAAO,GAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC,CAO1K"}
@@ -1 +1 @@
1
- {"version":3,"file":"translation-manager.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/translation-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AAysC9F;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,EAC/B,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,EACvB,MAAM,EAAE,oBAAoB,EAC5B,EACE,uBAA+B,EAC/B,OAAe,EACf,oBAA4B,EAC5B,MAA4B,EAC5B,YAAiB,EAClB,GAAE;IACD,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,YAAY,EAAE,CAAA;CACzB,GACL,OAAO,CAAC,iBAAiB,EAAE,CAAC,CA6K9B"}
1
+ {"version":3,"file":"translation-manager.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/translation-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAA;AA8sC9F;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,EAC/B,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,EACvB,MAAM,EAAE,oBAAoB,EAC5B,EACE,uBAA+B,EAC/B,OAAe,EACf,oBAA4B,EAC5B,MAA4B,EAC5B,YAAiB,EAClB,GAAE;IACD,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,oBAAoB,CAAC,EAAE,OAAO,CAAC;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,YAAY,EAAE,CAAA;CACzB,GACL,OAAO,CAAC,iBAAiB,EAAE,CAAC,CA6K9B"}