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.
Files changed (53) hide show
  1. package/README.md +43 -1
  2. package/dist/cjs/cli.js +5 -3
  3. package/dist/cjs/extractor/core/ast-visitors.js +1 -0
  4. package/dist/cjs/extractor/core/extractor.js +4 -0
  5. package/dist/cjs/extractor/core/translation-manager.js +4 -0
  6. package/dist/cjs/extractor/parsers/call-expression-handler.js +12 -9
  7. package/dist/cjs/extractor/parsers/comment-parser.js +6 -1
  8. package/dist/cjs/extractor/parsers/expression-resolver.js +26 -5
  9. package/dist/cjs/extractor/parsers/jsx-handler.js +60 -4
  10. package/dist/cjs/extractor/parsers/jsx-parser.js +161 -1
  11. package/dist/cjs/index.js +7 -1
  12. package/dist/cjs/instrumenter/core/instrumenter.js +38 -0
  13. package/dist/cjs/linter.js +35 -6
  14. package/dist/cjs/localize/localize.js +4 -2
  15. package/dist/cjs/status.js +5 -0
  16. package/dist/cjs/types-generator.js +7 -1
  17. package/dist/cjs/utils/message-format.js +284 -0
  18. package/dist/esm/cli.js +5 -3
  19. package/dist/esm/extractor/core/ast-visitors.js +1 -0
  20. package/dist/esm/extractor/core/extractor.js +4 -0
  21. package/dist/esm/extractor/core/translation-manager.js +4 -0
  22. package/dist/esm/extractor/parsers/call-expression-handler.js +12 -9
  23. package/dist/esm/extractor/parsers/comment-parser.js +6 -1
  24. package/dist/esm/extractor/parsers/expression-resolver.js +26 -5
  25. package/dist/esm/extractor/parsers/jsx-handler.js +61 -5
  26. package/dist/esm/extractor/parsers/jsx-parser.js +160 -2
  27. package/dist/esm/index.js +7 -1
  28. package/dist/esm/instrumenter/core/instrumenter.js +38 -0
  29. package/dist/esm/linter.js +35 -6
  30. package/dist/esm/localize/localize.js +4 -2
  31. package/dist/esm/status.js +5 -0
  32. package/dist/esm/types-generator.js +7 -1
  33. package/dist/esm/utils/message-format.js +278 -0
  34. package/package.json +3 -1
  35. package/types/extractor/core/ast-visitors.d.ts.map +1 -1
  36. package/types/extractor/core/extractor.d.ts.map +1 -1
  37. package/types/extractor/core/translation-manager.d.ts.map +1 -1
  38. package/types/extractor/parsers/call-expression-handler.d.ts.map +1 -1
  39. package/types/extractor/parsers/comment-parser.d.ts.map +1 -1
  40. package/types/extractor/parsers/expression-resolver.d.ts +5 -1
  41. package/types/extractor/parsers/expression-resolver.d.ts.map +1 -1
  42. package/types/extractor/parsers/jsx-handler.d.ts +13 -1
  43. package/types/extractor/parsers/jsx-handler.d.ts.map +1 -1
  44. package/types/extractor/parsers/jsx-parser.d.ts +16 -0
  45. package/types/extractor/parsers/jsx-parser.d.ts.map +1 -1
  46. package/types/instrumenter/core/instrumenter.d.ts.map +1 -1
  47. package/types/linter.d.ts.map +1 -1
  48. package/types/status.d.ts.map +1 -1
  49. package/types/types-generator.d.ts.map +1 -1
  50. package/types/types.d.ts +16 -3
  51. package/types/types.d.ts.map +1 -1
  52. package/types/utils/message-format.d.ts +55 -0
  53. package/types/utils/message-format.d.ts.map +1 -0
@@ -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.extract.disablePlurals) {
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
@@ -81,10 +81,10 @@ class ModuleTable extends Map {
81
81
  if (value !== undefined)
82
82
  return value;
83
83
  }
84
- return super.get(name);
84
+ return undefined;
85
85
  }
86
86
  has(name) {
87
- return this.scope.keysOf(name).some(key => super.has(key)) || super.has(name);
87
+ return this.scope.keysOf(name).some(key => super.has(key));
88
88
  }
