i18next-cli 1.89.0 → 1.91.0
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/README.md +57 -7
- package/dist/cjs/cli.js +8 -4
- package/dist/cjs/config.js +13 -12
- package/dist/cjs/extractor/core/extractor.js +1 -3
- package/dist/cjs/extractor/parsers/jsx-parser.js +4 -2
- package/dist/cjs/heuristic-config.js +51 -6
- package/dist/cjs/init.js +5 -0
- package/dist/cjs/instrumenter/core/instrumenter.js +272 -23
- package/dist/cjs/instrumenter/core/key-generator.js +1 -0
- package/dist/cjs/instrumenter/core/next-files.js +104 -0
- package/dist/cjs/instrumenter/core/transformer.js +97 -73
- package/dist/cjs/linter.js +190 -23
- package/dist/cjs/localize/agent-prompt.js +5 -2
- package/dist/cjs/localize/detect.js +1 -0
- package/dist/cjs/localize/localize.js +25 -4
- package/dist/esm/cli.js +8 -4
- package/dist/esm/config.js +13 -12
- package/dist/esm/extractor/core/extractor.js +1 -3
- package/dist/esm/extractor/parsers/jsx-parser.js +4 -3
- package/dist/esm/heuristic-config.js +52 -7
- package/dist/esm/init.js +5 -0
- package/dist/esm/instrumenter/core/instrumenter.js +273 -24
- package/dist/esm/instrumenter/core/key-generator.js +1 -0
- package/dist/esm/instrumenter/core/next-files.js +101 -0
- package/dist/esm/instrumenter/core/transformer.js +97 -73
- package/dist/esm/linter.js +192 -25
- package/dist/esm/localize/agent-prompt.js +5 -2
- package/dist/esm/localize/detect.js +1 -0
- package/dist/esm/localize/localize.js +25 -4
- package/package.json +2 -1
- package/types/cli.d.ts.map +1 -1
- package/types/config.d.ts.map +1 -1
- package/types/extractor/core/extractor.d.ts.map +1 -1
- package/types/extractor/parsers/jsx-parser.d.ts +1 -0
- package/types/extractor/parsers/jsx-parser.d.ts.map +1 -1
- package/types/heuristic-config.d.ts.map +1 -1
- package/types/init.d.ts.map +1 -1
- package/types/instrumenter/core/instrumenter.d.ts.map +1 -1
- package/types/instrumenter/core/key-generator.d.ts.map +1 -1
- package/types/instrumenter/core/next-files.d.ts +25 -0
- package/types/instrumenter/core/next-files.d.ts.map +1 -0
- package/types/instrumenter/core/transformer.d.ts +6 -0
- package/types/instrumenter/core/transformer.d.ts.map +1 -1
- package/types/linter.d.ts.map +1 -1
- package/types/localize/agent-prompt.d.ts +1 -1
- package/types/localize/agent-prompt.d.ts.map +1 -1
- package/types/localize/detect.d.ts +2 -0
- package/types/localize/detect.d.ts.map +1 -1
- package/types/localize/localize.d.ts.map +1 -1
- package/types/types.d.ts +54 -2
- package/types/types.d.ts.map +1 -1
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.
|
|
36
|
+
.version('1.91.0') // 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)');
|
|
@@ -99,7 +99,7 @@ program
|
|
|
99
99
|
return inputGlobs.some(g => minimatch(f, g, { dot: true })) &&
|
|
100
100
|
!ignoreGlobs.some(g => minimatch(f, g, { dot: true }));
|
|
101
101
|
};
|
|
102
|
-
const watcher = chokidar.watch(Array.from(new Set(inputGlobs.
|
|
102
|
+
const watcher = chokidar.watch(Array.from(new Set(inputGlobs.flatMap(globBases))), {
|
|
103
103
|
ignored: (path, stats) => /node_modules/.test(path) || (!!stats?.isFile() && !isInput(path)),
|
|
104
104
|
ignoreInitial: true,
|
|
105
105
|
persistent: true,
|
|
@@ -459,14 +459,18 @@ const deriveOutputIgnore = (output) => {
|
|
|
459
459
|
return [output.replace(/\{\{[^}]+\}\}/g, '*')];
|
|
460
460
|
};
|
|
461
461
|
// static leading folder of a glob: 'src/**/*.ts' -> 'src'
|
|
462
|
-
|
|
462
|
+
// the directories a glob starts in: `{app,components}/**` starts in two, not in the project root
|
|
463
|
+
const globBases = (pattern) => {
|
|
464
|
+
const brace = /^((?:[^*?[\]{}()!/]+\/)*)\{([^{}]+)\}(\/.*)?$/.exec(pattern);
|
|
465
|
+
if (brace)
|
|
466
|
+
return brace[2].split(',').flatMap(alt => globBases(`${brace[1]}${alt}${brace[3] ?? ''}`));
|
|
463
467
|
const parts = [];
|
|
464
468
|
for (const segment of pattern.split('/')) {
|
|
465
469
|
if (/[*?[\]{}()!]/.test(segment))
|
|
466
470
|
break;
|
|
467
471
|
parts.push(segment);
|
|
468
472
|
}
|
|
469
|
-
return parts.join('/') || '.';
|
|
473
|
+
return [parts.join('/') || '.'];
|
|
470
474
|
};
|
|
471
475
|
// helper to expand one or many glob patterns
|
|
472
476
|
const expandGlobs = async (patterns = []) => {
|
package/dist/esm/config.js
CHANGED
|
@@ -156,20 +156,21 @@ async function ensureConfig(configPath, logger = new ConsoleLogger()) {
|
|
|
156
156
|
async function findTsConfigFile() {
|
|
157
157
|
let currentDir = process.cwd();
|
|
158
158
|
while (true) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
catch {
|
|
165
|
-
// File not found, move to parent directory
|
|
166
|
-
const parentDir = dirname(currentDir);
|
|
167
|
-
if (parentDir === currentDir) {
|
|
168
|
-
// Reached the root of the file system
|
|
169
|
-
return null;
|
|
159
|
+
// a JavaScript project keeps its `paths` in jsconfig.json (`@/*` in a Next.js app)
|
|
160
|
+
for (const name of ['tsconfig.json', 'jsconfig.json']) {
|
|
161
|
+
try {
|
|
162
|
+
await access(join(currentDir, name));
|
|
163
|
+
return join(currentDir, name);
|
|
170
164
|
}
|
|
171
|
-
|
|
165
|
+
catch { }
|
|
166
|
+
}
|
|
167
|
+
// File not found, move to parent directory
|
|
168
|
+
const parentDir = dirname(currentDir);
|
|
169
|
+
if (parentDir === currentDir) {
|
|
170
|
+
// Reached the root of the file system
|
|
171
|
+
return null;
|
|
172
172
|
}
|
|
173
|
+
currentDir = parentDir;
|
|
173
174
|
}
|
|
174
175
|
}
|
|
175
176
|
/**
|
|
@@ -256,7 +256,7 @@ async function processFile(file, plugins, astVisitors, pluginContext, config, lo
|
|
|
256
256
|
});
|
|
257
257
|
}
|
|
258
258
|
catch (err) {
|
|
259
|
-
// Fallback for .ts files with JSX (already present)
|
|
259
|
+
// Fallback for .ts files with JSX (already present); JSX in a .js file is common enough to need no note
|
|
260
260
|
if (effectiveExt === '.ts' && !isTSX) {
|
|
261
261
|
try {
|
|
262
262
|
ast = await parse(code, {
|
|
@@ -266,7 +266,6 @@ async function processFile(file, plugins, astVisitors, pluginContext, config, lo
|
|
|
266
266
|
dynamicImport: true,
|
|
267
267
|
comments: true,
|
|
268
268
|
});
|
|
269
|
-
logger.info?.(`Parsed ${file} using TSX fallback`);
|
|
270
269
|
}
|
|
271
270
|
catch (err2) {
|
|
272
271
|
throw new ExtractorError('Failed to process file', file, err2);
|
|
@@ -282,7 +281,6 @@ async function processFile(file, plugins, astVisitors, pluginContext, config, lo
|
|
|
282
281
|
dynamicImport: true,
|
|
283
282
|
comments: true,
|
|
284
283
|
});
|
|
285
|
-
logger.info?.(`Parsed ${file} using JSX fallback`);
|
|
286
284
|
}
|
|
287
285
|
catch (err2) {
|
|
288
286
|
throw new ExtractorError('Failed to process file', file, err2);
|
|
@@ -501,8 +501,9 @@ function getElementName(element) {
|
|
|
501
501
|
}
|
|
502
502
|
function trimTextNode(text) {
|
|
503
503
|
text = text.replace(/\r\n/g, '\n'); // Normalize line endings
|
|
504
|
-
// If text is ONLY whitespace AND contains a newline, remove it entirely
|
|
505
|
-
|
|
504
|
+
// If text is ONLY whitespace AND contains a newline, remove it entirely. Only spaces and tabs, like
|
|
505
|
+
// Babel and SWC: an ` ` at the end of a line (U+00A0, which `\s` matches) stays a child
|
|
506
|
+
if (/^[ \t\n]+$/.test(text) && /\n/.test(text)) {
|
|
506
507
|
return null;
|
|
507
508
|
}
|
|
508
509
|
// Trim leading/trailing whitespace sequences containing newlines
|
|
@@ -668,4 +669,4 @@ function serializeJSXChildren(children, config) {
|
|
|
668
669
|
return rt.nodesToString(swcChildrenToReactNodes(children), i18nextOptions);
|
|
669
670
|
}
|
|
670
671
|
|
|
671
|
-
export { extractFromIcuMacro, extractFromIcuTrans, extractFromTransComponent };
|
|
672
|
+
export { extractFromIcuMacro, extractFromIcuTrans, extractFromTransComponent, trimTextNode };
|
|
@@ -1,7 +1,56 @@
|
|
|
1
1
|
import { glob } from 'glob';
|
|
2
|
-
import { readdir } from 'node:fs/promises';
|
|
2
|
+
import { readdir, readFile, stat } from 'node:fs/promises';
|
|
3
3
|
import { extname, dirname, join } from 'node:path';
|
|
4
|
+
import { readProjectDeps } from './utils/file-utils.js';
|
|
4
5
|
|
|
6
|
+
/** The directories source files usually live in, one glob for those that exist (`{app,components}/**`). */
|
|
7
|
+
async function sourceInput() {
|
|
8
|
+
const dirs = [];
|
|
9
|
+
// not `lib`, often the build output of a package
|
|
10
|
+
for (const dir of ['src', 'app', 'pages', 'components']) {
|
|
11
|
+
if (await stat(dir).then(found => found.isDirectory(), () => false))
|
|
12
|
+
dirs.push(dir);
|
|
13
|
+
}
|
|
14
|
+
const exts = '**/*.{js,jsx,mjs,ts,tsx}';
|
|
15
|
+
return [dirs.length > 1 ? `{${dirs.join(',')}}/${exts}` : `${dirs[0] ?? 'src'}/${exts}`];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The extract options the app's own setup decides: next-i18next's `defaultNS` (from its config, `common` when it
|
|
19
|
+
* sets none), the message format of i18next-icu or i18next-mf2, and `keySeparator: false` for translation files
|
|
20
|
+
* with flat keys that contain dots (`"blog.title": "…"`).
|
|
21
|
+
*/
|
|
22
|
+
async function setupOptions(translationFiles) {
|
|
23
|
+
const deps = await readProjectDeps();
|
|
24
|
+
const options = {};
|
|
25
|
+
if (deps['next-i18next']) {
|
|
26
|
+
const configs = await glob('{i18n.config,next-i18next.config,i18n.shared}.{ts,js,mjs,cjs}', { ignore: 'node_modules/**' });
|
|
27
|
+
for (const file of configs) {
|
|
28
|
+
const ns = /\bdefaultNS\s*:\s*['"]([^'"]+)['"]/.exec(await readFile(file, 'utf-8'))?.[1];
|
|
29
|
+
if (ns) {
|
|
30
|
+
options.defaultNS = ns;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
options.defaultNS ??= 'common';
|
|
35
|
+
}
|
|
36
|
+
if (deps['i18next-icu'])
|
|
37
|
+
options.messageFormat = 'icu';
|
|
38
|
+
else if (deps['i18next-mf2'])
|
|
39
|
+
options.messageFormat = 'mf2';
|
|
40
|
+
const flat = [];
|
|
41
|
+
for (const file of translationFiles.filter(f => extname(f) === '.json')) {
|
|
42
|
+
try {
|
|
43
|
+
const json = JSON.parse(await readFile(file, 'utf-8'));
|
|
44
|
+
if (Object.values(json).some(v => typeof v === 'object'))
|
|
45
|
+
return options;
|
|
46
|
+
flat.push(Object.keys(json).some(key => key.includes('.')));
|
|
47
|
+
}
|
|
48
|
+
catch { }
|
|
49
|
+
}
|
|
50
|
+
if (flat.includes(true))
|
|
51
|
+
options.keySeparator = false;
|
|
52
|
+
return options;
|
|
53
|
+
}
|
|
5
54
|
// A list of common glob patterns for the primary language ('en') or ('dev') translation files.
|
|
6
55
|
const HEURISTIC_PATTERNS = [
|
|
7
56
|
'public/locales/dev/*.json',
|
|
@@ -88,15 +137,11 @@ async function detectConfig() {
|
|
|
88
137
|
return {
|
|
89
138
|
locales,
|
|
90
139
|
extract: {
|
|
91
|
-
input:
|
|
92
|
-
'src/**/*.{js,jsx,mjs,ts,tsx}',
|
|
93
|
-
'app/**/*.{js,jsx,mjs,ts,tsx}',
|
|
94
|
-
'pages/**/*.{js,jsx,mjs,ts,tsx}',
|
|
95
|
-
'components/**/*.{js,jsx,mjs,ts,tsx}'
|
|
96
|
-
],
|
|
140
|
+
input: await sourceInput(),
|
|
97
141
|
output: join(basePath, '{{language}}', `{{namespace}}${extension}`),
|
|
98
142
|
outputFormat,
|
|
99
143
|
primaryLanguage: locales.includes('en') ? 'en' : locales[0],
|
|
144
|
+
...await setupOptions(files),
|
|
100
145
|
},
|
|
101
146
|
};
|
|
102
147
|
}
|
package/dist/esm/init.js
CHANGED
|
@@ -188,11 +188,16 @@ async function runInit(options = {}) {
|
|
|
188
188
|
const isTypeScript = answers.fileType.includes('TypeScript');
|
|
189
189
|
const isEsm = await isEsmProject();
|
|
190
190
|
const fileName = isTypeScript ? 'i18next.config.ts' : 'i18next.config.js';
|
|
191
|
+
// what the app's own setup decides (next-i18next's defaultNS, ICU, flat keys), as detected
|
|
192
|
+
const { defaultNS, messageFormat, keySeparator } = detectedConfig?.extract ?? {};
|
|
191
193
|
const configObject = {
|
|
192
194
|
locales: answers.locales,
|
|
193
195
|
extract: {
|
|
194
196
|
input: answers.input,
|
|
195
197
|
output: answers.output,
|
|
198
|
+
...(defaultNS !== undefined && { defaultNS }),
|
|
199
|
+
...(messageFormat && { messageFormat }),
|
|
200
|
+
...(keySeparator !== undefined && { keySeparator }),
|
|
196
201
|
},
|
|
197
202
|
};
|
|
198
203
|
if (locizeConfig)
|