i18next-cli 1.75.2 → 1.76.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 +43 -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 +12 -9
- package/dist/cjs/extractor/parsers/comment-parser.js +6 -1
- package/dist/cjs/extractor/parsers/expression-resolver.js +26 -5
- 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 +7 -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 +12 -9
- package/dist/esm/extractor/parsers/comment-parser.js +6 -1
- package/dist/esm/extractor/parsers/expression-resolver.js +26 -5
- 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 +7 -1
- package/dist/esm/utils/message-format.js +278 -0
- package/package.json +3 -1
- 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/expression-resolver.d.ts +5 -1
- package/types/extractor/parsers/expression-resolver.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 +16 -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
|
@@ -14,6 +14,7 @@ var logger = require('../../utils/logger.js');
|
|
|
14
14
|
var jsxAttributes = require('../../utils/jsx-attributes.js');
|
|
15
15
|
var astUtils = require('../../extractor/parsers/ast-utils.js');
|
|
16
16
|
var fileUtils = require('../../utils/file-utils.js');
|
|
17
|
+
var messageFormat = require('../../utils/message-format.js');
|
|
17
18
|
|
|
18
19
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
19
20
|
|
|
@@ -152,6 +153,9 @@ async function runInstrumenter(config, options, logger$1 = new logger.ConsoleLog
|
|
|
152
153
|
candidate.key = keyRegistry.add(keyGenerator.generateKeyFromContent(candidate.content), candidate.content);
|
|
153
154
|
}
|
|
154
155
|
}
|
|
156
|
+
// After key generation, so ICU/MF2 syntax does not end up in generated keys
|
|
157
|
+
if (messageFormat.usesMessageFormat(config))
|
|
158
|
+
approvedCandidates.forEach(candidate => toMessageFormatCandidate(candidate, config));
|
|
155
159
|
// Transform the file
|
|
156
160
|
const transformResult = transformer.transformFile(content, file, approvedCandidates, {
|
|
157
161
|
isDryRun: options.isDryRun,
|
|
@@ -1646,9 +1650,17 @@ function buildInitFileContent(opts) {
|
|
|
1646
1650
|
const defaultNS = config.extract.defaultNS !== false ? (config.extract.defaultNS || 'translation') : null;
|
|
1647
1651
|
const ns = defaultNS || 'translation';
|
|
1648
1652
|
// ── Dependencies for the install hint ──
|
|
1653
|
+
// ICU / MF2 values only render with their i18nFormat plugin (intl-messageformat is i18next-icu's peer)
|
|
1654
|
+
const formatPlugin = config.extract.messageFormat === 'icu'
|
|
1655
|
+
? { name: 'ICU', pkg: 'i18next-icu', deps: ['i18next-icu', 'intl-messageformat'] }
|
|
1656
|
+
: config.extract.messageFormat === 'mf2'
|
|
1657
|
+
? { name: 'MF2', pkg: 'i18next-mf2', deps: ['i18next-mf2'] }
|
|
1658
|
+
: undefined;
|
|
1649
1659
|
const deps = ['i18next'];
|
|
1650
1660
|
if (hasReact)
|
|
1651
1661
|
deps.push('react-i18next');
|
|
1662
|
+
if (formatPlugin)
|
|
1663
|
+
deps.push(...formatPlugin.deps);
|
|
1652
1664
|
if (strategy === 'resources-to-backend' && outputTemplate)
|
|
1653
1665
|
deps.push('i18next-resources-to-backend');
|
|
1654
1666
|
if (strategy === 'fs-backend' && outputTemplate)
|
|
@@ -1660,6 +1672,8 @@ function buildInitFileContent(opts) {
|
|
|
1660
1672
|
lines.push("import i18next from 'i18next'");
|
|
1661
1673
|
if (hasReact)
|
|
1662
1674
|
lines.push("import { initReactI18next } from 'react-i18next'");
|
|
1675
|
+
if (formatPlugin)
|
|
1676
|
+
lines.push(`import ${formatPlugin.name} from '${formatPlugin.pkg}'`);
|
|
1663
1677
|
if (outputTemplate) {
|
|
1664
1678
|
switch (strategy) {
|
|
1665
1679
|
case 'resources-to-backend':
|
|
@@ -1686,6 +1700,8 @@ function buildInitFileContent(opts) {
|
|
|
1686
1700
|
}
|
|
1687
1701
|
// ── .use() chain entries ──
|
|
1688
1702
|
const useEntries = [];
|
|
1703
|
+
if (formatPlugin)
|
|
1704
|
+
useEntries.push(` .use(${formatPlugin.name})`);
|
|
1689
1705
|
if (hasReact)
|
|
1690
1706
|
useEntries.push(' .use(initReactI18next)');
|
|
1691
1707
|
if (outputTemplate) {
|
|
@@ -1883,6 +1899,28 @@ async function injectI18nImportIntoEntryFile(initFilePath, logger) {
|
|
|
1883
1899
|
logger.warn(`Failed to inject i18n import into entry file: ${err}`);
|
|
1884
1900
|
}
|
|
1885
1901
|
}
|
|
1902
|
+
/**
|
|
1903
|
+
* Rewrites a candidate for `extract.messageFormat` `'icu'` or `'mf2'`: `{{name}}` placeholders become
|
|
1904
|
+
* `{name}` / `{$name}`, and plural forms become one message with a plural on `count`, so the code gets
|
|
1905
|
+
* `t('key', '{count, plural, one {…} other {…}}', { count: expr })` and the JSON one key.
|
|
1906
|
+
*/
|
|
1907
|
+
function toMessageFormatCandidate(candidate, config) {
|
|
1908
|
+
const convert = (text) => messageFormat.toMessageFormatPlaceholders(text, config);
|
|
1909
|
+
if (candidate.pluralForms) {
|
|
1910
|
+
const { countExpression, zero, one, other } = candidate.pluralForms;
|
|
1911
|
+
candidate.content = config.extract.messageFormat === 'mf2'
|
|
1912
|
+
? `.input {$count :number} .match $count ${zero !== undefined ? `0 {{${convert(zero)}}} ` : ''}${one !== undefined ? `one {{${convert(one)}}} ` : ''}* {{${convert(other)}}}`
|
|
1913
|
+
: `{count, plural, ${zero !== undefined ? `=0 {${convert(zero)}} ` : ''}${one !== undefined ? `one {${convert(one)}} ` : ''}other {${convert(other)}}}`;
|
|
1914
|
+
candidate.interpolations = [{ name: 'count', expression: countExpression }, ...(candidate.interpolations ?? [])];
|
|
1915
|
+
candidate.pluralForms = undefined;
|
|
1916
|
+
return;
|
|
1917
|
+
}
|
|
1918
|
+
// the content of a <Trans> candidate is JSX source; only its JSON value changes
|
|
1919
|
+
if (candidate.type !== 'jsx-mixed')
|
|
1920
|
+
candidate.content = convert(candidate.content);
|
|
1921
|
+
if (candidate.transValue)
|
|
1922
|
+
candidate.transValue = convert(candidate.transValue);
|
|
1923
|
+
}
|
|
1886
1924
|
/**
|
|
1887
1925
|
* Extracts and writes translation keys discovered during instrumentation.
|
|
1888
1926
|
*/
|
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;
|
|
@@ -186,6 +186,12 @@ ${i18nextResourcesForTs.mergeResourcesAsInterface(resources, { optimize: !!enabl
|
|
|
186
186
|
catch (e) {
|
|
187
187
|
outputPathExists = false;
|
|
188
188
|
}
|
|
189
|
+
// ICU and MF2 plurals like `one {{count} row}` contain double braces, which i18next's type-level
|
|
190
|
+
// {{variable}} extractor would read as interpolation (i18next >= 26.2.0 can turn it off)
|
|
191
|
+
const isIcuOrMf2 = config.extract.messageFormat === 'icu' || config.extract.messageFormat === 'mf2';
|
|
192
|
+
if (outputPathExists && isIcuOrMf2 && !(await promises.readFile(outputPath, 'utf-8')).includes('parseInterpolation')) {
|
|
193
|
+
internalLogger.warn(node_util.styleText('yellow', `Warning: extract.messageFormat is '${config.extract.messageFormat}', but ${config.types.output} does not set parseInterpolation. Add \`parseInterpolation: false;\` to CustomTypeOptions, see https://www.i18next.com/how-to/using-icu-or-mf2#typescript`));
|
|
194
|
+
}
|
|
189
195
|
if (!outputPathExists) {
|
|
190
196
|
// The main output file will now import from the resources file
|
|
191
197
|
const importPath = node_path.relative(node_path.dirname(outputPath), resourcesOutputPath)
|
|
@@ -236,7 +242,7 @@ import type Resources from './${importPath}';
|
|
|
236
242
|
declare module 'i18next' {
|
|
237
243
|
interface CustomTypeOptions {
|
|
238
244
|
enableSelector: ${typeof enableSelector === 'string' ? `"${enableSelector}"` : enableSelector};
|
|
239
|
-
defaultNS: ${defaultNS};${config.extract.fallbackNS ? `\n fallbackNS: ${fallbackNS};` : ''}
|
|
245
|
+
defaultNS: ${defaultNS};${config.extract.fallbackNS ? `\n fallbackNS: ${fallbackNS};` : ''}${isIcuOrMf2 ? '\n parseInterpolation: false;' : ''}
|
|
240
246
|
resources: Resources;
|
|
241
247
|
}
|
|
242
248
|
}`;
|
|
@@ -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.76.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
|
|
|
@@ -182,11 +183,11 @@ class CallExpressionHandler {
|
|
|
182
183
|
}
|
|
183
184
|
let defaultValue;
|
|
184
185
|
let options;
|
|
186
|
+
// Options inline or in a variable: `t('key', { count })`, `t('key', params)` (#307)
|
|
185
187
|
if (node.arguments.length > 1) {
|
|
186
188
|
const arg2 = unwrapParens(node.arguments[1].expression);
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
}
|
|
189
|
+
options = this.expressionResolver.objectLiteralOf(arg2);
|
|
190
|
+
if (options) ;
|
|
190
191
|
else if (arg2.type === 'StringLiteral') {
|
|
191
192
|
defaultValue = arg2.value;
|
|
192
193
|
}
|
|
@@ -198,10 +199,7 @@ class CallExpressionHandler {
|
|
|
198
199
|
}
|
|
199
200
|
}
|
|
200
201
|
if (node.arguments.length > 2) {
|
|
201
|
-
|
|
202
|
-
if (arg3.type === 'ObjectExpression') {
|
|
203
|
-
options = arg3;
|
|
204
|
-
}
|
|
202
|
+
options = this.expressionResolver.objectLiteralOf(node.arguments[2].expression) ?? options;
|
|
205
203
|
}
|
|
206
204
|
const defaultValueFromOptions = options ? this.getDefaultValueProp(options, 'defaultValue') : undefined;
|
|
207
205
|
const finalDefaultValue = (typeof defaultValueFromOptions === 'string' ? defaultValueFromOptions : defaultValue);
|
|
@@ -320,8 +318,10 @@ class CallExpressionHandler {
|
|
|
320
318
|
const locationEntry = location
|
|
321
319
|
? [{ file: this.getCurrentFile(), line: location.line, column: location.column }]
|
|
322
320
|
: undefined;
|
|
321
|
+
// ICU and MF2 keep plurals and selects inside one message: no context or plural keys
|
|
322
|
+
const plainMessage = usesMessageFormat(this.config);
|
|
323
323
|
if (options) {
|
|
324
|
-
const contextPropValue = getObjectPropValueExpression(options, 'context');
|
|
324
|
+
const contextPropValue = plainMessage ? undefined : getObjectPropValueExpression(options, 'context');
|
|
325
325
|
const keysWithContext = [];
|
|
326
326
|
// 1. Handle Context
|
|
327
327
|
if (contextPropValue?.type === 'StringLiteral' || contextPropValue?.type === 'NumericLiteral' || contextPropValue?.type === 'BooleanLiteral') {
|
|
@@ -404,7 +404,7 @@ class CallExpressionHandler {
|
|
|
404
404
|
}
|
|
405
405
|
return false;
|
|
406
406
|
})();
|
|
407
|
-
if (hasCount || isOrdinalByKey) {
|
|
407
|
+
if (!plainMessage && (hasCount || isOrdinalByKey)) {
|
|
408
408
|
// Check if plurals are disabled FIRST, before any plural optimization paths
|
|
409
409
|
if (this.config.extract.disablePlurals) {
|
|
410
410
|
// When plurals are disabled, treat count as a regular option (for interpolation only)
|
|
@@ -535,6 +535,9 @@ class CallExpressionHandler {
|
|
|
535
535
|
* plugin context.
|
|
536
536
|
*/
|
|
537
537
|
extractNestedKeys(text, _ns) {
|
|
538
|
+
// ICU and MF2 output $t() as text
|
|
539
|
+
if (usesMessageFormat(this.config))
|
|
540
|
+
return;
|
|
538
541
|
const references = parseNestedReferences(text, {
|
|
539
542
|
nestingPrefix: this.config.extract.nestingPrefix,
|
|
540
543
|
nestingSuffix: this.config.extract.nestingSuffix,
|