89
89
  set(name, value) {
90
90
  const key = this.scope.declaringKey(name);
@@ -183,6 +183,9 @@ class ExpressionResolver {
183
183
  // Per-file identifiers holding an object type, from their annotation or from the
184
184
  // function they were initialised by: `const animal = getAnimal()` -> { animal: { type: [...] } }
185
185
  objectVariables = new Map();
186
+ // Per-file object literals by variable, so `const params = { count }; t('key', params)`
187
+ // reads its options like an inline object (#307)
188
+ objectLiterals = new Map();
186
189
  // Set while the pre-scan runs. Captures that read other declarations queue a
187
190
  // replay here, because what they read may be declared in a file scanned later.
188
191
  recording = false;
@@ -202,6 +205,8 @@ class ExpressionResolver {
202
205
  fileImports = new Map();
203
206
  // module -> namespace import's local name -> the import's resolved path
204
207
  fileNamespaces = new Map();
208
+ // module -> the variables it declares, which shadow the same names declared in other files
209
+ fileVariables = new Map();
205
210
  // tsconfig `paths`, `@api` -> `/abs/src/api`
206
211
  pathAliases = {};
207
212
  constructor(hooks) {
@@ -230,16 +235,18 @@ class ExpressionResolver {
230
235
  }
231
236
  // ModuleTable: the keys `name` read in the current file may be stored under, most specific first
232
237
  keysOf(name) {
233
- if (name.includes('#'))
238
+ if (name.includes('#') || !this.currentModule)
234
239
  return [name];
235
- if (!this.currentModule)
236
- return [];
237
240
  const keys = [];
238
241
  const imported = this.fileImports.get(this.currentModule)?.get(name);
239
242
  const module = imported && this.moduleOf(imported.from);
240
243
  if (module)
241
244
  keys.push(`${module}#${imported.name}`);
242
245
  keys.push(`${this.currentModule}#${name}`);
246
+ // A variable of this file never means another file's same-named one, even when its own
247
+ // value isn't captured (`const params = { count }` vs another file's `params` array) (#307)
248
+ if (!this.fileVariables.get(this.currentModule)?.has(name))
249
+ keys.push(name);
243
250
  return keys;
244
251
  }
245
252
  // ModuleTable: the key a declaration made in the current file is also stored under
@@ -285,6 +292,7 @@ class ExpressionResolver {
285
292
  this.temporaryVariables.clear();
286
293
  this.arrayElementMembers.clear();
287
294
  this.objectVariables.clear();
295
+ this.objectLiterals.clear();
288
296
  }
289
297
  /**
290
298
  * Call once every file has been pre-scanned. Captures ran in file order, so one
@@ -400,6 +408,11 @@ class ExpressionResolver {
400
408
  if (node.id.type !== 'Identifier')
401
409
  return;
402
410
  const name = node.id.value;
411
+ if (this.currentModule) {
412
+ if (!this.fileVariables.has(this.currentModule))
413
+ this.fileVariables.set(this.currentModule, new Set());
414
+ this.fileVariables.get(this.currentModule).add(name);
415
+ }
403
416
  // `const items: IProps[] = …` → remember the element's member map so
404
417
  // `items.map(({ size }) => …)` can bind the callback parameter.
405
418
  const idTypeAnnotation = this.extractTypeAnnotation(node.id);
@@ -434,6 +447,7 @@ class ExpressionResolver {
434
447
  const unwrappedInit = unwrapExpr(init);
435
448
  // ObjectExpression -> map of string props
436
449
  if (unwrappedInit.type === 'ObjectExpression' && Array.isArray(unwrappedInit.properties)) {
450
+ this.objectLiterals.set(name, unwrappedInit);
437
451
  const map = {};
438
452
  for (const p of unwrappedInit.properties) {
439
453
  if (!p || p.type !== 'KeyValueProperty')
@@ -1103,6 +1117,13 @@ class ExpressionResolver {
1103
1117
  return v;
1104
1118
  return this.sharedVariableTable.get(name);
1105
1119
  }
1120
+ /** The object literal `expr` is, inline or through a variable of this file, or undefined. */
1121
+ objectLiteralOf(expr) {
1122
+ expr = unwrapExpr(expr);
1123
+ if (expr?.type === 'ObjectExpression')
1124
+ return expr;
1125
+ return expr?.type === 'Identifier' ? this.objectLiterals.get(expr.value) : undefined;
1126
+ }
1106
1127
  /**
1107
1128
  * Return the as-const object map stored for a variable name.
1108
1129
  * Returns undefined if the name is not a known object map.
@@ -1,17 +1,26 @@
1
1
  import { safePluralRules } from '../../utils/plural-rules.js';
2
- import { extractFromTransComponent } from './jsx-parser.js';
2
+ import { extractFromIcuMacro, extractFromIcuTrans, extractFromTransComponent } from './jsx-parser.js';
3
+ import { usesMessageFormat } from '../../utils/message-format.js';
3
4
  import { lineColumnFromOffset, unwrapParens, getObjectPropValue, isSimpleTemplateLiteral } from './ast-utils.js';
4
5
  import { applySelectorNsRewrite } from './call-expression-handler.js';
5
6
 
6
7
  // Checks if a string looks like natural language (contains spaces, punctuation, etc.)
7
8
  const naturalLanguageChars = /[ ,?!;]/;
8
9
  const looksLikeNaturalLanguage = (s) => naturalLanguageChars.test(s);
10
+ // react-i18next's exports of the IcuTrans component (IcuTransWithoutContext is the one for React Server Components)
11
+ const ICU_TRANS_EXPORTS = ['IcuTrans', 'IcuTransWithoutContext'];
9
12
  class JSXHandler {
10
13
  config;
11
14
  pluginContext;
12
15
  expressionResolver;
13
16
  getCurrentFile;
14
17
  getCurrentCode;
18
+ /** local name -> component, for the components the current file imports from react-i18next/icu.macro */
19
+ icuMacroComponents = new Map();
20
+ /** the names IcuTrans is used under in the current file */
21
+ icuTransNames = new Set(ICU_TRANS_EXPORTS);
22
+ /** the <Trans> elements in the case props of a macro <Plural> or <Select>, which belong to its message */
23
+ consumedByMacro = new WeakSet();
15
24
  constructor(config, pluginContext, expressionResolver, getCurrentFile, getCurrentCode) {
16
25
  this.config = config;
17
26
  this.pluginContext = pluginContext;
@@ -19,6 +28,33 @@ class JSXHandler {
19
28
  this.getCurrentFile = getCurrentFile;
20
29
  this.getCurrentCode = getCurrentCode;
21
30
  }
31
+ /**
32
+ * Records the local names the current file imports the components of `react-i18next/icu.macro`
33
+ * (`Trans`, `Plural`, `Select`, `SelectOrdinal`) and `IcuTrans` / `IcuTransWithoutContext` under,
34
+ * so `import { IcuTrans as T }` and `import { Trans } from 'react-i18next/icu.macro'` are recognized.
35
+ */
36
+ setReactI18nextImports(body) {
37
+ this.icuMacroComponents = new Map();
38
+ this.icuTransNames = new Set(ICU_TRANS_EXPORTS);
39
+ for (const item of body) {
40
+ if (item.type !== 'ImportDeclaration')
41
+ continue;
42
+ const isMacro = /^react-i18next\/icu\.macro(\.js)?$/.test(item.source.value);
43
+ if (!isMacro && !/^react-i18next(\/|$)/.test(item.source.value))
44
+ continue;
45
+ for (const specifier of item.specifiers) {
46
+ if (specifier.type !== 'ImportSpecifier')
47
+ continue;
48
+ const imported = specifier.imported?.value ?? specifier.local.value;
49
+ if (isMacro && (imported === 'Trans' || imported === 'Plural' || imported === 'Select' || imported === 'SelectOrdinal')) {
50
+ this.icuMacroComponents.set(specifier.local.value, imported);
51
+ }
52
+ else if (!isMacro && ICU_TRANS_EXPORTS.includes(imported)) {
53
+ this.icuTransNames.add(specifier.local.value);
54
+ }
55
+ }
56
+ }
57
+ }
22
58
  /**
23
59
  * Computes line and column from a node's normalised span.
24
60
  * SWC spans are normalised to file-relative offsets after parsing,
@@ -122,12 +158,28 @@ class JSXHandler {
122
158
  * @param getScopeInfo - Function to retrieve scope information for variables
123
159
  */
124
160
  handleJSXElement(node, getScopeInfo) {
161
+ if (this.consumedByMacro.has(node))
162
+ return;
125
163
  const elementName = this.getElementName(node);
126
- if (elementName && (this.config.extract.transComponents || ['Trans']).includes(elementName)) {
127
- this.warnOnUnrepresentableTransChildren(node, elementName);
164
+ const macroComponent = elementName ? this.icuMacroComponents.get(elementName) : undefined;
165
+ if (macroComponent && macroComponent !== 'Trans') {
166
+ for (const attr of node.opening.attributes ?? []) {
167
+ if (attr.type === 'JSXAttribute' && attr.value?.type === 'JSXExpressionContainer')
168
+ this.consumedByMacro.add(attr.value.expression);
169
+ }
170
+ }
171
+ const isIcuTrans = !!elementName && this.icuTransNames.has(elementName);
172
+ if (elementName && (macroComponent || isIcuTrans || (this.config.extract.transComponents || ['Trans']).includes(elementName))) {
173
+ // the macro writes {name} on purpose, so only plain <Trans> gets the {{name}} advice
174
+ if (!macroComponent && !isIcuTrans)
175
+ this.warnOnUnrepresentableTransChildren(node, elementName);
128
176
  let extractedAttributes = null;
129
177
  try {
130
- extractedAttributes = extractFromTransComponent(node, this.config);
178
+ extractedAttributes = macroComponent
179
+ ? extractFromIcuMacro(node, macroComponent)
180
+ : isIcuTrans
181
+ ? extractFromIcuTrans(node)
182
+ : extractFromTransComponent(node, this.config);
131
183
  }
132
184
  catch (err) {
133
185
  const loc = this.getLocationFromNode(node);
@@ -155,7 +207,11 @@ class JSXHandler {
155
207
  keysToProcess.push(extractedAttributes.serializedChildren);
156
208
  }
157
209
  let extractedKeys;
158
- const { contextExpression, optionsNode, defaultValue, hasCount, isOrdinal, serializedChildren } = extractedAttributes;
210
+ const { optionsNode, defaultValue, isOrdinal, serializedChildren } = extractedAttributes;
211
+ // ICU and MF2 keep plurals and selects inside one message: no context or plural keys
212
+ const plainMessage = usesMessageFormat(this.config);
213
+ const contextExpression = plainMessage ? undefined : extractedAttributes.contextExpression;
214
+ const hasCount = !plainMessage && extractedAttributes.hasCount;
159
215
  // Extract location information using the helper method
160
216
  const location = this.getLocationFromNode(node);
161
217
  const locations = location
@@ -1,5 +1,7 @@
1
1
  import { getObjectPropValueExpression, getObjectPropValue, isSimpleTemplateLiteral } from './ast-utils.js';
2
+ import { toMessageFormatPlaceholders } from '../../utils/message-format.js';
2
3
  import { createRequire } from 'node:module';
4
+ import { parseSync } from '@swc/core';
3
5
 
4
6
  let cachedReactI18next;
5
7
  function loadReactI18next() {
@@ -271,7 +273,8 @@ function extractFromTransComponent(node, config) {
271
273
  defaultValue = processedKeyValue;
272
274
  }
273
275
  else if (!defaultsAttr && serialized.trim()) {
274
- defaultValue = serialized;
276
+ // react-i18next serializes interpolation objects as {{name}}; ICU and MF2 values need {name} / {$name}
277
+ defaultValue = toMessageFormatPlaceholders(serialized, config);
275
278
  }
276
279
  // Determine if tOptions contained explicit defaultValue* properties
277
280
  const optionsHasDefaultProps = (opts) => {
@@ -299,6 +302,161 @@ function extractFromTransComponent(node, config) {
299
302
  explicitDefault
300
303
  };
301
304
  }
305
+ function findAttribute(node, name) {
306
+ return node.opening.attributes?.find((attr) => attr.type === 'JSXAttribute' && attr.name.type === 'Identifier' && attr.name.value === name);
307
+ }
308
+ // The `i18nKey` of an IcuTrans or icu.macro component, undefined when there is none (the message is the key then)
309
+ function getKeyExpression(node) {
310
+ const attr = findAttribute(node, 'i18nKey');
311
+ if (attr?.value?.type === 'StringLiteral')
312
+ return attr.value.value.trim() ? attr.value : undefined;
313
+ if (attr?.value?.type === 'JSXExpressionContainer' && attr.value.expression.type !== 'JSXEmptyExpression')
314
+ return attr.value.expression;
315
+ return undefined;
316
+ }
317
+ /**
318
+ * Extracts react-i18next's `<IcuTrans i18nKey="invite" defaultTranslation="We invited <0>{user}</0>." />`.
319
+ * Like IcuTrans at runtime, the key is `i18nKey`, or `defaultTranslation` when there is none.
320
+ */
321
+ function extractFromIcuTrans(node) {
322
+ const defaultAttr = findAttribute(node, 'defaultTranslation');
323
+ const defaultTranslation = (defaultAttr && getStringLiteralFromAttribute(defaultAttr)) ?? '';
324
+ const keyExpression = getKeyExpression(node);
325
+ if (!keyExpression && !defaultTranslation.trim())
326
+ return null;
327
+ const nsAttr = findAttribute(node, 'ns');
328
+ return {
329
+ keyExpression,
330
+ serializedChildren: defaultTranslation,
331
+ defaultValue: defaultTranslation,
332
+ ns: nsAttr && getStringLiteralFromAttribute(nsAttr),
333
+ explicitDefault: !!defaultTranslation,
334
+ };
335
+ }
336
+ /**
337
+ * Extracts the components of `react-i18next/icu.macro`. The macro compiles them to IcuTrans with a
338
+ * `defaultTranslation` it builds from the children or props, and that is the key when there is no
339
+ * `i18nKey`, so the message is built exactly like the macro does (quirks included):
340
+ *
341
+ * - `<Trans>Welcome, <strong>{name}</strong>!</Trans>` → `Welcome, <0>{name}</0>!`
342
+ * - `<Plural count={count} one="# item" other="# items" />` → `{count, plural, one {# item} other {# items}}`
343
+ * - `<Select switch={gender} male="He" other="They" />` → `{gender, select, male {He} other {They}}`
344
+ */
345
+ function extractFromIcuMacro(node, component) {
346
+ let message;
347
+ let ns;
348
+ if (component === 'Trans') {
349
+ const defaultsAttr = findAttribute(node, 'defaults');
350
+ const defaults = defaultsAttr && getStringLiteralFromAttribute(defaultsAttr);
351
+ // the macro parses `defaults` as JSX children, unless `content` or `components` is set
352
+ // (then it builds an empty message from the children it no longer has)
353
+ const children = defaults === undefined
354
+ ? node.children
355
+ : findAttribute(node, 'content') || findAttribute(node, 'components')
356
+ ? []
357
+ : parseSync(`<>${defaults}</>`, { syntax: 'ecmascript', jsx: true }).body[0].expression.children;
358
+ message = mergeMacroChildren(children, 0).text;
359
+ const nsAttr = findAttribute(node, 'ns');
360
+ ns = nsAttr && getStringLiteralFromAttribute(nsAttr);
361
+ }
362
+ else {
363
+ // Plural, Select and SelectOrdinal do not pass `ns` on (the macro would make it a case)
364
+ message = buildMacroChoice(node, component);
365
+ }
366
+ const keyExpression = getKeyExpression(node);
367
+ if (!keyExpression && !message.trim())
368
+ return null;
369
+ return { keyExpression, serializedChildren: message, defaultValue: message, ns };
370
+ }
371
+ // Port of icu.macro's mergeChildren: text, {name}, {name, number}, tagged templates, and <N>…</N>
372
+ // for every element, numbered per nesting level. Fragments and other expressions are dropped.
373
+ function mergeMacroChildren(children, start) {
374
+ let index = start;
375
+ let text = '';
376
+ for (const child of children) {
377
+ if (child.type === 'JSXText') {
378
+ text += child.value.replace(/^\n\s+/, '').replace(/\n\s+$/, '');
379
+ }
380
+ else if (child.type === 'JSXElement') {
381
+ text += `<${index}>${mergeMacroChildren(child.children, 0).text}</${index}>`;
382
+ index++;
383
+ }
384
+ else if (child.type === 'JSXExpressionContainer') {
385
+ const expr = child.expression;
386
+ if ((expr.type === 'StringLiteral' || expr.type === 'NumericLiteral' || expr.type === 'BooleanLiteral') && expr.value) {
387
+ text += String(expr.value);
388
+ }
389
+ else if (expr.type === 'Identifier') {
390
+ text += `{${expr.value}}`;
391
+ }
392
+ else if (expr.type === 'SequenceExpression') {
393
+ text += `{${expr.expressions.map((e) => e.value ?? '').join(', ')}}`;
394
+ }
395
+ else if (expr.type === 'TaggedTemplateExpression') {
396
+ const merged = mergeMacroTemplate(expr, index);
397
+ text += merged.text;
398
+ index = merged.next;
399
+ }
400
+ }
401
+ }
402
+ return { text, next: index };
403
+ }
404
+ // Port of icu.macro's tagged templates: number`${n}` → {n, number}, plural`${count}, one {…} other {…}` → {count, plural, …}
405
+ function mergeMacroTemplate(node, start) {
406
+ const type = node.tag.type === 'Identifier' ? node.tag.value : 'unknown';
407
+ const { quasis, expressions } = node.template;
408
+ let index = start;
409
+ let text = '';
410
+ quasis.forEach((quasi, i) => {
411
+ text += (quasi.cooked ?? '').replace(/\s+/g, ' ');
412
+ const expr = expressions[i];
413
+ if (!expr)
414
+ return;
415
+ if (expr.type === 'Identifier') {
416
+ text += `${expr.value}, ${type}`;
417
+ }
418
+ else if (expr.type === 'JSXElement') {
419
+ text += `<${index}>${mergeMacroChildren(expr.children, 0).text}</${index}>`;
420
+ index++;
421
+ }
422
+ else if (expr.type === 'TaggedTemplateExpression') {
423
+ const merged = mergeMacroTemplate(expr, index);
424
+ text += merged.text;
425
+ index = merged.next;
426
+ }
427
+ });
428
+ return { text: `{${text}}`, next: index };
429
+ }
430
+ // Port of icu.macro's pluralAsJSX / selectAsJSX
431
+ function buildMacroChoice(node, component) {
432
+ const type = component === 'Select' ? 'select' : component.toLowerCase();
433
+ const switchProp = component === 'Select' ? 'switch' : 'count';
434
+ let message = '';
435
+ let index = 0;
436
+ for (const attr of node.opening.attributes ?? []) {
437
+ if (attr.type !== 'JSXAttribute' || attr.name.type !== 'Identifier')
438
+ continue;
439
+ const name = attr.name.value;
440
+ if (name === 'i18nKey' || name === 'values')
441
+ continue;
442
+ const selector = component !== 'Select' && name.startsWith('$') ? `=${name.slice(1)}` : name;
443
+ if (name === switchProp) {
444
+ const expr = attr.value?.type === 'JSXExpressionContainer' ? attr.value.expression : undefined;
445
+ const arg = expr?.type === 'Identifier' ? expr.value : component === 'Select' ? 'selectKey' : 'count';
446
+ message = `{${arg}, ${type}, ${message}`;
447
+ }
448
+ else if (attr.value?.type === 'StringLiteral') {
449
+ message = `${message} ${selector} {${attr.value.value}}`;
450
+ }
451
+ else if (attr.value?.type === 'JSXExpressionContainer') {
452
+ const expr = attr.value.expression;
453
+ const merged = mergeMacroChildren(expr.type === 'JSXElement' || expr.type === 'JSXFragment' ? expr.children : [], index);
454
+ message = `${message} ${selector} {${merged.text}}`;
455
+ index = merged.next;
456
+ }
457
+ }
458
+ return `${message}}`;
459
+ }
302
460
  /**
303
461
  * Creates a dummy React component. The implementation / return value is
304
462
  * irrelevant, as long as we have something realistic-looking to pass to
@@ -505,4 +663,4 @@ function serializeJSXChildren(children, config) {
505
663
  return rt.nodesToString(swcChildrenToReactNodes(children), i18nextOptions);
506
664
  }
507
665
 
508
- export { extractFromTransComponent };
666
+ export { extractFromIcuMacro, extractFromIcuTrans, extractFromTransComponent };
package/dist/esm/index.js CHANGED
@@ -3,7 +3,14 @@ export { extract, runExtractor } from './extractor/core/extractor.js';
3
3
  export { findKeys } from './extractor/core/key-finder.js';
4
4
  export { getTranslations } from './extractor/core/translation-manager.js';
5
5
  import 'node:path';
6
+ import 'node:fs/promises';
7
+ import 'glob';
8
+ import 'jsonc-parser';
9
+ import 'jiti';
10
+ import '@croct/json5-parser';
11
+ import 'yaml';
6
12
  import 'node:module';
13
+ import '@swc/core';
7
14
  export { recommendedAcceptedAttributes, recommendedAcceptedTags, runLinter } from './linter.js';
8
15
  export { runSyncer } from './syncer.js';
9
16
  export { runStatus, runUnusedReport } from './status.js';
@@ -13,5 +20,4 @@ export { runInstrumenter, writeExtractedKeys } from './instrumenter/core/instrum
13
20
  import './utils/jsx-attributes.js';
14
21
  import 'magic-string';
15
22
  export { runLocalize } from './localize/localize.js';
16
- import 'node:fs/promises';
17
23
  export { AGENT_PROMPT } from './localize/agent-prompt.js';
@@ -12,6 +12,7 @@ import { ConsoleLogger } from '../../utils/logger.js';
12
12
  import { ignoredAttributeSet } from '../../utils/jsx-attributes.js';
13
13
  import { normalizeSpansToCharIndices, collectIgnoredLineRanges } from '../../extractor/parsers/ast-utils.js';
14
14
  import { getOutputPath } from '../../utils/file-utils.js';
15
+ import { usesMessageFormat, toMessageFormatPlaceholders } from '../../utils/message-format.js';
15
16
 
16
17
  /**
17
18
  * Main orchestrator for the instrument command.
@@ -146,6 +147,9 @@ async function runInstrumenter(config, options, logger = new ConsoleLogger()) {
146
147
  candidate.key = keyRegistry.add(generateKeyFromContent(candidate.content), candidate.content);
147
148
  }
148
149
  }
150
+ // After key generation, so ICU/MF2 syntax does not end up in generated keys
151
+ if (usesMessageFormat(config))
152
+ approvedCandidates.forEach(candidate => toMessageFormatCandidate(candidate, config));
149
153
  // Transform the file
150
154
  const transformResult = transformFile(content, file, approvedCandidates, {
151
155
  isDryRun: options.isDryRun,
@@ -1640,9 +1644,17 @@ function buildInitFileContent(opts) {
1640
1644
  const defaultNS = config.extract.defaultNS !== false ? (config.extract.defaultNS || 'translation') : null;
1641
1645
  const ns = defaultNS || 'translation';
1642
1646
  // ── Dependencies for the install hint ──
1647
+ // ICU / MF2 values only render with their i18nFormat plugin (intl-messageformat is i18next-icu's peer)
1648
+ const formatPlugin = config.extract.messageFormat === 'icu'
1649
+ ? { name: 'ICU', pkg: 'i18next-icu', deps: ['i18next-icu', 'intl-messageformat'] }
1650
+ : config.extract.messageFormat === 'mf2'
1651
+ ? { name: 'MF2', pkg: 'i18next-mf2', deps: ['i18next-mf2'] }
1652
+ : undefined;
1643
1653
  const deps = ['i18next'];
1644
1654
  if (hasReact)
1645
1655
  deps.push('react-i18next');
1656
+ if (formatPlugin)
1657
+ deps.push(...formatPlugin.deps);
1646
1658
  if (strategy === 'resources-to-backend' && outputTemplate)
1647
1659
  deps.push('i18next-resources-to-backend');
1648
1660
  if (strategy === 'fs-backend' && outputTemplate)
@@ -1654,6 +1666,8 @@ function buildInitFileContent(opts) {
1654
1666
  lines.push("import i18next from 'i18next'");
1655
1667
  if (hasReact)
1656
1668
  lines.push("import { initReactI18next } from 'react-i18next'");
1669
+ if (formatPlugin)
1670
+ lines.push(`import ${formatPlugin.name} from '${formatPlugin.pkg}'`);
1657
1671
  if (outputTemplate) {
1658
1672
  switch (strategy) {
1659
1673
  case 'resources-to-backend':
@@ -1680,6 +1694,8 @@ function buildInitFileContent(opts) {
1680
1694
  }
1681
1695
  // ── .use() chain entries ──
1682
1696
  const useEntries = [];
1697
+ if (formatPlugin)
1698
+ useEntries.push(` .use(${formatPlugin.name})`);
1683
1699
  if (hasReact)
1684
1700
  useEntries.push(' .use(initReactI18next)');
1685
1701
  if (outputTemplate) {
@@ -1877,6 +1893,28 @@ async function injectI18nImportIntoEntryFile(initFilePath, logger) {
1877
1893
  logger.warn(`Failed to inject i18n import into entry file: ${err}`);
1878
1894
  }
1879
1895
  }
1896
+ /**
1897
+ * Rewrites a candidate for `extract.messageFormat` `'icu'` or `'mf2'`: `{{name}}` placeholders become
1898
+ * `{name}` / `{$name}`, and plural forms become one message with a plural on `count`, so the code gets
1899
+ * `t('key', '{count, plural, one {…} other {…}}', { count: expr })` and the JSON one key.
1900
+ */
1901
+ function toMessageFormatCandidate(candidate, config) {
1902
+ const convert = (text) => toMessageFormatPlaceholders(text, config);
1903
+ if (candidate.pluralForms) {
1904
+ const { countExpression, zero, one, other } = candidate.pluralForms;
1905
+ candidate.content = config.extract.messageFormat === 'mf2'
1906
+ ? `.input {$count :number} .match $count ${zero !== undefined ? `0 {{${convert(zero)}}} ` : ''}${one !== undefined ? `one {{${convert(one)}}} ` : ''}* {{${convert(other)}}}`
1907
+ : `{count, plural, ${zero !== undefined ? `=0 {${convert(zero)}} ` : ''}${one !== undefined ? `one {${convert(one)}} ` : ''}other {${convert(other)}}}`;
1908
+ candidate.interpolations = [{ name: 'count', expression: countExpression }, ...(candidate.interpolations ?? [])];
1909
+ candidate.pluralForms = undefined;
1910
+ return;
1911
+ }
1912
+ // the content of a <Trans> candidate is JSX source; only its JSON value changes
1913
+ if (candidate.type !== 'jsx-mixed')
1914
+ candidate.content = convert(candidate.content);
1915
+ if (candidate.transValue)
1916
+ candidate.transValue = convert(candidate.transValue);
1917
+ }
1880
1918
  /**
1881
1919
  * Extracts and writes translation keys discovered during instrumentation.
1882
1920
  */
@@ -9,6 +9,7 @@ import { createSpinnerLike } from './utils/wrap-ora.js';
9
9
  import { acceptedTags, translatableAttributes, ignoredTags, ignoredAttributeLowerSet } from './utils/jsx-attributes.js';
10
10
  import { normalizeSpansToCharIndices, collectIgnoredLineRanges, lineColumnFromOffset } from './extractor/parsers/ast-utils.js';
11
11
  import { matchesFunctionPattern } from './extractor/utils/function-matcher.js';
12
+ import { loadMessageAnalyzer, lintMessageFormatResources } from './utils/message-format.js';
12
13
 
13
14
  /**
14
15
  * Loads all translation values from the primary locale's JSON files and returns
@@ -162,8 +163,20 @@ function isTranslationCall(node, config) {
162
163
  }
163
164
  return false;
164
165
  }
165
- // Helper to lint interpolation parameter errors in t() calls
166
- function lintInterpolationParams(ast, code, config, translationValues) {
166
+ // The argument names of an ICU or MF2 message; none when it does not parse (`lint` reports
167
+ // syntax errors of translation files on its own).
168
+ function messageArgs(analyze, str) {
169
+ try {
170
+ return [...analyze(str).args];
171
+ }
172
+ catch {
173
+ return [];
174
+ }
175
+ }
176
+ // Helper to lint interpolation parameter errors in t() calls.
177
+ // With `analyze` (extract.messageFormat 'icu' or 'mf2') the arguments come from the message parser
178
+ // instead of the {{…}} pattern, so ICU/MF2 braces are not read as i18next interpolation.
179
+ function lintInterpolationParams(ast, code, config, translationValues, analyze) {
167
180
  const issues = [];
168
181
  // Only run if enabled (default true)
169
182
  const enabled = config.lint?.checkInterpolationParams !== false;
@@ -271,7 +284,7 @@ function lintInterpolationParams(ast, code, config, translationValues) {
271
284
  continue;
272
285
  lastSearchIndex = position + searchText.length;
273
286
  const issueLineNumber = getLineNumber(position);
274
- const keys = extractInterpolationKeys(translationStr, config);
287
+ const keys = analyze ? messageArgs(analyze, translationStr) : extractInterpolationKeys(translationStr, config);
275
288
  // Only check for unused parameters if there is at least one interpolation key in the string
276
289
  if (keys.length > 0) {
277
290
  // i18next supports nested object access via dot notation (e.g. {{author.name}} with { author }).
@@ -530,6 +543,8 @@ class Linter extends EventEmitter {
530
543
  // Load translation values once so the interpolation linter can resolve lookup keys
531
544
  // to their translated strings (fixes: key != value interpolation not detected)
532
545
  const translationValues = await loadPrimaryTranslationValues(config);
546
+ const { messageFormat } = config.extract;
547
+ const analyze = messageFormat === 'icu' || messageFormat === 'mf2' ? await loadMessageAnalyzer(messageFormat) : undefined;
533
548
  const plugins = config.plugins || [];
534
549
  const lintPluginContext = this.createLintPluginContext(config);
535
550
  await this.initializeLintPlugins(plugins, lintPluginContext);
@@ -610,7 +625,7 @@ class Linter extends EventEmitter {
610
625
  // Collect hardcoded string issues
611
626
  const hardcodedStrings = findHardcodedStrings(ast, code, config);
612
627
  // Collect interpolation parameter issues
613
- const interpolationIssues = lintInterpolationParams(ast, code, config, translationValues);
628
+ const interpolationIssues = lintInterpolationParams(ast, code, config, translationValues, analyze);
614
629
  // Collect string-concatenation issues
615
630
  const concatenationIssues = lintConcatenation(ast, code, config);
616
631
  let allIssues = [...hardcodedStrings, ...interpolationIssues, ...concatenationIssues];
@@ -627,6 +642,13 @@ class Linter extends EventEmitter {
627
642
  issuesByFile.set(file, allIssues);
628
643
  }
629
644
  }
645
+ if (analyze) {
646
+ this.emit('progress', { message: `Checking ${messageFormat === 'mf2' ? 'MF2' : 'ICU'} messages in translation files...` });
647
+ for (const [file, issues] of await lintMessageFormatResources(config, analyze)) {
648
+ totalIssues += issues.length;
649
+ issuesByFile.set(file, [...(issuesByFile.get(file) ?? []), ...issues]);
650
+ }
651
+ }
630
652
  const files = Object.fromEntries(issuesByFile.entries());
631
653
  // `success` reflects errors only — warning-severity issues (e.g. string
632
654
  // concatenation) are reported but do not fail the run / CI.
@@ -773,9 +795,16 @@ async function runLinterCli(config, options = {}) {
773
795
  else
774
796
  console.log(styleText('yellow', `\n${file}`));
775
797
  issues.forEach(({ text, line, type, severity }) => {
776
- const label = type === 'interpolation' ? 'Interpolation issue' : type === 'concatenation' ? 'String concatenation' : 'Found hardcoded string';
798
+ const label = type === 'interpolation'
799
+ ? 'Interpolation issue'
800
+ : type === 'concatenation'
801
+ ? 'String concatenation'
802
+ : type === 'messageFormat'
803
+ ? `${config.extract.messageFormat === 'mf2' ? 'MF2' : 'ICU'} message`
804
+ : 'Found hardcoded string';
777
805
  const tag = severity === 'warning' ? styleText('yellow', 'Warning:') : styleText('red', 'Error:');
778
- const detail = ` ${styleText('gray', `${line}:`)} ${tag} ${label}: "${text}"`;
806
+ // line 0: a translation file whose format has no line lookup (YAML, JS)
807
+ const detail = ` ${line ? `${styleText('gray', `${line}:`)} ` : ''}${tag} ${label}: "${text}"`;
779
808
  if (typeof internalLogger.info === 'function')
780
809
  internalLogger.info(detail);
781
810
  else
@@ -7,11 +7,13 @@ import { loadConfig, ensureConfig } from '../config.js';
7
7
  import { detectConfig } from '../heuristic-config.js';
8
8
  import { runExtractor } from '../extractor/core/extractor.js';
9
9
  import 'node:path';
10
- import 'node:module';
11
- import { getNestedKeys, getNestedValue } from '../utils/nested-object.js';
10
+ import 'jsonc-parser';
12
11
  import 'jiti';
13
12
  import '@croct/json5-parser';
14
13
  import 'yaml';
14
+ import 'node:module';
15
+ import '@swc/core';
16
+ import { getNestedKeys, getNestedValue } from '../utils/nested-object.js';
15
17
  import { runInstrumenter, findExistingI18nInitFile } from '../instrumenter/core/instrumenter.js';
16
18
  import '../utils/jsx-attributes.js';
17
19
  import 'magic-string';
@@ -8,8 +8,10 @@ import { getNestedKeys, getNestedValue } from './utils/nested-object.js';
8
8
  import { loadTranslationFile, getOutputPath } from './utils/file-utils.js';
9
9
  import { safePluralRules } from './utils/plural-rules.js';
10
10
  import { parseNestedReferences } from './utils/nesting.js';
11
+ import { usesMessageFormat } from './utils/message-format.js';
11
12
  import { isContextVariantOfAcceptingKey } from './utils/context-variants.js';
12
13
  import 'node:module';
14
+ import '@swc/core';
13
15
  import { shouldShowFunnel, recordFunnelShown } from './utils/funnel-msg-tracker.js';
14
16
  import { printUntranslatedFunnel } from './utils/locize-funnel.js';
15
17
 
@@ -246,6 +248,9 @@ async function generateStatusReport(config) {
246
248
  ? ((await loadTranslationFile(getMergedPath(primaryLanguage))) || {})
247
249
  : null;
248
250
  const collectNestedRefsFromValue = (value, refNs, bucket, seen) => {
251
+ // ICU and MF2 output $t() as text, so it references no key
252
+ if (usesMessageFormat(config))
253
+ return;
249
254
  if (typeof value === 'string') {
250
255
  if (seen.has(value))
251
256
  return;