i18next-cli 1.75.3 → 1.77.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 +44 -1
- package/dist/cjs/cli.js +5 -3
- package/dist/cjs/extractor/core/ast-visitors.js +1 -0
- package/dist/cjs/extractor/core/extractor.js +4 -0
- package/dist/cjs/extractor/core/translation-manager.js +4 -0
- package/dist/cjs/extractor/parsers/call-expression-handler.js +8 -2
- package/dist/cjs/extractor/parsers/comment-parser.js +6 -1
- package/dist/cjs/extractor/parsers/jsx-handler.js +60 -4
- package/dist/cjs/extractor/parsers/jsx-parser.js +161 -1
- package/dist/cjs/index.js +7 -1
- package/dist/cjs/instrumenter/core/instrumenter.js +38 -0
- package/dist/cjs/linter.js +35 -6
- package/dist/cjs/localize/localize.js +4 -2
- package/dist/cjs/status.js +5 -0
- package/dist/cjs/types-generator.js +32 -1
- package/dist/cjs/utils/message-format.js +284 -0
- package/dist/esm/cli.js +5 -3
- package/dist/esm/extractor/core/ast-visitors.js +1 -0
- package/dist/esm/extractor/core/extractor.js +4 -0
- package/dist/esm/extractor/core/translation-manager.js +4 -0
- package/dist/esm/extractor/parsers/call-expression-handler.js +8 -2
- package/dist/esm/extractor/parsers/comment-parser.js +6 -1
- package/dist/esm/extractor/parsers/jsx-handler.js +61 -5
- package/dist/esm/extractor/parsers/jsx-parser.js +160 -2
- package/dist/esm/index.js +7 -1
- package/dist/esm/instrumenter/core/instrumenter.js +38 -0
- package/dist/esm/linter.js +35 -6
- package/dist/esm/localize/localize.js +4 -2
- package/dist/esm/status.js +5 -0
- package/dist/esm/types-generator.js +32 -1
- package/dist/esm/utils/message-format.js +278 -0
- package/package.json +5 -3
- package/types/extractor/core/ast-visitors.d.ts.map +1 -1
- package/types/extractor/core/extractor.d.ts.map +1 -1
- package/types/extractor/core/translation-manager.d.ts.map +1 -1
- package/types/extractor/parsers/call-expression-handler.d.ts.map +1 -1
- package/types/extractor/parsers/comment-parser.d.ts.map +1 -1
- package/types/extractor/parsers/jsx-handler.d.ts +13 -1
- package/types/extractor/parsers/jsx-handler.d.ts.map +1 -1
- package/types/extractor/parsers/jsx-parser.d.ts +16 -0
- package/types/extractor/parsers/jsx-parser.d.ts.map +1 -1
- package/types/instrumenter/core/instrumenter.d.ts.map +1 -1
- package/types/linter.d.ts.map +1 -1
- package/types/status.d.ts.map +1 -1
- package/types/types-generator.d.ts.map +1 -1
- package/types/types.d.ts +17 -3
- package/types/types.d.ts.map +1 -1
- package/types/utils/message-format.d.ts +55 -0
- package/types/utils/message-format.d.ts.map +1 -0
package/dist/cjs/linter.js
CHANGED
|
@@ -11,6 +11,7 @@ var wrapOra = require('./utils/wrap-ora.js');
|
|
|
11
11
|
var jsxAttributes = require('./utils/jsx-attributes.js');
|
|
12
12
|
var astUtils = require('./extractor/parsers/ast-utils.js');
|
|
13
13
|
var functionMatcher = require('./extractor/utils/function-matcher.js');
|
|
14
|
+
var messageFormat = require('./utils/message-format.js');
|
|
14
15
|
|
|
15
16
|
/**
|
|
16
17
|
* Loads all translation values from the primary locale's JSON files and returns
|
|
@@ -164,8 +165,20 @@ function isTranslationCall(node, config) {
|
|
|
164
165
|
}
|
|
165
166
|
return false;
|
|
166
167
|
}
|
|
167
|
-
//
|
|
168
|
-
|
|
168
|
+
// The argument names of an ICU or MF2 message; none when it does not parse (`lint` reports
|
|
169
|
+
// syntax errors of translation files on its own).
|
|
170
|
+
function messageArgs(analyze, str) {
|
|
171
|
+
try {
|
|
172
|
+
return [...analyze(str).args];
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
return [];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
// Helper to lint interpolation parameter errors in t() calls.
|
|
179
|
+
// With `analyze` (extract.messageFormat 'icu' or 'mf2') the arguments come from the message parser
|
|
180
|
+
// instead of the {{…}} pattern, so ICU/MF2 braces are not read as i18next interpolation.
|
|
181
|
+
function lintInterpolationParams(ast, code, config, translationValues, analyze) {
|
|
169
182
|
const issues = [];
|
|
170
183
|
// Only run if enabled (default true)
|
|
171
184
|
const enabled = config.lint?.checkInterpolationParams !== false;
|
|
@@ -273,7 +286,7 @@ function lintInterpolationParams(ast, code, config, translationValues) {
|
|
|
273
286
|
continue;
|
|
274
287
|
lastSearchIndex = position + searchText.length;
|
|
275
288
|
const issueLineNumber = getLineNumber(position);
|
|
276
|
-
const keys = extractInterpolationKeys(translationStr, config);
|
|
289
|
+
const keys = analyze ? messageArgs(analyze, translationStr) : extractInterpolationKeys(translationStr, config);
|
|
277
290
|
// Only check for unused parameters if there is at least one interpolation key in the string
|
|
278
291
|
if (keys.length > 0) {
|
|
279
292
|
// i18next supports nested object access via dot notation (e.g. {{author.name}} with { author }).
|
|
@@ -532,6 +545,8 @@ class Linter extends node_events.EventEmitter {
|
|
|
532
545
|
// Load translation values once so the interpolation linter can resolve lookup keys
|
|
533
546
|
// to their translated strings (fixes: key != value interpolation not detected)
|
|
534
547
|
const translationValues = await loadPrimaryTranslationValues(config);
|
|
548
|
+
const { messageFormat: messageFormat$1 } = config.extract;
|
|
549
|
+
const analyze = messageFormat$1 === 'icu' || messageFormat$1 === 'mf2' ? await messageFormat.loadMessageAnalyzer(messageFormat$1) : undefined;
|
|
535
550
|
const plugins = config.plugins || [];
|
|
536
551
|
const lintPluginContext = this.createLintPluginContext(config);
|
|
537
552
|
await this.initializeLintPlugins(plugins, lintPluginContext);
|
|
@@ -612,7 +627,7 @@ class Linter extends node_events.EventEmitter {
|
|
|
612
627
|
// Collect hardcoded string issues
|
|
613
628
|
const hardcodedStrings = findHardcodedStrings(ast, code, config);
|
|
614
629
|
// Collect interpolation parameter issues
|
|
615
|
-
const interpolationIssues = lintInterpolationParams(ast, code, config, translationValues);
|
|
630
|
+
const interpolationIssues = lintInterpolationParams(ast, code, config, translationValues, analyze);
|
|
616
631
|
// Collect string-concatenation issues
|
|
617
632
|
const concatenationIssues = lintConcatenation(ast, code, config);
|
|
618
633
|
let allIssues = [...hardcodedStrings, ...interpolationIssues, ...concatenationIssues];
|
|
@@ -629,6 +644,13 @@ class Linter extends node_events.EventEmitter {
|
|
|
629
644
|
issuesByFile.set(file, allIssues);
|
|
630
645
|
}
|
|
631
646
|
}
|
|
647
|
+
if (analyze) {
|
|
648
|
+
this.emit('progress', { message: `Checking ${messageFormat$1 === 'mf2' ? 'MF2' : 'ICU'} messages in translation files...` });
|
|
649
|
+
for (const [file, issues] of await messageFormat.lintMessageFormatResources(config, analyze)) {
|
|
650
|
+
totalIssues += issues.length;
|
|
651
|
+
issuesByFile.set(file, [...(issuesByFile.get(file) ?? []), ...issues]);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
632
654
|
const files = Object.fromEntries(issuesByFile.entries());
|
|
633
655
|
// `success` reflects errors only — warning-severity issues (e.g. string
|
|
634
656
|
// concatenation) are reported but do not fail the run / CI.
|
|
@@ -775,9 +797,16 @@ async function runLinterCli(config, options = {}) {
|
|
|
775
797
|
else
|
|
776
798
|
console.log(node_util.styleText('yellow', `\n${file}`));
|
|
777
799
|
issues.forEach(({ text, line, type, severity }) => {
|
|
778
|
-
const label = type === 'interpolation'
|
|
800
|
+
const label = type === 'interpolation'
|
|
801
|
+
? 'Interpolation issue'
|
|
802
|
+
: type === 'concatenation'
|
|
803
|
+
? 'String concatenation'
|
|
804
|
+
: type === 'messageFormat'
|
|
805
|
+
? `${config.extract.messageFormat === 'mf2' ? 'MF2' : 'ICU'} message`
|
|
806
|
+
: 'Found hardcoded string';
|
|
779
807
|
const tag = severity === 'warning' ? node_util.styleText('yellow', 'Warning:') : node_util.styleText('red', 'Error:');
|
|
780
|
-
|
|
808
|
+
// line 0: a translation file whose format has no line lookup (YAML, JS)
|
|
809
|
+
const detail = ` ${line ? `${node_util.styleText('gray', `${line}:`)} ` : ''}${tag} ${label}: "${text}"`;
|
|
781
810
|
if (typeof internalLogger.info === 'function')
|
|
782
811
|
internalLogger.info(detail);
|
|
783
812
|
else
|
|
@@ -9,11 +9,13 @@ var config = require('../config.js');
|
|
|
9
9
|
var heuristicConfig = require('../heuristic-config.js');
|
|
10
10
|
var extractor = require('../extractor/core/extractor.js');
|
|
11
11
|
require('node:path');
|
|
12
|
-
require('
|
|
13
|
-
var nestedObject = require('../utils/nested-object.js');
|
|
12
|
+
require('jsonc-parser');
|
|
14
13
|
require('jiti');
|
|
15
14
|
require('@croct/json5-parser');
|
|
16
15
|
require('yaml');
|
|
16
|
+
require('node:module');
|
|
17
|
+
require('@swc/core');
|
|
18
|
+
var nestedObject = require('../utils/nested-object.js');
|
|
17
19
|
var instrumenter = require('../instrumenter/core/instrumenter.js');
|
|
18
20
|
require('../utils/jsx-attributes.js');
|
|
19
21
|
require('magic-string');
|
package/dist/cjs/status.js
CHANGED
|
@@ -10,8 +10,10 @@ var nestedObject = require('./utils/nested-object.js');
|
|
|
10
10
|
var fileUtils = require('./utils/file-utils.js');
|
|
11
11
|
var pluralRules = require('./utils/plural-rules.js');
|
|
12
12
|
var nesting = require('./utils/nesting.js');
|
|
13
|
+
var messageFormat = require('./utils/message-format.js');
|
|
13
14
|
var contextVariants = require('./utils/context-variants.js');
|
|
14
15
|
require('node:module');
|
|
16
|
+
require('@swc/core');
|
|
15
17
|
var funnelMsgTracker = require('./utils/funnel-msg-tracker.js');
|
|
16
18
|
var locizeFunnel = require('./utils/locize-funnel.js');
|
|
17
19
|
|
|
@@ -252,6 +254,9 @@ async function generateStatusReport(config) {
|
|
|
252
254
|
? ((await fileUtils.loadTranslationFile(getMergedPath(primaryLanguage))) || {})
|
|
253
255
|
: null;
|
|
254
256
|
const collectNestedRefsFromValue = (value, refNs, bucket, seen) => {
|
|
257
|
+
// ICU and MF2 output $t() as text, so it references no key
|
|
258
|
+
if (messageFormat.usesMessageFormat(config))
|
|
259
|
+
return;
|
|
255
260
|
if (typeof value === 'string') {
|
|
256
261
|
if (seen.has(value))
|
|
257
262
|
return;
|
|
@@ -18,6 +18,23 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
|
18
18
|
var yaml__default = /*#__PURE__*/_interopDefault(yaml);
|
|
19
19
|
var vm__default = /*#__PURE__*/_interopDefault(vm);
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* The `parseInterpolation` value for ICU or MF2 messages: `'icu'` / `'mf2'` (typed arguments) when the
|
|
23
|
+
* project's i18next is 26.5.0 or later, else `false` (no argument checks, needs 26.2.0). Looks for
|
|
24
|
+
* node_modules/i18next from the working directory upwards, so hoisted monorepo installs count.
|
|
25
|
+
*/
|
|
26
|
+
async function parseInterpolationFor(messageFormat) {
|
|
27
|
+
for (let dir = process.cwd();; dir = node_path.dirname(dir)) {
|
|
28
|
+
try {
|
|
29
|
+
const { version } = JSON.parse(await promises.readFile(node_path.join(dir, 'node_modules', 'i18next', 'package.json'), 'utf-8'));
|
|
30
|
+
const [major, minor] = String(version).split('.').map(Number);
|
|
31
|
+
return major > 26 || (major === 26 && minor >= 5) ? `'${messageFormat}'` : 'false';
|
|
32
|
+
}
|
|
33
|
+
catch { }
|
|
34
|
+
if (node_path.dirname(dir) === dir)
|
|
35
|
+
return 'false';
|
|
36
|
+
}
|
|
37
|
+
}
|
|
21
38
|
async function loadFile(file) {
|
|
22
39
|
const ext = node_path.extname(file);
|
|
23
40
|
if (['.ts', '.mts', '.cts', '.js', '.mjs', '.cjs'].includes(ext)) {
|
|
@@ -186,6 +203,20 @@ ${i18nextResourcesForTs.mergeResourcesAsInterface(resources, { optimize: !!enabl
|
|
|
186
203
|
catch (e) {
|
|
187
204
|
outputPathExists = false;
|
|
188
205
|
}
|
|
206
|
+
// ICU and MF2 plurals like `one {{count} row}` contain double braces, which i18next's type-level
|
|
207
|
+
// {{variable}} extractor would read as interpolation: i18next >= 26.5.0 parses ICU / MF2 instead,
|
|
208
|
+
// >= 26.2.0 can turn it off
|
|
209
|
+
const messageFormat = config.extract.messageFormat === 'icu' || config.extract.messageFormat === 'mf2' ? config.extract.messageFormat : undefined;
|
|
210
|
+
const parseInterpolation = messageFormat && await parseInterpolationFor(messageFormat);
|
|
211
|
+
if (outputPathExists && messageFormat) {
|
|
212
|
+
const existing = await promises.readFile(outputPath, 'utf-8');
|
|
213
|
+
if (!existing.includes('parseInterpolation')) {
|
|
214
|
+
internalLogger.warn(node_util.styleText('yellow', `Warning: extract.messageFormat is '${messageFormat}', but ${config.types.output} does not set parseInterpolation. Add \`parseInterpolation: ${parseInterpolation};\` to CustomTypeOptions, see https://www.i18next.com/how-to/using-icu-or-mf2#typescript`));
|
|
215
|
+
}
|
|
216
|
+
else if (parseInterpolation !== 'false' && /parseInterpolation\s*:\s*false/.test(existing)) {
|
|
217
|
+
logMessages.push(` ${node_util.styleText('yellow', 'Hint:')} your i18next version can type-check ${messageFormat === 'icu' ? 'ICU' : 'MF2'} arguments: set \`parseInterpolation: ${parseInterpolation};\` in ${config.types.output}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
189
220
|
if (!outputPathExists) {
|
|
190
221
|
// The main output file will now import from the resources file
|
|
191
222
|
const importPath = node_path.relative(node_path.dirname(outputPath), resourcesOutputPath)
|
|
@@ -236,7 +267,7 @@ import type Resources from './${importPath}';
|
|
|
236
267
|
declare module 'i18next' {
|
|
237
268
|
interface CustomTypeOptions {
|
|
238
269
|
enableSelector: ${typeof enableSelector === 'string' ? `"${enableSelector}"` : enableSelector};
|
|
239
|
-
defaultNS: ${defaultNS};${config.extract.fallbackNS ? `\n fallbackNS: ${fallbackNS};` : ''}
|
|
270
|
+
defaultNS: ${defaultNS};${config.extract.fallbackNS ? `\n fallbackNS: ${fallbackNS};` : ''}${parseInterpolation ? `\n parseInterpolation: ${parseInterpolation};` : ''}
|
|
240
271
|
resources: Resources;
|
|
241
272
|
}
|
|
242
273
|
}`;
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var promises = require('node:fs/promises');
|
|
4
|
+
var node_path = require('node:path');
|
|
5
|
+
var glob = require('glob');
|
|
6
|
+
var jsoncParser = require('jsonc-parser');
|
|
7
|
+
var fileUtils = require('./file-utils.js');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* True for `extract.messageFormat` `'icu'` and `'mf2'`. These formats put plurals and selects into the
|
|
11
|
+
* message itself and have no `$t()` nesting, so the CLI writes no plural or context suffix keys for them.
|
|
12
|
+
*/
|
|
13
|
+
const usesMessageFormat = (config) => config.extract.messageFormat === 'icu' || config.extract.messageFormat === 'mf2';
|
|
14
|
+
/**
|
|
15
|
+
* Rewrites an i18next-format text (`Hello {{name}}`, `{{total, number}}`) into the configured message format:
|
|
16
|
+
* `{name}` / `{total, number}` for ICU, `{$name}` / `{$total :number}` for MF2, and escapes the text around the
|
|
17
|
+
* placeholders. Values of the i18next format are returned as is.
|
|
18
|
+
* ponytail: `#` is not quoted inside ICU plurals, and an MF2 text starting with `.` is left to `lint` to report.
|
|
19
|
+
*/
|
|
20
|
+
function toMessageFormatPlaceholders(value, config) {
|
|
21
|
+
const format = config.extract.messageFormat;
|
|
22
|
+
if (format !== 'icu' && format !== 'mf2')
|
|
23
|
+
return value;
|
|
24
|
+
const text = (segment, beforePlaceholder) => format === 'mf2'
|
|
25
|
+
? segment.replace(/[\\{}]/g, '\\$&')
|
|
26
|
+
: segment
|
|
27
|
+
// an apostrophe before { } < > or ' starts quoted text in ICU, so a literal one is doubled
|
|
28
|
+
.replace(beforePlaceholder ? /'(?=[{}<>']|$)/g : /'(?=[{}<>'])/g, "''")
|
|
29
|
+
.replace(/[{}]/g, "'$&'");
|
|
30
|
+
let result = '';
|
|
31
|
+
let last = 0;
|
|
32
|
+
for (const match of value.matchAll(/\{\{\s*([^{},\s]+)\s*(?:,\s*([^{}]*?)\s*)?\}\}/g)) {
|
|
33
|
+
const [placeholder, name, fmt] = match;
|
|
34
|
+
result += text(value.slice(last, match.index), true) +
|
|
35
|
+
(format === 'icu' ? `{${name}${fmt ? `, ${fmt}` : ''}}` : `{$${name}${fmt ? ` :${fmt}` : ''}}`);
|
|
36
|
+
last = match.index + placeholder.length;
|
|
37
|
+
}
|
|
38
|
+
return result + text(value.slice(last), false);
|
|
39
|
+
}
|
|
40
|
+
const CLDR_ORDER = ['zero', 'one', 'two', 'few', 'many', 'other'];
|
|
41
|
+
const byCldrOrder = (a, b) => CLDR_ORDER.indexOf(a) - CLDR_ORDER.indexOf(b);
|
|
42
|
+
const MF2_NUMBER_LITERAL = /^-?(0|[1-9]\d*)(\.\d+)?([eE][-+]?\d+)?$/;
|
|
43
|
+
/**
|
|
44
|
+
* Loads the parser for `messageFormat` lazily, so projects in the i18next format never load it.
|
|
45
|
+
*/
|
|
46
|
+
async function loadMessageAnalyzer(format) {
|
|
47
|
+
if (format === 'icu') {
|
|
48
|
+
const { parse, TYPE } = await import('@formatjs/icu-messageformat-parser');
|
|
49
|
+
const walk = (elements, shape) => {
|
|
50
|
+
for (const el of elements) {
|
|
51
|
+
if (el.type === TYPE.literal || el.type === TYPE.pound)
|
|
52
|
+
continue;
|
|
53
|
+
if (el.type === TYPE.tag) {
|
|
54
|
+
walk(el.children, shape);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
shape.args.add(el.value);
|
|
58
|
+
if (el.type === TYPE.plural) {
|
|
59
|
+
const selectors = Object.keys(el.options);
|
|
60
|
+
shape.plurals.push({
|
|
61
|
+
arg: el.value,
|
|
62
|
+
type: el.pluralType === 'ordinal' ? 'ordinal' : 'cardinal',
|
|
63
|
+
categories: selectors.filter(selector => !selector.startsWith('=')),
|
|
64
|
+
exact: selectors.filter(selector => selector.startsWith('=')).map(selector => Number(selector.slice(1))),
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
if (el.type === TYPE.plural || el.type === TYPE.select) {
|
|
68
|
+
for (const option of Object.values(el.options))
|
|
69
|
+
walk(option.value, shape);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
return (message) => {
|
|
74
|
+
let elements;
|
|
75
|
+
try {
|
|
76
|
+
// ignoreTag like i18next-icu, so the <0>…</0> placeholders of <Trans> stay text
|
|
77
|
+
elements = parse(message, { ignoreTag: true });
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
const column = err?.location?.start?.column;
|
|
81
|
+
throw new Error(column ? `${err.message} at column ${column}` : String(err?.message ?? err));
|
|
82
|
+
}
|
|
83
|
+
const shape = { args: new Set(), plurals: [] };
|
|
84
|
+
walk(elements, shape);
|
|
85
|
+
return shape;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const { parseMessage, validate } = await import('messageformat');
|
|
89
|
+
return (message) => {
|
|
90
|
+
const msg = parseMessage(message); // throws a MessageSyntaxError
|
|
91
|
+
const shape = { args: validate(msg).variables, plurals: [] }; // validate throws a MessageDataModelError
|
|
92
|
+
if (msg.type !== 'select')
|
|
93
|
+
return shape;
|
|
94
|
+
msg.selectors.forEach((selector, i) => {
|
|
95
|
+
const type = mf2PluralType(msg.declarations, selector.name);
|
|
96
|
+
if (!type)
|
|
97
|
+
return;
|
|
98
|
+
const categories = new Set();
|
|
99
|
+
const exact = [];
|
|
100
|
+
for (const { keys } of msg.variants) {
|
|
101
|
+
const key = keys[i];
|
|
102
|
+
if (key.type === '*')
|
|
103
|
+
categories.add('other');
|
|
104
|
+
else if (MF2_NUMBER_LITERAL.test(key.value))
|
|
105
|
+
exact.push(Number(key.value));
|
|
106
|
+
else
|
|
107
|
+
categories.add(key.value);
|
|
108
|
+
}
|
|
109
|
+
shape.plurals.push({ arg: selector.name, type, categories: [...categories], exact });
|
|
110
|
+
});
|
|
111
|
+
return shape;
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
// The plural type a `.match` selector variable resolves to, following `.local $a = {$b}` references.
|
|
115
|
+
// ponytail: only :number and :integer select by plural category; the draft :percent and :currency are not checked.
|
|
116
|
+
function mf2PluralType(declarations, name) {
|
|
117
|
+
let decl = declarations.find(d => d.name === name);
|
|
118
|
+
for (let hops = 0; decl && hops <= declarations.length; hops++) {
|
|
119
|
+
const fn = decl.value.functionRef;
|
|
120
|
+
if (fn) {
|
|
121
|
+
if (fn.name !== 'number' && fn.name !== 'integer')
|
|
122
|
+
return undefined;
|
|
123
|
+
const select = fn.options?.select;
|
|
124
|
+
if (!select)
|
|
125
|
+
return 'cardinal';
|
|
126
|
+
if (select.type !== 'literal')
|
|
127
|
+
return undefined;
|
|
128
|
+
return select.value === 'ordinal' ? 'ordinal' : select.value === 'plural' ? 'cardinal' : undefined;
|
|
129
|
+
}
|
|
130
|
+
const arg = decl.value.arg;
|
|
131
|
+
decl = arg?.type === 'variable' && arg.name !== decl.name ? declarations.find(d => d.name === arg.name) : undefined;
|
|
132
|
+
}
|
|
133
|
+
return undefined;
|
|
134
|
+
}
|
|
135
|
+
const categoryCache = new Map();
|
|
136
|
+
/**
|
|
137
|
+
* The plural categories a message for `lng` may and must have: allowed are the locale's CLDR
|
|
138
|
+
* categories, required are `other` plus every category a whole number from 0 to 1000 selects.
|
|
139
|
+
* So ru needs one/few/many, while fr, es, it and pt may leave out `many` (from 1,000,000 only)
|
|
140
|
+
* and cs/lt `many` (fractions only). Undefined when Intl does not know the locale.
|
|
141
|
+
* Same rule as the Locize AI-review guard and MCP server.
|
|
142
|
+
*/
|
|
143
|
+
function pluralCategories(lng, type) {
|
|
144
|
+
const cacheKey = `${lng}|${type}`;
|
|
145
|
+
if (categoryCache.has(cacheKey))
|
|
146
|
+
return categoryCache.get(cacheKey);
|
|
147
|
+
let categories;
|
|
148
|
+
try {
|
|
149
|
+
if (Intl.PluralRules.supportedLocalesOf([lng]).length) {
|
|
150
|
+
const rules = new Intl.PluralRules(lng, { type });
|
|
151
|
+
const numbers = new Map([['other', []]]);
|
|
152
|
+
for (let i = 0; i <= 1000; i++) {
|
|
153
|
+
const category = rules.select(i);
|
|
154
|
+
const list = numbers.get(category);
|
|
155
|
+
if (list)
|
|
156
|
+
list.push(i);
|
|
157
|
+
else
|
|
158
|
+
numbers.set(category, [i]);
|
|
159
|
+
}
|
|
160
|
+
categories = { allowed: rules.resolvedOptions().pluralCategories, required: [...numbers.keys()], numbers };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
// an invalid language tag: only `other` is required
|
|
165
|
+
}
|
|
166
|
+
categoryCache.set(cacheKey, categories);
|
|
167
|
+
return categories;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* The required categories a plural lacks. A category other than `other` also counts as present when
|
|
171
|
+
* exact selectors cover every whole number it is selected for: en `=1` covers `one`, while ru `=1`
|
|
172
|
+
* does not (ru `one` is also 21, 31, ...). This is where the rule deliberately differs from Locize.
|
|
173
|
+
*/
|
|
174
|
+
function missingPluralCategories(plural, lng) {
|
|
175
|
+
const expected = pluralCategories(lng, plural.type);
|
|
176
|
+
const exact = new Set(plural.exact);
|
|
177
|
+
return (expected?.required ?? ['other'])
|
|
178
|
+
.filter(c => !plural.categories.includes(c))
|
|
179
|
+
.filter(c => c === 'other' || !expected.numbers.get(c).every(n => exact.has(n)))
|
|
180
|
+
.sort(byCldrOrder);
|
|
181
|
+
}
|
|
182
|
+
function* leaves(value, path = []) {
|
|
183
|
+
if (value && typeof value === 'object') {
|
|
184
|
+
for (const [k, v] of Object.entries(value))
|
|
185
|
+
yield* leaves(v, [...path, Array.isArray(value) ? Number(k) : k]);
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
yield [path, value];
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// The line of a key in a JSON file, 0 for other formats or when it is not found.
|
|
192
|
+
async function lineFinder(file) {
|
|
193
|
+
if (!/\.json5?$/i.test(file))
|
|
194
|
+
return () => 0;
|
|
195
|
+
const content = await promises.readFile(node_path.resolve(process.cwd(), file), 'utf-8');
|
|
196
|
+
const tree = jsoncParser.parseTree(content);
|
|
197
|
+
return (path) => {
|
|
198
|
+
const node = tree && jsoncParser.findNodeAtLocation(tree, path);
|
|
199
|
+
return node ? content.slice(0, node.parent?.offset ?? node.offset).split('\n').length : 0;
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Checks every translation value of every locale for `extract.messageFormat` `'icu'` or `'mf2'`:
|
|
204
|
+
* syntax, argument names against the primary language, and plural categories per locale.
|
|
205
|
+
* Returns the issues per translation file.
|
|
206
|
+
*/
|
|
207
|
+
async function lintMessageFormatResources(config, analyze) {
|
|
208
|
+
const isMf2 = config.extract.messageFormat === 'mf2';
|
|
209
|
+
const formatName = isMf2 ? 'MF2' : 'ICU';
|
|
210
|
+
const arg = (name) => isMf2 ? `{$${name}}` : `{${name}}`;
|
|
211
|
+
const { output, ignoreNamespaces = [] } = config.extract;
|
|
212
|
+
const keySeparator = config.extract.keySeparator || '.';
|
|
213
|
+
const primary = config.extract.primaryLanguage || config.locales[0] || 'en';
|
|
214
|
+
const locales = [primary, ...config.locales.filter(l => l !== primary)];
|
|
215
|
+
// Namespace discovery as in `sync`: the primary language's files
|
|
216
|
+
const namespaces = typeof output === 'string' && !output.includes('{{namespace}}')
|
|
217
|
+
? [undefined]
|
|
218
|
+
: (await glob.glob(fileUtils.getOutputPath(output, primary, '*').replace(/\\/g, '/')))
|
|
219
|
+
.map(file => node_path.basename(file).split('.')[0])
|
|
220
|
+
.filter(ns => !ignoreNamespaces.includes(ns));
|
|
221
|
+
const issuesByFile = new Map();
|
|
222
|
+
for (const ns of namespaces) {
|
|
223
|
+
let primaryShapes = new Map();
|
|
224
|
+
for (const lng of locales) {
|
|
225
|
+
const file = fileUtils.getOutputPath(output, lng, ns);
|
|
226
|
+
const resources = await fileUtils.loadTranslationFile(file);
|
|
227
|
+
if (!resources)
|
|
228
|
+
continue;
|
|
229
|
+
const shapes = new Map();
|
|
230
|
+
const problems = [];
|
|
231
|
+
for (const [path, value] of leaves(resources)) {
|
|
232
|
+
// an empty value is untranslated, not a message
|
|
233
|
+
if (typeof value !== 'string' || value === '')
|
|
234
|
+
continue;
|
|
235
|
+
const key = path.join(keySeparator);
|
|
236
|
+
const report = (text) => problems.push({ path, text: `${key}: ${text}` });
|
|
237
|
+
let shape;
|
|
238
|
+
try {
|
|
239
|
+
shape = analyze(value);
|
|
240
|
+
}
|
|
241
|
+
catch (err) {
|
|
242
|
+
const hint = value.includes('{{') ? `. i18next interpolation like {{name}} does not work with ${formatName}, use ${arg('name')}` : '';
|
|
243
|
+
report(`${formatName} syntax error: ${err?.message ?? err}${hint}`);
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
shapes.set(key, shape);
|
|
247
|
+
const source = lng === primary ? undefined : primaryShapes.get(key);
|
|
248
|
+
if (source) {
|
|
249
|
+
const missing = [...source.args].filter(a => !shape.args.has(a)).map(arg);
|
|
250
|
+
const unexpected = [...shape.args].filter(a => !source.args.has(a)).map(arg);
|
|
251
|
+
if (missing.length || unexpected.length) {
|
|
252
|
+
report(`arguments differ from "${primary}": ${[
|
|
253
|
+
missing.length ? `missing ${missing.join(', ')}` : '',
|
|
254
|
+
unexpected.length ? `unexpected ${unexpected.join(', ')}` : '',
|
|
255
|
+
].filter(Boolean).join(', ')}`);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
for (const plural of shape.plurals) {
|
|
259
|
+
const expected = pluralCategories(lng, plural.type);
|
|
260
|
+
const label = `${plural.type === 'ordinal' ? (isMf2 ? 'ordinal' : 'selectordinal') : 'plural'} ${arg(plural.arg)}`;
|
|
261
|
+
const missing = missingPluralCategories(plural, lng);
|
|
262
|
+
const unexpected = expected ? plural.categories.filter(c => !expected.allowed.includes(c)).sort(byCldrOrder) : [];
|
|
263
|
+
if (missing.length)
|
|
264
|
+
report(`${label} is missing the "${lng}" categories ${missing.join(', ')}`);
|
|
265
|
+
if (unexpected.length)
|
|
266
|
+
report(`${label} has categories "${lng}" does not use: ${unexpected.join(', ')}`);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
if (lng === primary)
|
|
270
|
+
primaryShapes = shapes;
|
|
271
|
+
if (problems.length) {
|
|
272
|
+
const lineOf = await lineFinder(file);
|
|
273
|
+
issuesByFile.set(file, problems.map(({ path, text }) => ({ text, line: lineOf(path), type: 'messageFormat' })));
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
return issuesByFile;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
exports.lintMessageFormatResources = lintMessageFormatResources;
|
|
281
|
+
exports.loadMessageAnalyzer = loadMessageAnalyzer;
|
|
282
|
+
exports.pluralCategories = pluralCategories;
|
|
283
|
+
exports.toMessageFormatPlaceholders = toMessageFormatPlaceholders;
|
|
284
|
+
exports.usesMessageFormat = usesMessageFormat;
|
package/dist/esm/cli.js
CHANGED
|
@@ -8,12 +8,14 @@ import { ensureConfig, loadConfig } from './config.js';
|
|
|
8
8
|
import { detectConfig } from './heuristic-config.js';
|
|
9
9
|
import { runExtractor } from './extractor/core/extractor.js';
|
|
10
10
|
import 'node:path';
|
|
11
|
-
import 'node:module';
|
|
12
|
-
import { getNestedKeys, getNestedValue } from './utils/nested-object.js';
|
|
13
11
|
import 'node:fs/promises';
|
|
12
|
+
import 'jsonc-parser';
|
|
14
13
|
import 'jiti';
|
|
15
14
|
import '@croct/json5-parser';
|
|
16
15
|
import 'yaml';
|
|
16
|
+
import 'node:module';
|
|
17
|
+
import '@swc/core';
|
|
18
|
+
import { getNestedKeys, getNestedValue } from './utils/nested-object.js';
|
|
17
19
|
import { runTypesGenerator } from './types-generator.js';
|
|
18
20
|
import { runSyncer } from './syncer.js';
|
|
19
21
|
import { runMigrator } from './migrator.js';
|
|
@@ -31,7 +33,7 @@ const program = new Command();
|
|
|
31
33
|
program
|
|
32
34
|
.name('i18next-cli')
|
|
33
35
|
.description('A unified, high-performance i18next CLI.')
|
|
34
|
-
.version('1.
|
|
36
|
+
.version('1.77.0') // This string is replaced with the actual version at build time by rollup
|
|
35
37
|
.addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
|
|
36
38
|
// new: global config override option
|
|
37
39
|
program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
|
|
@@ -204,6 +204,7 @@ class ASTVisitors {
|
|
|
204
204
|
// Reset per-file captured variables in the expression resolver so variables from other files don't leak.
|
|
205
205
|
this.expressionResolver.resetFileSymbols();
|
|
206
206
|
this.scopeManager.enterScope(); // Create the root scope for the file
|
|
207
|
+
this.jsxHandler.setReactI18nextImports(node.body ?? []);
|
|
207
208
|
this.walk(node);
|
|
208
209
|
this.scopeManager.exitScope(); // Clean up the root scope
|
|
209
210
|
}
|
|
@@ -158,6 +158,10 @@ function mayContainExtractionSite(code, config) {
|
|
|
158
158
|
}
|
|
159
159
|
for (const component of config.extract.transComponents || ['Trans'])
|
|
160
160
|
names.add(component.split('.').pop());
|
|
161
|
+
// always extracted, see JSXHandler
|
|
162
|
+
names.add('IcuTrans');
|
|
163
|
+
names.add('IcuTransWithoutContext');
|
|
164
|
+
names.add('icu.macro');
|
|
161
165
|
const alt = (set) => [...set].map(s => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|');
|
|
162
166
|
re = new RegExp(`(?<!\\w)(?:(?:${alt(calls)})\\s*(?:\\?\\.\\s*)?[(<]|(?:${alt(names)})(?![\\w$]))`);
|
|
163
167
|
extractionSiteRegexes.set(config, re);
|
|
@@ -6,6 +6,7 @@ import { resolveDefaultValue } from '../../utils/default-value.js';
|
|
|
6
6
|
import { ConsoleLogger } from '../../utils/logger.js';
|
|
7
7
|
import { safePluralRules } from '../../utils/plural-rules.js';
|
|
8
8
|
import { parseNestedReferences } from '../../utils/nesting.js';
|
|
9
|
+
import { usesMessageFormat } from '../../utils/message-format.js';
|
|
9
10
|
import { isContextVariantOfAcceptingKey } from '../../utils/context-variants.js';
|
|
10
11
|
|
|
11
12
|
// used for natural language check
|
|
@@ -278,6 +279,9 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
|
|
|
278
279
|
})();
|
|
279
280
|
const seenNestedValues = new Set();
|
|
280
281
|
const collectFromValue = (value) => {
|
|
282
|
+
// ICU and MF2 output $t() as text, so it references no key
|
|
283
|
+
if (usesMessageFormat(config))
|
|
284
|
+
return;
|
|
281
285
|
if (typeof value === 'string') {
|
|
282
286
|
if (seenNestedValues.has(value))
|
|
283
287
|
return;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { safePluralRules } from '../../utils/plural-rules.js';
|
|
2
2
|
import { parseNestedReferences } from '../../utils/nesting.js';
|
|
3
|
+
import { usesMessageFormat } from '../../utils/message-format.js';
|
|
3
4
|
import { getObjectPropValue, unwrapParens, lineColumnFromOffset, isSimpleTemplateLiteral, getObjectPropValueExpression } from './ast-utils.js';
|
|
4
5
|
import { matchesFunctionPattern } from '../utils/function-matcher.js';
|
|
5
6
|
|
|
@@ -317,8 +318,10 @@ class CallExpressionHandler {
|
|
|
317
318
|
const locationEntry = location
|
|
318
319
|
? [{ file: this.getCurrentFile(), line: location.line, column: location.column }]
|
|
319
320
|
: undefined;
|
|
321
|
+
// ICU and MF2 keep plurals and selects inside one message: no context or plural keys
|
|
322
|
+
const plainMessage = usesMessageFormat(this.config);
|
|
320
323
|
if (options) {
|
|
321
|
-
const contextPropValue = getObjectPropValueExpression(options, 'context');
|
|
324
|
+
const contextPropValue = plainMessage ? undefined : getObjectPropValueExpression(options, 'context');
|
|
322
325
|
const keysWithContext = [];
|
|
323
326
|
// 1. Handle Context
|
|
324
327
|
if (contextPropValue?.type === 'StringLiteral' || contextPropValue?.type === 'NumericLiteral' || contextPropValue?.type === 'BooleanLiteral') {
|
|
@@ -401,7 +404,7 @@ class CallExpressionHandler {
|
|
|
401
404
|
}
|
|
402
405
|
return false;
|
|
403
406
|
})();
|
|
404
|
-
if (hasCount || isOrdinalByKey) {
|
|
407
|
+
if (!plainMessage && (hasCount || isOrdinalByKey)) {
|
|
405
408
|
// Check if plurals are disabled FIRST, before any plural optimization paths
|
|
406
409
|
if (this.config.extract.disablePlurals) {
|
|
407
410
|
// When plurals are disabled, treat count as a regular option (for interpolation only)
|
|
@@ -532,6 +535,9 @@ class CallExpressionHandler {
|
|
|
532
535
|
* plugin context.
|
|
533
536
|
*/
|
|
534
537
|
extractNestedKeys(text, _ns) {
|
|
538
|
+
// ICU and MF2 output $t() as text
|
|
539
|
+
if (usesMessageFormat(this.config))
|
|
540
|
+
return;
|
|
535
541
|
const references = parseNestedReferences(text, {
|
|
536
542
|
nestingPrefix: this.config.extract.nestingPrefix,
|
|
537
543
|
nestingSuffix: this.config.extract.nestingSuffix,
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { safePluralRules } from '../../utils/plural-rules.js';
|
|
2
|
+
import { usesMessageFormat } from '../../utils/message-format.js';
|
|
2
3
|
|
|
3
4
|
// Checks if a string looks like natural language (contains spaces, punctuation, etc.)
|
|
4
5
|
const naturalLanguageChars = /[ ,?!;]/;
|
|
@@ -136,7 +137,11 @@ function extractKeysFromComments(code, pluginContext, config, scopeResolver, kee
|
|
|
136
137
|
continue;
|
|
137
138
|
}
|
|
138
139
|
// 5. Handle context and count combinations based on disablePlurals setting
|
|
139
|
-
if (config
|
|
140
|
+
if (usesMessageFormat(config)) {
|
|
141
|
+
// ICU and MF2 keep plurals and selects inside one message
|
|
142
|
+
pluginContext.addKey({ key, ns, defaultValue: defaultValue ?? key });
|
|
143
|
+
}
|
|
144
|
+
else if (config.extract.disablePlurals) {
|
|
140
145
|
// When plurals are disabled, ignore count for key generation. We still
|
|
141
146
|
// tag the key with `hasCount` (when a count was present) so `status` can
|
|
142
147
|
// recognise it as count-driven; file generation ignores the flag under
|