i18next-cli 1.88.0 → 1.89.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 +62 -9
- package/dist/cjs/cli.js +18 -3
- package/dist/cjs/extractor/core/translation-manager.js +9 -2
- package/dist/cjs/extractor/parsers/expression-resolver.js +9 -0
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/init.js +3 -11
- package/dist/cjs/instrumenter/core/instrumenter.js +1114 -209
- package/dist/cjs/instrumenter/core/key-generator.js +19 -9
- package/dist/cjs/instrumenter/core/string-detector.js +106 -29
- package/dist/cjs/instrumenter/core/transformer.js +89 -31
- package/dist/cjs/localize/agent-prompt.js +3 -2
- package/dist/cjs/localize/detect.js +2 -11
- package/dist/cjs/localize/localize.js +2 -1
- package/dist/cjs/utils/file-utils.js +48 -0
- package/dist/cjs/utils/jsx-attributes.js +3 -1
- package/dist/esm/cli.js +18 -3
- package/dist/esm/extractor/core/translation-manager.js +9 -2
- package/dist/esm/extractor/parsers/expression-resolver.js +9 -0
- package/dist/esm/index.js +2 -1
- package/dist/esm/init.js +3 -11
- package/dist/esm/instrumenter/core/instrumenter.js +1117 -212
- package/dist/esm/instrumenter/core/key-generator.js +19 -9
- package/dist/esm/instrumenter/core/string-detector.js +107 -31
- package/dist/esm/instrumenter/core/transformer.js +89 -31
- package/dist/esm/localize/agent-prompt.js +3 -2
- package/dist/esm/localize/detect.js +3 -12
- package/dist/esm/localize/localize.js +2 -1
- package/dist/esm/utils/file-utils.js +49 -2
- package/dist/esm/utils/jsx-attributes.js +3 -1
- package/package.json +1 -1
- package/types/cli.d.ts.map +1 -1
- package/types/extractor/core/translation-manager.d.ts.map +1 -1
- package/types/extractor/parsers/expression-resolver.d.ts.map +1 -1
- package/types/index.d.ts +1 -1
- package/types/index.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 +2 -0
- package/types/instrumenter/core/key-generator.d.ts.map +1 -1
- package/types/instrumenter/core/string-detector.d.ts +5 -1
- package/types/instrumenter/core/string-detector.d.ts.map +1 -1
- package/types/instrumenter/core/transformer.d.ts +10 -1
- package/types/instrumenter/core/transformer.d.ts.map +1 -1
- package/types/instrumenter/index.d.ts +1 -1
- package/types/instrumenter/index.d.ts.map +1 -1
- package/types/localize/agent-prompt.d.ts +3 -2
- package/types/localize/agent-prompt.d.ts.map +1 -1
- package/types/localize/detect.d.ts.map +1 -1
- package/types/types.d.ts +48 -1
- package/types/types.d.ts.map +1 -1
- package/types/utils/file-utils.d.ts +7 -0
- package/types/utils/file-utils.d.ts.map +1 -1
- package/types/utils/jsx-attributes.d.ts.map +1 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var promises = require('node:fs/promises');
|
|
4
4
|
var node_path = require('node:path');
|
|
5
|
+
var minimatch = require('minimatch');
|
|
5
6
|
var jiti = require('jiti');
|
|
6
7
|
var config = require('../config.js');
|
|
7
8
|
var json5Parser = require('@croct/json5-parser');
|
|
@@ -29,6 +30,52 @@ class ParseTranslationFileError extends Error {
|
|
|
29
30
|
this.name = 'ParseTranslationFileError';
|
|
30
31
|
}
|
|
31
32
|
}
|
|
33
|
+
async function readPackageJson(dir) {
|
|
34
|
+
try {
|
|
35
|
+
return JSON.parse(await promises.readFile(node_path.resolve(dir, 'package.json'), 'utf-8'));
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const depsOf = (pkg) => ({ ...pkg?.peerDependencies, ...pkg?.devDependencies, ...pkg?.dependencies });
|
|
42
|
+
/** A workspace root's package globs: npm/yarn `workspaces` (array or `{ packages }`), or `pnpm-workspace.yaml`. */
|
|
43
|
+
async function workspaceGlobs(dir, pkg) {
|
|
44
|
+
const workspaces = pkg?.workspaces;
|
|
45
|
+
if (Array.isArray(workspaces))
|
|
46
|
+
return workspaces;
|
|
47
|
+
if (Array.isArray(workspaces?.packages))
|
|
48
|
+
return workspaces.packages;
|
|
49
|
+
try {
|
|
50
|
+
const packages = yaml__default.default.parse(await promises.readFile(node_path.resolve(dir, 'pnpm-workspace.yaml'), 'utf-8'))?.packages;
|
|
51
|
+
return Array.isArray(packages) ? packages : [];
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The project's declared packages: `dependencies`, `devDependencies` and `peerDependencies` of the
|
|
59
|
+
* `package.json` in `cwd`, plus, for a package of a workspace (npm/yarn `workspaces` or
|
|
60
|
+
* `pnpm-workspace.yaml`), the ones of the workspace root, where shared packages such as `react` are often
|
|
61
|
+
* declared only once. The nearer package wins; a folder the root's globs do not list gets nothing from it.
|
|
62
|
+
*/
|
|
63
|
+
async function readProjectDeps(cwd = process.cwd()) {
|
|
64
|
+
const own = await readPackageJson(cwd);
|
|
65
|
+
const deps = depsOf(own);
|
|
66
|
+
if (own?.workspaces)
|
|
67
|
+
return deps;
|
|
68
|
+
for (let dir = node_path.dirname(node_path.resolve(cwd)); dir !== node_path.dirname(dir); dir = node_path.dirname(dir)) {
|
|
69
|
+
const pkg = await readPackageJson(dir);
|
|
70
|
+
const globs = await workspaceGlobs(dir, pkg);
|
|
71
|
+
if (!globs)
|
|
72
|
+
continue;
|
|
73
|
+
const member = node_path.relative(dir, node_path.resolve(cwd)).split(node_path.sep).join('/');
|
|
74
|
+
const isMember = globs.some(g => !g.startsWith('!') && minimatch.minimatch(member, g.replace(/^\.\//, '').replace(/\/$/, '')));
|
|
75
|
+
return isMember ? { ...depsOf(pkg), ...deps } : deps;
|
|
76
|
+
}
|
|
77
|
+
return deps;
|
|
78
|
+
}
|
|
32
79
|
/**
|
|
33
80
|
* Resolve an output template (string or function) into an actual path string.
|
|
34
81
|
*
|
|
@@ -240,4 +287,5 @@ exports.ignoredNamespaceMatcher = ignoredNamespaceMatcher;
|
|
|
240
287
|
exports.inferFormatFromPath = inferFormatFromPath;
|
|
241
288
|
exports.loadRawJson5Content = loadRawJson5Content;
|
|
242
289
|
exports.loadTranslationFile = loadTranslationFile;
|
|
290
|
+
exports.readProjectDeps = readProjectDeps;
|
|
243
291
|
exports.serializeTranslationFile = serializeTranslationFile;
|
|
@@ -29,7 +29,9 @@ const translatableAttributes = [
|
|
|
29
29
|
'aria-roledescription', 'aria-valuetext',
|
|
30
30
|
'content', 'description',
|
|
31
31
|
'label', 'placeholder',
|
|
32
|
-
'summary', 'caption', 'title'
|
|
32
|
+
'summary', 'caption', 'title',
|
|
33
|
+
// tooltip libraries (react-tooltip and the like)
|
|
34
|
+
'data-tip', 'data-tooltip', 'data-tooltip-content'
|
|
33
35
|
];
|
|
34
36
|
/**
|
|
35
37
|
* Pre-built Set (lower-cased) for fast membership checks in hot loops.
|
package/dist/esm/cli.js
CHANGED
|
@@ -25,7 +25,7 @@ import { runUnusedReport, runStatus } from './status.js';
|
|
|
25
25
|
import { runLocizeSync, runLocizeDownload, runLocizeMigrate } from './locize.js';
|
|
26
26
|
import { runRenameKey } from './rename-key.js';
|
|
27
27
|
import { runInstrumenter } from './instrumenter/core/instrumenter.js';
|
|
28
|
-
import './
|
|
28
|
+
import './instrumenter/core/string-detector.js';
|
|
29
29
|
import 'magic-string';
|
|
30
30
|
import { runLocalize } from './localize/localize.js';
|
|
31
31
|
|
|
@@ -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.89.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)');
|
|
@@ -321,6 +321,21 @@ program
|
|
|
321
321
|
});
|
|
322
322
|
// Display results
|
|
323
323
|
if (!options.quiet) {
|
|
324
|
+
// The preview: each text with the key it would get, by file
|
|
325
|
+
if (options.dryRun) {
|
|
326
|
+
for (const { file, candidates, languageChangeSites } of results.files) {
|
|
327
|
+
if (!candidates.length && !languageChangeSites?.length)
|
|
328
|
+
continue;
|
|
329
|
+
console.log(styleText('bold', `\n${file}`));
|
|
330
|
+
for (const c of candidates) {
|
|
331
|
+
const text = c.content.replace(/\s+/g, ' ');
|
|
332
|
+
console.log(` ${c.line}: "${text.length > 80 ? text.slice(0, 79) + '…' : text}" → ${c.key}`);
|
|
333
|
+
}
|
|
334
|
+
for (const site of languageChangeSites ?? []) {
|
|
335
|
+
console.log(` ${site.line}: changeLanguage(${site.languageExpression}) before the call that sets the language`);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
324
339
|
console.log(styleText('bold', '\nInstrumentation Summary:'));
|
|
325
340
|
console.log(` Total candidates: ${results.totalCandidates}`);
|
|
326
341
|
console.log(` Approved: ${results.totalTransformed}`);
|
|
@@ -333,7 +348,7 @@ program
|
|
|
333
348
|
console.log('Run again without --dry-run to apply changes.');
|
|
334
349
|
}
|
|
335
350
|
if (results.files.length > 0) {
|
|
336
|
-
console.log(styleText('green', `\n✅ ${results.files.length} file(s) ready for instrumentation`));
|
|
351
|
+
console.log(styleText('green', `\n✅ ${results.files.length} file(s) ${options.dryRun ? 'ready for instrumentation' : 'instrumented'}`));
|
|
337
352
|
}
|
|
338
353
|
else {
|
|
339
354
|
console.log(styleText('yellow', '\n⚠️ No files required instrumentation'));
|
|
@@ -852,8 +852,15 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
|
|
|
852
852
|
if (separator && typeof separator === 'string') {
|
|
853
853
|
const conflictingPath = findNestingConflict(newTranslations, key, separator);
|
|
854
854
|
if (conflictingPath !== null) {
|
|
855
|
-
|
|
856
|
-
|
|
855
|
+
// The keys come from the code, the same for every language: reported once, with the primary one (or
|
|
856
|
+
// the first language, when the primary one is not among them)
|
|
857
|
+
if (locale === (config.locales.includes(primaryLanguage) ? primaryLanguage : config.locales[0])) {
|
|
858
|
+
const conflict = conflictingPath === key
|
|
859
|
+
? `key "${key}" is also the parent of nested keys ("${key}${separator}…")`
|
|
860
|
+
: `key "${key}" conflicts with existing key "${conflictingPath}"`;
|
|
861
|
+
logger.error(`Error: Nesting conflict: ${conflict}. ` +
|
|
862
|
+
`"${key}" will be skipped — fix the overlapping key paths in your source code to avoid missing translations at runtime.`);
|
|
863
|
+
}
|
|
857
864
|
continue;
|
|
858
865
|
}
|
|
859
866
|
}
|
|
@@ -983,6 +983,15 @@ class ExpressionResolver {
|
|
|
983
983
|
if (args?.length && params) {
|
|
984
984
|
return this.withTypeParams(params, args, () => this.collectObjectTypeMembers(this.objectTypeMembersRaw.get(name) ?? []));
|
|
985
985
|
}
|
|
986
|
+
// `Omit<Foo, 'type'>`, `Pick<Foo, 'status'>`, `Partial<Foo>` → Foo's members, without or only the named ones
|
|
987
|
+
if (args?.length && ['Omit', 'Pick', 'Partial', 'Required', 'Readonly', 'NonNullable'].includes(name)) {
|
|
988
|
+
const members = this.resolveTypeMembers(args[0]);
|
|
989
|
+
if (!members || (name !== 'Omit' && name !== 'Pick'))
|
|
990
|
+
return members;
|
|
991
|
+
const keys = new Set(this.resolvePossibleStringValuesFromType(args[1]));
|
|
992
|
+
const kept = Object.entries(members).filter(([path]) => keys.has(path.split('.')[0]) === (name === 'Pick'));
|
|
993
|
+
return kept.length > 0 ? Object.fromEntries(kept) : undefined;
|
|
994
|
+
}
|
|
986
995
|
return this.objectTypeTable.get(name);
|
|
987
996
|
}
|
|
988
997
|
}
|
package/dist/esm/index.js
CHANGED
|
@@ -6,6 +6,7 @@ import 'node:path';
|
|
|
6
6
|
import 'node:fs/promises';
|
|
7
7
|
import 'glob';
|
|
8
8
|
import 'jsonc-parser';
|
|
9
|
+
import 'minimatch';
|
|
9
10
|
import 'jiti';
|
|
10
11
|
import '@croct/json5-parser';
|
|
11
12
|
import 'yaml';
|
|
@@ -17,7 +18,7 @@ export { runStatus, runUnusedReport } from './status.js';
|
|
|
17
18
|
export { runTypesGenerator } from './types-generator.js';
|
|
18
19
|
export { runRenameKey } from './rename-key.js';
|
|
19
20
|
export { runInstrumenter, writeExtractedKeys } from './instrumenter/core/instrumenter.js';
|
|
20
|
-
import './
|
|
21
|
+
import './instrumenter/core/string-detector.js';
|
|
21
22
|
import 'magic-string';
|
|
22
23
|
export { runLocalize } from './localize/localize.js';
|
|
23
24
|
export { AGENT_PROMPT } from './localize/agent-prompt.js';
|
package/dist/esm/init.js
CHANGED
|
@@ -5,6 +5,7 @@ import { detectConfig } from './heuristic-config.js';
|
|
|
5
5
|
import { openBrowser, promptLocizeCredentials } from './utils/locize-onboarding.js';
|
|
6
6
|
import { scaffoldInlangProject } from './utils/inlang-scaffold.js';
|
|
7
7
|
import { buildAgentNote, writeAgentNote } from './utils/agent-note.js';
|
|
8
|
+
import { readProjectDeps } from './utils/file-utils.js';
|
|
8
9
|
|
|
9
10
|
const LOCIZE_SIGNUP_URL = 'https://www.locize.app/register?from=i18next_cli__init-wizard';
|
|
10
11
|
/**
|
|
@@ -39,19 +40,10 @@ async function isEsmProject() {
|
|
|
39
40
|
* When running via `npx` without a local install, `defineConfig` would not be available
|
|
40
41
|
* at runtime, so the generated config should fall back to a plain object export.
|
|
41
42
|
*
|
|
42
|
-
* @returns Promise resolving to true if i18next-cli is
|
|
43
|
+
* @returns Promise resolving to true if i18next-cli is a declared dependency (see readProjectDeps)
|
|
43
44
|
*/
|
|
44
45
|
async function isCliLocallyInstalled() {
|
|
45
|
-
|
|
46
|
-
const packageJsonPath = resolve(process.cwd(), 'package.json');
|
|
47
|
-
const content = await readFile(packageJsonPath, 'utf-8');
|
|
48
|
-
const packageJson = JSON.parse(content);
|
|
49
|
-
const deps = { ...packageJson.dependencies, ...packageJson.devDependencies };
|
|
50
|
-
return !!deps['i18next-cli'];
|
|
51
|
-
}
|
|
52
|
-
catch {
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
46
|
+
return !!(await readProjectDeps())['i18next-cli'];
|
|
55
47
|
}
|
|
56
48
|
/**
|
|
57
49
|
* Checks whether the project uses TypeScript by looking for a tsconfig.json.
|