i18next-cli 1.80.1 → 1.80.3
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 +1 -1
- package/dist/cjs/extractor/core/translation-manager.js +4 -45
- package/dist/cjs/rename-key.js +10 -4
- package/dist/cjs/syncer.js +11 -4
- package/dist/cjs/utils/nested-object.js +38 -1
- package/dist/esm/cli.js +1 -1
- package/dist/esm/extractor/core/translation-manager.js +5 -46
- package/dist/esm/rename-key.js +11 -5
- package/dist/esm/syncer.js +11 -4
- package/dist/esm/utils/nested-object.js +37 -2
- package/package.json +2 -2
- package/types/extractor/core/translation-manager.d.ts.map +1 -1
- package/types/syncer.d.ts.map +1 -1
- package/types/utils/nested-object.d.ts +11 -0
- package/types/utils/nested-object.d.ts.map +1 -1
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.
|
|
42
|
+
.version('1.80.3') // 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)');
|
|
@@ -11,8 +11,6 @@ var nesting = require('../../utils/nesting.js');
|
|
|
11
11
|
var messageFormat = require('../../utils/message-format.js');
|
|
12
12
|
var contextVariants = require('../../utils/context-variants.js');
|
|
13
13
|
|
|
14
|
-
// used for natural language check
|
|
15
|
-
const chars = [' ', ',', '?', '!', ';'];
|
|
16
14
|
const pluralForms = ['zero', 'one', 'two', 'few', 'many', 'other'];
|
|
17
15
|
/**
|
|
18
16
|
* Converts a glob pattern to a regular expression for matching keys
|
|
@@ -24,31 +22,6 @@ function globToRegex(glob) {
|
|
|
24
22
|
const regexString = `^${escaped.replace(/\*/g, '.*')}$`;
|
|
25
23
|
return new RegExp(regexString);
|
|
26
24
|
}
|
|
27
|
-
/**
|
|
28
|
-
* Checks if a key looks like an object path or natural language.
|
|
29
|
-
* (like in i18next)
|
|
30
|
-
*/
|
|
31
|
-
function looksLikeObjectPath(key, separator, regex) {
|
|
32
|
-
if (!regex)
|
|
33
|
-
return true;
|
|
34
|
-
let matched = !regex.test(key);
|
|
35
|
-
if (!matched) {
|
|
36
|
-
const ki = key.indexOf(separator);
|
|
37
|
-
if (ki > 0 && !regex.test(key.substring(0, ki))) {
|
|
38
|
-
matched = true;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return matched;
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Returns true when splitting `key` by `separator` would produce at least one
|
|
45
|
-
* empty string segment (e.g. "Loading..." split by "." → ["Loading","","",""]).
|
|
46
|
-
* Keys with empty segments must be treated as flat keys, not nested paths,
|
|
47
|
-
* otherwise they create `{ "": { "": "..." } }` entries in the JSON output.
|
|
48
|
-
*/
|
|
49
|
-
function hasEmptySegments(key, separator) {
|
|
50
|
-
return key.split(separator).some(s => s === '');
|
|
51
|
-
}
|
|
52
25
|
/**
|
|
53
26
|
* Detects a nesting conflict for `key` against the object being built.
|
|
54
27
|
* Returns the conflicting ancestor/descendant key path as a string when a
|
|
@@ -208,10 +181,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
|
|
|
208
181
|
return false;
|
|
209
182
|
};
|
|
210
183
|
// Prepare regex for natural language detection
|
|
211
|
-
const
|
|
212
|
-
const naturalLanguageRegex = possibleChars.length > 0
|
|
213
|
-
? new RegExp(`(${possibleChars.map((c) => (c === '?' ? '\\?' : c)).join('|')})`)
|
|
214
|
-
: null;
|
|
184
|
+
const naturalLanguageRegex = nestedObject.naturalLanguageRegexFor(nsSep, keySeparator ?? '.');
|
|
215
185
|
// Build a set of base keys that accept context (only if preserveContextVariants is enabled)
|
|
216
186
|
// These are keys that were called with a context parameter in the source code
|
|
217
187
|
const keysAcceptingContext = new Set();
|
|
@@ -741,20 +711,9 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
|
|
|
741
711
|
}
|
|
742
712
|
}
|
|
743
713
|
}
|
|
744
|
-
//
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
if (!looksLikeObjectPath(key, separator, naturalLanguageRegex)) {
|
|
748
|
-
// Natural-language key — treat as flat
|
|
749
|
-
separator = false;
|
|
750
|
-
}
|
|
751
|
-
else if (hasEmptySegments(key, separator)) {
|
|
752
|
-
// Splitting would produce empty-string segments (e.g. "Loading..." split by "."
|
|
753
|
-
// yields ["Loading","","",""]). Storing those creates { "": { "": "…" } }
|
|
754
|
-
// noise in the JSON, so treat the whole key as a flat leaf instead.
|
|
755
|
-
separator = false;
|
|
756
|
-
}
|
|
757
|
-
}
|
|
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);
|
|
758
717
|
const existingValue = nestedObject.getNestedValue(existingTranslations, key, separator);
|
|
759
718
|
const primaryExistingValue = locale === primaryLanguage
|
|
760
719
|
? existingValue
|
package/dist/cjs/rename-key.js
CHANGED
|
@@ -551,6 +551,12 @@ function escapeRegex(str) {
|
|
|
551
551
|
async function updateTranslationFiles(oldParts, newParts, config, dryRun, namespaceKeyMap, logger) {
|
|
552
552
|
const results = [];
|
|
553
553
|
const keySeparator = config.extract.keySeparator ?? '.';
|
|
554
|
+
// Write the new key the way the extractor would store it (flat for "Loading..." or
|
|
555
|
+
// natural-language keys), and remove the old one the way it is actually stored.
|
|
556
|
+
const nsSeparator = config.extract.nsSeparator;
|
|
557
|
+
const naturalLanguageRegex = nestedObject.naturalLanguageRegexFor(typeof nsSeparator === 'string' ? nsSeparator : ':', keySeparator);
|
|
558
|
+
const newKeySeparator = nestedObject.storedKeySeparator(newParts.key, keySeparator, naturalLanguageRegex);
|
|
559
|
+
const oldKeySeparatorIn = (translations) => Object.prototype.hasOwnProperty.call(translations, oldParts.key) ? false : keySeparator;
|
|
554
560
|
// Helper: determine whether a flattened key-set indicates presence of `baseKey`.
|
|
555
561
|
const namespaceHasKey = (set, baseKey) => {
|
|
556
562
|
if (!set)
|
|
@@ -677,8 +683,8 @@ async function updateTranslationFiles(oldParts, newParts, config, dryRun, namesp
|
|
|
677
683
|
if (oldValue !== undefined) {
|
|
678
684
|
if (ns === targetNamespace) {
|
|
679
685
|
// rename within same file (nested)
|
|
680
|
-
deleteNestedValue(oldTranslations, oldParts.key,
|
|
681
|
-
nestedObject.setNestedValue(oldTranslations, newParts.key, oldValue,
|
|
686
|
+
deleteNestedValue(oldTranslations, oldParts.key, oldKeySeparatorIn(oldTranslations));
|
|
687
|
+
nestedObject.setNestedValue(oldTranslations, newParts.key, oldValue, newKeySeparator);
|
|
682
688
|
if (!dryRun) {
|
|
683
689
|
const content = fileUtils.serializeTranslationFile(oldTranslations, config.extract.outputFormat, config.extract.indentation);
|
|
684
690
|
await promises.writeFile(oldFullPath, content, 'utf-8');
|
|
@@ -691,7 +697,7 @@ async function updateTranslationFiles(oldParts, newParts, config, dryRun, namesp
|
|
|
691
697
|
else {
|
|
692
698
|
// move nested value across namespaces
|
|
693
699
|
const updatedOld = { ...oldTranslations };
|
|
694
|
-
deleteNestedValue(updatedOld, oldParts.key,
|
|
700
|
+
deleteNestedValue(updatedOld, oldParts.key, oldKeySeparatorIn(updatedOld));
|
|
695
701
|
if (!dryRun) {
|
|
696
702
|
const contentOld = fileUtils.serializeTranslationFile(updatedOld, config.extract.outputFormat, config.extract.indentation);
|
|
697
703
|
await promises.writeFile(oldFullPath, contentOld, 'utf-8');
|
|
@@ -706,7 +712,7 @@ async function updateTranslationFiles(oldParts, newParts, config, dryRun, namesp
|
|
|
706
712
|
catch { }
|
|
707
713
|
if (!newTranslations)
|
|
708
714
|
newTranslations = {};
|
|
709
|
-
nestedObject.setNestedValue(newTranslations, newParts.key, oldValue,
|
|
715
|
+
nestedObject.setNestedValue(newTranslations, newParts.key, oldValue, newKeySeparator);
|
|
710
716
|
if (!dryRun) {
|
|
711
717
|
const contentNew = fileUtils.serializeTranslationFile(newTranslations, config.extract.outputFormat, config.extract.indentation);
|
|
712
718
|
await promises.writeFile(newFullPath, contentNew, 'utf-8');
|
package/dist/cjs/syncer.js
CHANGED
|
@@ -181,6 +181,13 @@ async function runSyncer(config, options = {}) {
|
|
|
181
181
|
// canonical order, a subsequent `sync` will read that order and reproduce
|
|
182
182
|
// it exactly — the pipeline becomes idempotent.
|
|
183
183
|
const existingSecondaryKeys = nestedObject.getNestedKeys(existingSecondaryTranslations, keySeparator ?? '.');
|
|
184
|
+
// Keep a key flat when the primary or the existing secondary file has it as one flat
|
|
185
|
+
// top-level key, which is how the extractor writes keys like "Loading..." or
|
|
186
|
+
// "Hello world. Bye". Splitting them on the separator would nest them under new parents.
|
|
187
|
+
const keySeparatorFor = (key) => Object.prototype.hasOwnProperty.call(primaryTranslations, key) ||
|
|
188
|
+
Object.prototype.hasOwnProperty.call(existingSecondaryTranslations, key)
|
|
189
|
+
? false
|
|
190
|
+
: (keySeparator ?? '.');
|
|
184
191
|
const handledKeys = new Set();
|
|
185
192
|
// Pass 1: existing keys in their current order (preserves extract's ordering)
|
|
186
193
|
for (const key of existingSecondaryKeys) {
|
|
@@ -188,7 +195,7 @@ async function runSyncer(config, options = {}) {
|
|
|
188
195
|
const primaryValue = nestedObject.getNestedValue(primaryTranslations, key, keySeparator ?? '.');
|
|
189
196
|
const existingValue = nestedObject.getNestedValue(existingSecondaryTranslations, key, keySeparator ?? '.');
|
|
190
197
|
const valueToSet = existingValue ?? defaultValue.resolveDefaultValue(defaultValue$1, key, ns, lang, primaryValue);
|
|
191
|
-
nestedObject.setNestedValue(newSecondaryTranslations, key, valueToSet,
|
|
198
|
+
nestedObject.setNestedValue(newSecondaryTranslations, key, valueToSet, keySeparatorFor(key));
|
|
192
199
|
handledKeys.add(key);
|
|
193
200
|
}
|
|
194
201
|
else if (isLocaleSpecificPluralExtension(key)) {
|
|
@@ -197,14 +204,14 @@ async function runSyncer(config, options = {}) {
|
|
|
197
204
|
// placeholder left by a previous (buggy) sync run and should not
|
|
198
205
|
// be perpetuated. Under --changed-only nothing is ever dropped.
|
|
199
206
|
if ((existingValue !== '' && existingValue != null) || isChangedKey) {
|
|
200
|
-
nestedObject.setNestedValue(newSecondaryTranslations, key, existingValue,
|
|
207
|
+
nestedObject.setNestedValue(newSecondaryTranslations, key, existingValue, keySeparatorFor(key));
|
|
201
208
|
handledKeys.add(key);
|
|
202
209
|
}
|
|
203
210
|
}
|
|
204
211
|
else if (isChangedKey) {
|
|
205
212
|
// --changed-only: removals are out of scope, keep obsolete keys as-is
|
|
206
213
|
const existingValue = nestedObject.getNestedValue(existingSecondaryTranslations, key, keySeparator ?? '.');
|
|
207
|
-
nestedObject.setNestedValue(newSecondaryTranslations, key, existingValue,
|
|
214
|
+
nestedObject.setNestedValue(newSecondaryTranslations, key, existingValue, keySeparatorFor(key));
|
|
208
215
|
}
|
|
209
216
|
// else: obsolete key — omit it from the output
|
|
210
217
|
}
|
|
@@ -216,7 +223,7 @@ async function runSyncer(config, options = {}) {
|
|
|
216
223
|
continue;
|
|
217
224
|
const primaryValue = nestedObject.getNestedValue(primaryTranslations, key, keySeparator ?? '.');
|
|
218
225
|
const valueToSet = defaultValue.resolveDefaultValue(defaultValue$1, key, ns, lang, primaryValue);
|
|
219
|
-
nestedObject.setNestedValue(newSecondaryTranslations, key, valueToSet,
|
|
226
|
+
nestedObject.setNestedValue(newSecondaryTranslations, key, valueToSet, keySeparatorFor(key));
|
|
220
227
|
}
|
|
221
228
|
}
|
|
222
229
|
untranslatedByLocale[lang] = (untranslatedByLocale[lang] ?? 0) + nestedObject.getNestedKeys(newSecondaryTranslations, keySeparator ?? '.')
|
|
@@ -74,7 +74,13 @@ function getNestedValue(obj, path, keySeparator) {
|
|
|
74
74
|
if (keySeparator === false) {
|
|
75
75
|
return obj[path];
|
|
76
76
|
}
|
|
77
|
-
|
|
77
|
+
const value = path.split(keySeparator).reduce((acc, key) => acc && acc[key], obj);
|
|
78
|
+
// The extractor writes a key that cannot be used as a path ("Loading...", or a
|
|
79
|
+
// natural-language key such as "Hello world. Bye") as a single flat top-level key.
|
|
80
|
+
if (value === undefined && obj != null && Object.prototype.hasOwnProperty.call(obj, path)) {
|
|
81
|
+
return obj[path];
|
|
82
|
+
}
|
|
83
|
+
return value;
|
|
78
84
|
}
|
|
79
85
|
/**
|
|
80
86
|
* Extracts all nested keys from an object, optionally with a prefix.
|
|
@@ -118,7 +124,38 @@ function getNestedKeys(obj, keySeparator, prefix = '') {
|
|
|
118
124
|
return acc;
|
|
119
125
|
}, []);
|
|
120
126
|
}
|
|
127
|
+
const naturalLanguageChars = [' ', ',', '?', '!', ';'];
|
|
128
|
+
/**
|
|
129
|
+
* Builds the regex that marks a key as natural language (like i18next does): any of
|
|
130
|
+
* ` ,?!;` that is not itself used as the namespace or key separator.
|
|
131
|
+
*/
|
|
132
|
+
function naturalLanguageRegexFor(nsSeparator, keySeparator) {
|
|
133
|
+
const possibleChars = naturalLanguageChars.filter((c) => nsSeparator.indexOf(c) < 0 && (typeof keySeparator === 'string' ? keySeparator.indexOf(c) < 0 : true));
|
|
134
|
+
return possibleChars.length > 0
|
|
135
|
+
? new RegExp(`(${possibleChars.map((c) => (c === '?' ? '\\?' : c)).join('|')})`)
|
|
136
|
+
: null;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* The separator a key is stored with in a translation file, or `false` when it is
|
|
140
|
+
* stored as one flat key: a serialized Trans component (`<...`), a natural-language
|
|
141
|
+
* key such as "Hello world. Bye", or a key with empty segments such as "Loading...".
|
|
142
|
+
*/
|
|
143
|
+
function storedKeySeparator(key, keySeparator, naturalLanguageRegex) {
|
|
144
|
+
if (keySeparator === false || key.startsWith('<'))
|
|
145
|
+
return false;
|
|
146
|
+
if (naturalLanguageRegex) {
|
|
147
|
+
const ki = key.indexOf(keySeparator);
|
|
148
|
+
const isPath = !naturalLanguageRegex.test(key) || (ki > 0 && !naturalLanguageRegex.test(key.substring(0, ki)));
|
|
149
|
+
if (!isPath)
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
if (key.split(keySeparator).some(s => s === ''))
|
|
153
|
+
return false;
|
|
154
|
+
return keySeparator;
|
|
155
|
+
}
|
|
121
156
|
|
|
122
157
|
exports.getNestedKeys = getNestedKeys;
|
|
123
158
|
exports.getNestedValue = getNestedValue;
|
|
159
|
+
exports.naturalLanguageRegexFor = naturalLanguageRegexFor;
|
|
124
160
|
exports.setNestedValue = setNestedValue;
|
|
161
|
+
exports.storedKeySeparator = storedKeySeparator;
|
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.
|
|
36
|
+
.version('1.80.3') // 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)');
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { resolve, basename, extname } from 'node:path';
|
|
2
2
|
import { glob } from 'glob';
|
|
3
|
-
import { getNestedKeys, getNestedValue, setNestedValue } from '../../utils/nested-object.js';
|
|
3
|
+
import { naturalLanguageRegexFor, getNestedKeys, getNestedValue, setNestedValue, storedKeySeparator } from '../../utils/nested-object.js';
|
|
4
4
|
import { getOutputPath, loadTranslationFile } from '../../utils/file-utils.js';
|
|
5
5
|
import { resolveDefaultValue } from '../../utils/default-value.js';
|
|
6
6
|
import { ConsoleLogger } from '../../utils/logger.js';
|
|
@@ -9,8 +9,6 @@ import { parseNestedReferences } from '../../utils/nesting.js';
|
|
|
9
9
|
import { usesMessageFormat } from '../../utils/message-format.js';
|
|
10
10
|
import { isContextVariantOfAcceptingKey } from '../../utils/context-variants.js';
|
|
11
11
|
|
|
12
|
-
// used for natural language check
|
|
13
|
-
const chars = [' ', ',', '?', '!', ';'];
|
|
14
12
|
const pluralForms = ['zero', 'one', 'two', 'few', 'many', 'other'];
|
|
15
13
|
/**
|
|
16
14
|
* Converts a glob pattern to a regular expression for matching keys
|
|
@@ -22,31 +20,6 @@ function globToRegex(glob) {
|
|
|
22
20
|
const regexString = `^${escaped.replace(/\*/g, '.*')}$`;
|
|
23
21
|
return new RegExp(regexString);
|
|
24
22
|
}
|
|
25
|
-
/**
|
|
26
|
-
* Checks if a key looks like an object path or natural language.
|
|
27
|
-
* (like in i18next)
|
|
28
|
-
*/
|
|
29
|
-
function looksLikeObjectPath(key, separator, regex) {
|
|
30
|
-
if (!regex)
|
|
31
|
-
return true;
|
|
32
|
-
let matched = !regex.test(key);
|
|
33
|
-
if (!matched) {
|
|
34
|
-
const ki = key.indexOf(separator);
|
|
35
|
-
if (ki > 0 && !regex.test(key.substring(0, ki))) {
|
|
36
|
-
matched = true;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return matched;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Returns true when splitting `key` by `separator` would produce at least one
|
|
43
|
-
* empty string segment (e.g. "Loading..." split by "." → ["Loading","","",""]).
|
|
44
|
-
* Keys with empty segments must be treated as flat keys, not nested paths,
|
|
45
|
-
* otherwise they create `{ "": { "": "..." } }` entries in the JSON output.
|
|
46
|
-
*/
|
|
47
|
-
function hasEmptySegments(key, separator) {
|
|
48
|
-
return key.split(separator).some(s => s === '');
|
|
49
|
-
}
|
|
50
23
|
/**
|
|
51
24
|
* Detects a nesting conflict for `key` against the object being built.
|
|
52
25
|
* Returns the conflicting ancestor/descendant key path as a string when a
|
|
@@ -206,10 +179,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
|
|
|
206
179
|
return false;
|
|
207
180
|
};
|
|
208
181
|
// Prepare regex for natural language detection
|
|
209
|
-
const
|
|
210
|
-
const naturalLanguageRegex = possibleChars.length > 0
|
|
211
|
-
? new RegExp(`(${possibleChars.map((c) => (c === '?' ? '\\?' : c)).join('|')})`)
|
|
212
|
-
: null;
|
|
182
|
+
const naturalLanguageRegex = naturalLanguageRegexFor(nsSep, keySeparator ?? '.');
|
|
213
183
|
// Build a set of base keys that accept context (only if preserveContextVariants is enabled)
|
|
214
184
|
// These are keys that were called with a context parameter in the source code
|
|
215
185
|
const keysAcceptingContext = new Set();
|
|
@@ -739,20 +709,9 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
|
|
|
739
709
|
}
|
|
740
710
|
}
|
|
741
711
|
}
|
|
742
|
-
//
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
if (!looksLikeObjectPath(key, separator, naturalLanguageRegex)) {
|
|
746
|
-
// Natural-language key — treat as flat
|
|
747
|
-
separator = false;
|
|
748
|
-
}
|
|
749
|
-
else if (hasEmptySegments(key, separator)) {
|
|
750
|
-
// Splitting would produce empty-string segments (e.g. "Loading..." split by "."
|
|
751
|
-
// yields ["Loading","","",""]). Storing those creates { "": { "": "…" } }
|
|
752
|
-
// noise in the JSON, so treat the whole key as a flat leaf instead.
|
|
753
|
-
separator = false;
|
|
754
|
-
}
|
|
755
|
-
}
|
|
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);
|
|
756
715
|
const existingValue = getNestedValue(existingTranslations, key, separator);
|
|
757
716
|
const primaryExistingValue = locale === primaryLanguage
|
|
758
717
|
? existingValue
|
package/dist/esm/rename-key.js
CHANGED
|
@@ -3,7 +3,7 @@ import { readFile, writeFile } from 'node:fs/promises';
|
|
|
3
3
|
import { resolve, basename } from 'node:path';
|
|
4
4
|
import { ConsoleLogger } from './utils/logger.js';
|
|
5
5
|
import { getOutputPath, loadTranslationFile, serializeTranslationFile } from './utils/file-utils.js';
|
|
6
|
-
import { getNestedValue, setNestedValue } from './utils/nested-object.js';
|
|
6
|
+
import { getNestedValue, naturalLanguageRegexFor, storedKeySeparator, setNestedValue } from './utils/nested-object.js';
|
|
7
7
|
import { shouldShowFunnel, recordFunnelShown } from './utils/funnel-msg-tracker.js';
|
|
8
8
|
import { styleText } from 'node:util';
|
|
9
9
|
|
|
@@ -549,6 +549,12 @@ function escapeRegex(str) {
|
|
|
549
549
|
async function updateTranslationFiles(oldParts, newParts, config, dryRun, namespaceKeyMap, logger) {
|
|
550
550
|
const results = [];
|
|
551
551
|
const keySeparator = config.extract.keySeparator ?? '.';
|
|
552
|
+
// Write the new key the way the extractor would store it (flat for "Loading..." or
|
|
553
|
+
// natural-language keys), and remove the old one the way it is actually stored.
|
|
554
|
+
const nsSeparator = config.extract.nsSeparator;
|
|
555
|
+
const naturalLanguageRegex = naturalLanguageRegexFor(typeof nsSeparator === 'string' ? nsSeparator : ':', keySeparator);
|
|
556
|
+
const newKeySeparator = storedKeySeparator(newParts.key, keySeparator, naturalLanguageRegex);
|
|
557
|
+
const oldKeySeparatorIn = (translations) => Object.prototype.hasOwnProperty.call(translations, oldParts.key) ? false : keySeparator;
|
|
552
558
|
// Helper: determine whether a flattened key-set indicates presence of `baseKey`.
|
|
553
559
|
const namespaceHasKey = (set, baseKey) => {
|
|
554
560
|
if (!set)
|
|
@@ -675,8 +681,8 @@ async function updateTranslationFiles(oldParts, newParts, config, dryRun, namesp
|
|
|
675
681
|
if (oldValue !== undefined) {
|
|
676
682
|
if (ns === targetNamespace) {
|
|
677
683
|
// rename within same file (nested)
|
|
678
|
-
deleteNestedValue(oldTranslations, oldParts.key,
|
|
679
|
-
setNestedValue(oldTranslations, newParts.key, oldValue,
|
|
684
|
+
deleteNestedValue(oldTranslations, oldParts.key, oldKeySeparatorIn(oldTranslations));
|
|
685
|
+
setNestedValue(oldTranslations, newParts.key, oldValue, newKeySeparator);
|
|
680
686
|
if (!dryRun) {
|
|
681
687
|
const content = serializeTranslationFile(oldTranslations, config.extract.outputFormat, config.extract.indentation);
|
|
682
688
|
await writeFile(oldFullPath, content, 'utf-8');
|
|
@@ -689,7 +695,7 @@ async function updateTranslationFiles(oldParts, newParts, config, dryRun, namesp
|
|
|
689
695
|
else {
|
|
690
696
|
// move nested value across namespaces
|
|
691
697
|
const updatedOld = { ...oldTranslations };
|
|
692
|
-
deleteNestedValue(updatedOld, oldParts.key,
|
|
698
|
+
deleteNestedValue(updatedOld, oldParts.key, oldKeySeparatorIn(updatedOld));
|
|
693
699
|
if (!dryRun) {
|
|
694
700
|
const contentOld = serializeTranslationFile(updatedOld, config.extract.outputFormat, config.extract.indentation);
|
|
695
701
|
await writeFile(oldFullPath, contentOld, 'utf-8');
|
|
@@ -704,7 +710,7 @@ async function updateTranslationFiles(oldParts, newParts, config, dryRun, namesp
|
|
|
704
710
|
catch { }
|
|
705
711
|
if (!newTranslations)
|
|
706
712
|
newTranslations = {};
|
|
707
|
-
setNestedValue(newTranslations, newParts.key, oldValue,
|
|
713
|
+
setNestedValue(newTranslations, newParts.key, oldValue, newKeySeparator);
|
|
708
714
|
if (!dryRun) {
|
|
709
715
|
const contentNew = serializeTranslationFile(newTranslations, config.extract.outputFormat, config.extract.indentation);
|
|
710
716
|
await writeFile(newFullPath, contentNew, 'utf-8');
|
package/dist/esm/syncer.js
CHANGED
|
@@ -179,6 +179,13 @@ async function runSyncer(config, options = {}) {
|
|
|
179
179
|
// canonical order, a subsequent `sync` will read that order and reproduce
|
|
180
180
|
// it exactly — the pipeline becomes idempotent.
|
|
181
181
|
const existingSecondaryKeys = getNestedKeys(existingSecondaryTranslations, keySeparator ?? '.');
|
|
182
|
+
// Keep a key flat when the primary or the existing secondary file has it as one flat
|
|
183
|
+
// top-level key, which is how the extractor writes keys like "Loading..." or
|
|
184
|
+
// "Hello world. Bye". Splitting them on the separator would nest them under new parents.
|
|
185
|
+
const keySeparatorFor = (key) => Object.prototype.hasOwnProperty.call(primaryTranslations, key) ||
|
|
186
|
+
Object.prototype.hasOwnProperty.call(existingSecondaryTranslations, key)
|
|
187
|
+
? false
|
|
188
|
+
: (keySeparator ?? '.');
|
|
182
189
|
const handledKeys = new Set();
|
|
183
190
|
// Pass 1: existing keys in their current order (preserves extract's ordering)
|
|
184
191
|
for (const key of existingSecondaryKeys) {
|
|
@@ -186,7 +193,7 @@ async function runSyncer(config, options = {}) {
|
|
|
186
193
|
const primaryValue = getNestedValue(primaryTranslations, key, keySeparator ?? '.');
|
|
187
194
|
const existingValue = getNestedValue(existingSecondaryTranslations, key, keySeparator ?? '.');
|
|
188
195
|
const valueToSet = existingValue ?? resolveDefaultValue(defaultValue, key, ns, lang, primaryValue);
|
|
189
|
-
setNestedValue(newSecondaryTranslations, key, valueToSet,
|
|
196
|
+
setNestedValue(newSecondaryTranslations, key, valueToSet, keySeparatorFor(key));
|
|
190
197
|
handledKeys.add(key);
|
|
191
198
|
}
|
|
192
199
|
else if (isLocaleSpecificPluralExtension(key)) {
|
|
@@ -195,14 +202,14 @@ async function runSyncer(config, options = {}) {
|
|
|
195
202
|
// placeholder left by a previous (buggy) sync run and should not
|
|
196
203
|
// be perpetuated. Under --changed-only nothing is ever dropped.
|
|
197
204
|
if ((existingValue !== '' && existingValue != null) || isChangedKey) {
|
|
198
|
-
setNestedValue(newSecondaryTranslations, key, existingValue,
|
|
205
|
+
setNestedValue(newSecondaryTranslations, key, existingValue, keySeparatorFor(key));
|
|
199
206
|
handledKeys.add(key);
|
|
200
207
|
}
|
|
201
208
|
}
|
|
202
209
|
else if (isChangedKey) {
|
|
203
210
|
// --changed-only: removals are out of scope, keep obsolete keys as-is
|
|
204
211
|
const existingValue = getNestedValue(existingSecondaryTranslations, key, keySeparator ?? '.');
|
|
205
|
-
setNestedValue(newSecondaryTranslations, key, existingValue,
|
|
212
|
+
setNestedValue(newSecondaryTranslations, key, existingValue, keySeparatorFor(key));
|
|
206
213
|
}
|
|
207
214
|
// else: obsolete key — omit it from the output
|
|
208
215
|
}
|
|
@@ -214,7 +221,7 @@ async function runSyncer(config, options = {}) {
|
|
|
214
221
|
continue;
|
|
215
222
|
const primaryValue = getNestedValue(primaryTranslations, key, keySeparator ?? '.');
|
|
216
223
|
const valueToSet = resolveDefaultValue(defaultValue, key, ns, lang, primaryValue);
|
|
217
|
-
setNestedValue(newSecondaryTranslations, key, valueToSet,
|
|
224
|
+
setNestedValue(newSecondaryTranslations, key, valueToSet, keySeparatorFor(key));
|
|
218
225
|
}
|
|
219
226
|
}
|
|
220
227
|
untranslatedByLocale[lang] = (untranslatedByLocale[lang] ?? 0) + getNestedKeys(newSecondaryTranslations, keySeparator ?? '.')
|
|
@@ -72,7 +72,13 @@ function getNestedValue(obj, path, keySeparator) {
|
|
|
72
72
|
if (keySeparator === false) {
|
|
73
73
|
return obj[path];
|
|
74
74
|
}
|
|
75
|
-
|
|
75
|
+
const value = path.split(keySeparator).reduce((acc, key) => acc && acc[key], obj);
|
|
76
|
+
// The extractor writes a key that cannot be used as a path ("Loading...", or a
|
|
77
|
+
// natural-language key such as "Hello world. Bye") as a single flat top-level key.
|
|
78
|
+
if (value === undefined && obj != null && Object.prototype.hasOwnProperty.call(obj, path)) {
|
|
79
|
+
return obj[path];
|
|
80
|
+
}
|
|
81
|
+
return value;
|
|
76
82
|
}
|
|
77
83
|
/**
|
|
78
84
|
* Extracts all nested keys from an object, optionally with a prefix.
|
|
@@ -116,5 +122,34 @@ function getNestedKeys(obj, keySeparator, prefix = '') {
|
|
|
116
122
|
return acc;
|
|
117
123
|
}, []);
|
|
118
124
|
}
|
|
125
|
+
const naturalLanguageChars = [' ', ',', '?', '!', ';'];
|
|
126
|
+
/**
|
|
127
|
+
* Builds the regex that marks a key as natural language (like i18next does): any of
|
|
128
|
+
* ` ,?!;` that is not itself used as the namespace or key separator.
|
|
129
|
+
*/
|
|
130
|
+
function naturalLanguageRegexFor(nsSeparator, keySeparator) {
|
|
131
|
+
const possibleChars = naturalLanguageChars.filter((c) => nsSeparator.indexOf(c) < 0 && (typeof keySeparator === 'string' ? keySeparator.indexOf(c) < 0 : true));
|
|
132
|
+
return possibleChars.length > 0
|
|
133
|
+
? new RegExp(`(${possibleChars.map((c) => (c === '?' ? '\\?' : c)).join('|')})`)
|
|
134
|
+
: null;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The separator a key is stored with in a translation file, or `false` when it is
|
|
138
|
+
* stored as one flat key: a serialized Trans component (`<...`), a natural-language
|
|
139
|
+
* key such as "Hello world. Bye", or a key with empty segments such as "Loading...".
|
|
140
|
+
*/
|
|
141
|
+
function storedKeySeparator(key, keySeparator, naturalLanguageRegex) {
|
|
142
|
+
if (keySeparator === false || key.startsWith('<'))
|
|
143
|
+
return false;
|
|
144
|
+
if (naturalLanguageRegex) {
|
|
145
|
+
const ki = key.indexOf(keySeparator);
|
|
146
|
+
const isPath = !naturalLanguageRegex.test(key) || (ki > 0 && !naturalLanguageRegex.test(key.substring(0, ki)));
|
|
147
|
+
if (!isPath)
|
|
148
|
+
return false;
|
|
149
|
+
}
|
|
150
|
+
if (key.split(keySeparator).some(s => s === ''))
|
|
151
|
+
return false;
|
|
152
|
+
return keySeparator;
|
|
153
|
+
}
|
|
119
154
|
|
|
120
|
-
export { getNestedKeys, getNestedValue, setNestedValue };
|
|
155
|
+
export { getNestedKeys, getNestedValue, naturalLanguageRegexFor, setNestedValue, storedKeySeparator };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "i18next-cli",
|
|
3
|
-
"version": "1.80.
|
|
3
|
+
"version": "1.80.3",
|
|
4
4
|
"description": "A unified, high-performance i18next CLI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -80,7 +80,7 @@
|
|
|
80
80
|
"execa": "^10.2.1",
|
|
81
81
|
"glob": "^13.0.6",
|
|
82
82
|
"i18next": "^26.5.10",
|
|
83
|
-
"i18next-resources-for-ts": "^2.1.
|
|
83
|
+
"i18next-resources-for-ts": "^2.1.2",
|
|
84
84
|
"inquirer": "^14.2.4",
|
|
85
85
|
"jiti": "^2.7.0",
|
|
86
86
|
"jsonc-parser": "^3.3.1",
|
|
@@ -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;
|
|
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"}
|
package/types/syncer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syncer.d.ts","sourceRoot":"","sources":["../src/syncer.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAU9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,SAAS,CAC7B,MAAM,EAAE,oBAAoB,EAC5B,OAAO,GAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,OAAO,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAO,
|
|
1
|
+
{"version":3,"file":"syncer.d.ts","sourceRoot":"","sources":["../src/syncer.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAU9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,SAAS,CAC7B,MAAM,EAAE,oBAAoB,EAC5B,OAAO,GAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,OAAO,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAO,iBAyOzF"}
|
|
@@ -68,4 +68,15 @@ export declare function getNestedValue(obj: Record<string, any>, path: string, k
|
|
|
68
68
|
* ```
|
|
69
69
|
*/
|
|
70
70
|
export declare function getNestedKeys(obj: object, keySeparator: string | false, prefix?: string): string[];
|
|
71
|
+
/**
|
|
72
|
+
* Builds the regex that marks a key as natural language (like i18next does): any of
|
|
73
|
+
* ` ,?!;` that is not itself used as the namespace or key separator.
|
|
74
|
+
*/
|
|
75
|
+
export declare function naturalLanguageRegexFor(nsSeparator: string, keySeparator: string | false): RegExp | null;
|
|
76
|
+
/**
|
|
77
|
+
* The separator a key is stored with in a translation file, or `false` when it is
|
|
78
|
+
* stored as one flat key: a serialized Trans component (`<...`), a natural-language
|
|
79
|
+
* key such as "Hello world. Bye", or a key with empty segments such as "Loading...".
|
|
80
|
+
*/
|
|
81
|
+
export declare function storedKeySeparator(key: string, keySeparator: string | false, naturalLanguageRegex: RegExp | null): string | false;
|
|
71
82
|
//# sourceMappingURL=nested-object.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"nested-object.d.ts","sourceRoot":"","sources":["../../src/utils/nested-object.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACxB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,GAAG,EACV,YAAY,EAAE,MAAM,GAAG,KAAK,GAC3B,IAAI,CAoCN;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACxB,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,GAAG,KAAK,GAC3B,GAAG,
|
|
1
|
+
{"version":3,"file":"nested-object.d.ts","sourceRoot":"","sources":["../../src/utils/nested-object.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACxB,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,GAAG,EACV,YAAY,EAAE,MAAM,GAAG,KAAK,GAC3B,IAAI,CAoCN;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,cAAc,CAC5B,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EACxB,IAAI,EAAE,MAAM,EACZ,YAAY,EAAE,MAAM,GAAG,KAAK,GAC3B,GAAG,CAWL;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,aAAa,CAAE,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,KAAK,EAAE,MAAM,SAAK,GAAG,MAAM,EAAE,CAa/F;AAID;;;GAGG;AACH,wBAAgB,uBAAuB,CAAE,WAAW,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,IAAI,CAOzG;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAE,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,KAAK,EAAE,oBAAoB,EAAE,MAAM,GAAG,IAAI,GAAG,MAAM,GAAG,KAAK,CASlI"}
|