i18next-cli 1.54.2 → 1.56.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 (34) hide show
  1. package/README.md +10 -3
  2. package/dist/cjs/cli.js +66 -2
  3. package/dist/cjs/extractor/core/ast-visitors.js +11 -1
  4. package/dist/cjs/extractor/core/extractor.js +1 -1
  5. package/dist/cjs/extractor/core/translation-manager.js +127 -54
  6. package/dist/cjs/extractor/parsers/call-expression-handler.js +17 -93
  7. package/dist/cjs/extractor/parsers/expression-resolver.js +96 -11
  8. package/dist/cjs/status.js +73 -12
  9. package/dist/cjs/utils/context-variants.js +59 -0
  10. package/dist/cjs/utils/nesting.js +100 -0
  11. package/dist/esm/cli.js +66 -2
  12. package/dist/esm/extractor/core/ast-visitors.js +11 -1
  13. package/dist/esm/extractor/core/extractor.js +1 -1
  14. package/dist/esm/extractor/core/translation-manager.js +126 -53
  15. package/dist/esm/extractor/parsers/call-expression-handler.js +17 -93
  16. package/dist/esm/extractor/parsers/expression-resolver.js +96 -11
  17. package/dist/esm/status.js +74 -13
  18. package/dist/esm/utils/context-variants.js +57 -0
  19. package/dist/esm/utils/nesting.js +98 -0
  20. package/package.json +1 -1
  21. package/types/cli.d.ts.map +1 -1
  22. package/types/extractor/core/ast-visitors.d.ts.map +1 -1
  23. package/types/extractor/core/extractor.d.ts +1 -0
  24. package/types/extractor/core/extractor.d.ts.map +1 -1
  25. package/types/extractor/core/translation-manager.d.ts.map +1 -1
  26. package/types/extractor/parsers/call-expression-handler.d.ts +3 -2
  27. package/types/extractor/parsers/call-expression-handler.d.ts.map +1 -1
  28. package/types/extractor/parsers/expression-resolver.d.ts +11 -0
  29. package/types/extractor/parsers/expression-resolver.d.ts.map +1 -1
  30. package/types/status.d.ts.map +1 -1
  31. package/types/utils/context-variants.d.ts +22 -0
  32. package/types/utils/context-variants.d.ts.map +1 -0
  33. package/types/utils/nesting.d.ts +36 -0
  34. package/types/utils/nesting.d.ts.map +1 -0
@@ -5,6 +5,8 @@ import { getOutputPath, loadTranslationFile } from '../../utils/file-utils.js';
5
5
  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
+ import { parseNestedReferences } from '../../utils/nesting.js';
9
+ import { isContextVariantOfAcceptingKey } from '../../utils/context-variants.js';
8
10
 
9
11
  // used for natural language check
10
12
  const chars = [' ', ',', '?', '!', ';'];
@@ -19,58 +21,6 @@ function globToRegex(glob) {
19
21
  const regexString = `^${escaped.replace(/\*/g, '.*')}$`;
20
22
  return new RegExp(regexString);
21
23
  }
22
- /**
23
- * Checks if an existing key is a context variant of a base key that accepts context.
24
- * This function handles complex cases where:
25
- * - The key might have plural suffixes (_one, _other, etc.)
26
- * - The context value itself might contain the separator (e.g., mc_laren)
27
- *
28
- * @param existingKey - The key from the translation file to check
29
- * @param keysAcceptingContext - Set of base keys that were used with context in source code
30
- * @param pluralSeparator - The separator used for plural forms (default: '_')
31
- * @param contextSeparator - The separator used for context variants (default: '_')
32
- * @returns true if the existing key is a context variant of a key accepting context
33
- */
34
- function isContextVariantOfAcceptingKey(existingKey, keysAcceptingContext, pluralSeparator, contextSeparator) {
35
- if (keysAcceptingContext.size === 0) {
36
- return false;
37
- }
38
- // Try to extract the base key from this existing key by removing context and/or plural suffixes
39
- let potentialBaseKey = existingKey;
40
- // First, try removing plural suffixes if present
41
- for (const form of pluralForms) {
42
- if (potentialBaseKey.endsWith(`${pluralSeparator}${form}`)) {
43
- potentialBaseKey = potentialBaseKey.slice(0, -(pluralSeparator.length + form.length));
44
- break;
45
- }
46
- if (potentialBaseKey.endsWith(`${pluralSeparator}ordinal${pluralSeparator}${form}`)) {
47
- potentialBaseKey = potentialBaseKey.slice(0, -(pluralSeparator.length + 'ordinal'.length + pluralSeparator.length + form.length));
48
- break;
49
- }
50
- }
51
- // Then, try removing the context suffix to get the base key
52
- // We need to check all possible base keys since the context value itself might contain separators
53
- // For example: 'formula_one_mc_laren' could be:
54
- // - base: 'formula_one_mc', context: 'laren'
55
- // - base: 'formula_one', context: 'mc_laren' ← correct
56
- // - base: 'formula', context: 'one_mc_laren'
57
- const parts = potentialBaseKey.split(contextSeparator);
58
- if (parts.length > 1) {
59
- // Try removing 1, 2, 3... parts from the end to find a matching base key
60
- for (let i = 1; i < parts.length; i++) {
61
- const baseWithoutContext = parts.slice(0, -i).join(contextSeparator);
62
- if (keysAcceptingContext.has(baseWithoutContext)) {
63
- return true;
64
- }
65
- }
66
- }
67
- // Also check if the key itself (after removing plural suffix) accepts context
68
- // This handles cases like 'friend_other' where 'friend' accepts context
69
- if (keysAcceptingContext.has(potentialBaseKey)) {
70
- return true;
71
- }
72
- return false;
73
- }
74
24
  /**
75
25
  * Checks if a key looks like an object path or natural language.
76
26
  * (like in i18next)
@@ -283,6 +233,100 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
283
233
  return [...union];
284
234
  })()
285
235
  : null;
236
+ // Discover keys that are only referenced through `$t(...)` nested references
237
+ // inside existing translation values (see issue #241). These keys are
238
+ // invisible to the AST-based extractor, so without this step they would be
239
+ // deleted when `removeUnusedKeys` is true and never expanded into the plural
240
+ // forms a secondary locale needs.
241
+ //
242
+ // We inject synthetic ExtractedKey entries for each discovered reference so
243
+ // the normal filter / plural-expansion pipeline picks them up — for the
244
+ // primary language this preserves the existing variants, and for secondary
245
+ // languages this generates the correct per-locale plural skeleton.
246
+ const syntheticNestedKeys = [];
247
+ const namespaceMatches = (refNs) => {
248
+ if (namespace === undefined)
249
+ return true;
250
+ // Nested references arrive from parseNestedReferences with `ns` either set
251
+ // from an explicit `ns:key` prefix or defaulted to config.extract.defaultNS.
252
+ // Normalise to the same bucket keys used in `keysByNS`.
253
+ const normalizedRef = refNs === undefined || refNs === null
254
+ ? config.extract.defaultNS ?? 'translation'
255
+ : refNs;
256
+ return normalizedRef === namespace;
257
+ };
258
+ // All cardinal plural categories we should expand to for a context+count
259
+ // nested reference, covering every configured locale so the per-locale
260
+ // filter can then keep only the relevant ones.
261
+ const nestedContextCountCategories = (() => {
262
+ const union = new Set();
263
+ for (const loc of config.locales) {
264
+ safePluralRules(loc, { type: 'cardinal' }).resolvedOptions().pluralCategories.forEach(c => union.add(c));
265
+ }
266
+ return [...union];
267
+ })();
268
+ const seenNestedValues = new Set();
269
+ const collectFromValue = (value) => {
270
+ if (typeof value === 'string') {
271
+ if (seenNestedValues.has(value))
272
+ return;
273
+ seenNestedValues.add(value);
274
+ const refs = parseNestedReferences(value, {
275
+ nestingPrefix: config.extract.nestingPrefix,
276
+ nestingSuffix: config.extract.nestingSuffix,
277
+ nestingOptionsSeparator: config.extract.nestingOptionsSeparator,
278
+ nsSeparator: config.extract.nsSeparator,
279
+ defaultNS: config.extract.defaultNS
280
+ });
281
+ for (const ref of refs) {
282
+ if (!namespaceMatches(ref.ns))
283
+ continue;
284
+ const effectiveHasCount = ref.hasCount && !config.extract.disablePlurals;
285
+ if (ref.context !== undefined) {
286
+ const ctxKey = `${ref.key}${contextSeparator}${ref.context}`;
287
+ if (effectiveHasCount) {
288
+ // `ctxKey` contains `contextSeparator` (which equals pluralSeparator
289
+ // by default) so we cannot hand it to the base plural expansion
290
+ // pass. Instead, push fully-expanded variants and rely on the
291
+ // per-locale filter to keep the relevant ones.
292
+ for (const category of nestedContextCountCategories) {
293
+ syntheticNestedKeys.push({
294
+ key: `${ctxKey}${pluralSeparator}${category}`,
295
+ hasCount: true,
296
+ isExpandedPlural: true
297
+ });
298
+ }
299
+ }
300
+ else {
301
+ syntheticNestedKeys.push({ key: ref.key });
302
+ syntheticNestedKeys.push({ key: ctxKey });
303
+ }
304
+ }
305
+ else if (effectiveHasCount) {
306
+ // Plain plural reference — push the base plural key and let the
307
+ // normal expansion in the main loop emit per-locale variants.
308
+ syntheticNestedKeys.push({ key: ref.key, hasCount: true });
309
+ }
310
+ else {
311
+ syntheticNestedKeys.push({ key: ref.key });
312
+ }
313
+ }
314
+ }
315
+ else if (value && typeof value === 'object' && !Array.isArray(value)) {
316
+ for (const v of Object.values(value)) {
317
+ collectFromValue(v);
318
+ }
319
+ }
320
+ };
321
+ // Scan both the locale being built and the primary locale so that newly
322
+ // introduced references are propagated to every locale on the first run.
323
+ collectFromValue(existingTranslations);
324
+ if (primaryExistingTranslations && primaryExistingTranslations !== existingTranslations) {
325
+ collectFromValue(primaryExistingTranslations);
326
+ }
327
+ const nsKeysWithNested = syntheticNestedKeys.length > 0
328
+ ? [...nsKeys, ...syntheticNestedKeys]
329
+ : nsKeys;
286
330
  // Prepare namespace pattern checking helpers
287
331
  const rawPreserve = config.extract.preservePatterns || [];
288
332
  // Helper to check if a key should be filtered out during extraction
@@ -338,7 +382,7 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
338
382
  return false;
339
383
  };
340
384
  // Filter nsKeys to only include keys relevant to this language
341
- const filteredKeys = nsKeys.filter(({ key, hasCount, isOrdinal, explicitDefault }) => {
385
+ const filteredKeys = nsKeysWithNested.filter(({ key, hasCount, isOrdinal, explicitDefault }) => {
342
386
  // FIRST: Check if key matches preservePatterns and should be excluded
343
387
  if (shouldFilterKey(key)) {
344
388
  return false;
@@ -419,6 +463,35 @@ function buildNewTranslationsForNs(nsKeys, existingTranslations, config, locale,
419
463
  setNestedValue(newTranslations, existingKey, value, keySeparator ?? '.');
420
464
  }
421
465
  }
466
+ // PROPAGATE CONTEXT VARIANTS FROM PRIMARY TO SECONDARY (issue #242):
467
+ // When `preserveContextVariants` is enabled and the source code uses a
468
+ // dynamic context value (e.g. `t('exportType', { context: type })`), the
469
+ // extractor tags the base key as "accepting context" but the actual context
470
+ // values (e.g. `gas`, `water`) are only known from the primary translation
471
+ // file. Propagate those variants from primary to secondary locales so every
472
+ // locale ends up with the same key skeleton — translators and downstream
473
+ // `sync` can then fill in real values.
474
+ if (preserveContextVariants && locale !== primaryLanguage && primaryExistingTranslations) {
475
+ const primaryKeys = getNestedKeys(primaryExistingTranslations, keySeparator ?? '.');
476
+ for (const primaryKey of primaryKeys) {
477
+ if (shouldFilterKey(primaryKey))
478
+ continue;
479
+ const isContextVariant = isContextVariantOfAcceptingKey(primaryKey, keysAcceptingContext, pluralSeparator, contextSeparator);
480
+ if (!isContextVariant)
481
+ continue;
482
+ const separator = primaryKey.startsWith('<') ? false : (keySeparator ?? '.');
483
+ const alreadySet = getNestedValue(newTranslations, primaryKey, separator);
484
+ if (alreadySet !== undefined)
485
+ continue;
486
+ // Prefer an existing secondary value if present, otherwise fall back to
487
+ // the configured defaultValue (empty string for secondaries by default).
488
+ const existingSecondaryValue = getNestedValue(existingTranslations, primaryKey, separator);
489
+ const valueToSet = existingSecondaryValue !== undefined
490
+ ? existingSecondaryValue
491
+ : resolveDefaultValue(emptyDefaultValue, primaryKey, namespace || config?.extract?.defaultNS || 'translation', locale);
492
+ setNestedValue(newTranslations, primaryKey, valueToSet, separator);
493
+ }
494
+ }
422
495
  // PRESERVE LOCALE-SPECIFIC PLURAL FORMS: When dealing with plural keys in non-primary locales,
423
496
  // preserve any existing plural forms that are NOT being explicitly generated.
424
497
  // This ensures that locale-specific forms (like _few, _many) added by translators are preserved.
@@ -1,4 +1,5 @@
1
1
  import { safePluralRules } from '../../utils/plural-rules.js';
2
+ import { parseNestedReferences } from '../../utils/nesting.js';
2
3
  import { lineColumnFromOffset, isSimpleTemplateLiteral, getObjectPropValue, getObjectPropValueExpression } from './ast-utils.js';
3
4
 
4
5
  // Helper to escape regex characters
@@ -452,103 +453,26 @@ class CallExpressionHandler {
452
453
  }
453
454
  }
454
455
  /**
455
- * Scans a string for nested translations like $t(key, options) and extracts them.
456
+ * Scans a string for nested translations like $t(key, options) and registers
457
+ * the referenced keys (plus their plural / context variants) on the current
458
+ * plugin context.
456
459
  */
457
- extractNestedKeys(text, ns) {
458
- if (!text || typeof text !== 'string')
459
- return;
460
- const prefix = this.config.extract.nestingPrefix ?? '$t(';
461
- const suffix = this.config.extract.nestingSuffix ?? ')';
462
- const escapedPrefix = escapeRegex(prefix);
463
- const escapedSuffix = escapeRegex(suffix);
464
- // Regex adapted from i18next Interpolator.js
465
- // Matches nested calls like $t(key) or $t(key, { options })
466
- // It handles balanced parentheses to some extent and quoted strings
467
- const nestingRegexp = new RegExp(`${escapedPrefix}((?:[^()"']+|"[^"]*"|'[^']*'|\\((?:[^()]|"[^"]*"|'[^']*')*\\))*?)${escapedSuffix}`, 'g');
468
- let match;
469
- while ((match = nestingRegexp.exec(text)) !== null) {
470
- if (match[1]) {
471
- // Do NOT trust the outer `ns` blindly — compute namespace from the nested key itself
472
- // inside processNestedContent. Pass `undefined` so processNestedContent resolves ns
473
- // deterministically (either from key "ns:key" or from defaultNS).
474
- this.processNestedContent(match[1], undefined);
475
- }
476
- }
477
- }
478
- processNestedContent(content, ns) {
479
- let key = content;
480
- let optionsString = '';
481
- const separator = this.config.extract.nestingOptionsSeparator ?? ',';
482
- // Logic adapted from i18next Interpolator.js handleHasOptions
483
- if (content.indexOf(separator) < 0) {
484
- key = content.trim();
485
- }
486
- else {
487
- // Split by separator, but be careful about objects
488
- // i18next does: const c = key.split(new RegExp(`${sep}[ ]*{`));
489
- // This assumes options start with {
490
- const sepRegex = new RegExp(`${escapeRegex(separator)}[ ]*{`);
491
- const parts = content.split(sepRegex);
492
- if (parts.length > 1) {
493
- key = parts[0].trim();
494
- // Reconstruct the options part: add back the '{' that was consumed by split
495
- optionsString = `{${parts.slice(1).join(separator + ' {')}`;
460
+ extractNestedKeys(text, _ns) {
461
+ const references = parseNestedReferences(text, {
462
+ nestingPrefix: this.config.extract.nestingPrefix,
463
+ nestingSuffix: this.config.extract.nestingSuffix,
464
+ nestingOptionsSeparator: this.config.extract.nestingOptionsSeparator,
465
+ nsSeparator: this.config.extract.nsSeparator,
466
+ defaultNS: this.config.extract.defaultNS
467
+ });
468
+ for (const { key, ns: nestedNs, hasCount, context } of references) {
469
+ const effectiveHasCount = hasCount && !this.config.extract.disablePlurals;
470
+ if (effectiveHasCount || context !== undefined) {
471
+ this.generateNestedPluralKeys(key, nestedNs, effectiveHasCount, context);
496
472
  }
497
473
  else {
498
- // Fallback for simple split if no object pattern found
499
- const sepIdx = content.indexOf(separator);
500
- key = content.substring(0, sepIdx).trim();
501
- optionsString = content.substring(sepIdx + 1).trim();
502
- }
503
- }
504
- // Remove quotes from key if present
505
- if ((key.startsWith("'") && key.endsWith("'")) || (key.startsWith('"') && key.endsWith('"'))) {
506
- key = key.slice(1, -1);
507
- }
508
- if (!key)
509
- return;
510
- // Resolve namespace for the nested key:
511
- // If nested key contains nsSeparator (e.g. "ns:key"), extract namespace,
512
- // otherwise use configured defaultNS.
513
- let nestedNs;
514
- const nsSeparator = this.config.extract.nsSeparator ?? ':';
515
- if (nsSeparator && key.includes(nsSeparator)) {
516
- const parts = key.split(nsSeparator);
517
- const candidateNs = parts[0];
518
- if (!looksLikeNaturalLanguage(candidateNs)) {
519
- nestedNs = parts.shift();
520
- key = parts.join(nsSeparator);
521
- if (!key || key.trim() === '')
522
- return;
474
+ this.pluginContext.addKey({ key, ns: nestedNs });
523
475
  }
524
- else {
525
- nestedNs = this.config.extract.defaultNS;
526
- }
527
- }
528
- else {
529
- nestedNs = this.config.extract.defaultNS;
530
- }
531
- let hasCount = false;
532
- let context;
533
- if (optionsString) {
534
- // Simple regex check for count and context in the options string
535
- // This is an approximation since we don't have a full JSON parser here that handles JS objects perfectly
536
- // but it should cover most static cases.
537
- // Check for count: ...
538
- if (/['"]?count['"]?\s*:/.test(optionsString)) {
539
- hasCount = true;
540
- }
541
- // Check for context: ...
542
- const contextMatch = /['"]?context['"]?\s*:\s*(['"])(.*?)\1/.exec(optionsString);
543
- if (contextMatch) {
544
- context = contextMatch[2];
545
- }
546
- }
547
- if ((hasCount && !this.config.extract.disablePlurals) || context !== undefined) {
548
- this.generateNestedPluralKeys(key, nestedNs, hasCount && !this.config.extract.disablePlurals, context);
549
- }
550
- else {
551
- this.pluginContext.addKey({ key, ns: nestedNs });
552
476
  }
553
477
  }
554
478
  generateNestedPluralKeys(key, ns, hasCount, context) {
@@ -16,6 +16,11 @@ class ExpressionResolver {
16
16
  // Shared (cross-file) table for type aliases — populated alongside typeAliasTable.
17
17
  // Persists across resetFileSymbols() so exported type aliases are visible to importers.
18
18
  sharedTypeAliasTable = new Map();
19
+ // Shared (cross-file) table for function return-value sets. Populated from
20
+ // both explicit return-type annotations and body-inferred return values so
21
+ // that `t(fn())` / `const x = fn(); t(\`...${x}...\`)` work across files.
22
+ // Persists across resetFileSymbols() just like the other shared tables.
23
+ sharedFunctionReturnTable = new Map();
19
24
  // Temporary per-scope variable overrides, used to inject .map() / .forEach()
20
25
  // callback parameters while the callback body is being walked.
21
26
  temporaryVariables = new Map();
@@ -166,15 +171,23 @@ class ExpressionResolver {
166
171
  return;
167
172
  }
168
173
  // pattern 3 (arrow function variant):
169
- // `const fn = (): 'a' | 'b' => ...` — capture the explicit return type annotation.
174
+ // `const fn = (): 'a' | 'b' => ...` — capture the explicit return type annotation,
175
+ // OR fall back to walking the body's return expressions / expression body
176
+ // when no annotation is present (mirrors TS's own return-type inference).
170
177
  if (unwrappedInit.type === 'ArrowFunctionExpression' || unwrappedInit.type === 'FunctionExpression') {
178
+ let returnVals = [];
171
179
  const rawReturnType = unwrappedInit.returnType ?? unwrappedInit.typeAnnotation;
172
180
  if (rawReturnType) {
181
+ // Explicit annotation — trust it even when it resolves to [].
173
182
  const tsType = rawReturnType.typeAnnotation ?? rawReturnType;
174
- const returnVals = this.resolvePossibleStringValuesFromType(tsType);
175
- if (returnVals.length > 0) {
176
- this.variableTable.set(name, returnVals);
177
- }
183
+ returnVals = this.resolvePossibleStringValuesFromType(tsType);
184
+ }
185
+ else {
186
+ returnVals = this.inferReturnValuesFromFunctionBody(unwrappedInit);
187
+ }
188
+ if (returnVals.length > 0) {
189
+ this.variableTable.set(name, returnVals);
190
+ this.sharedFunctionReturnTable.set(name, returnVals);
178
191
  }
179
192
  }
180
193
  }
@@ -229,19 +242,85 @@ class ExpressionResolver {
229
242
  // or directly in `.returnType` (FunctionExpression / ArrowFunctionExpression).
230
243
  const fn = node.function ?? node;
231
244
  const rawReturnType = fn.returnType ?? fn.typeAnnotation;
232
- if (!rawReturnType)
233
- return;
234
- // Unwrap TsTypeAnnotation wrapper if present
235
- const tsType = rawReturnType.typeAnnotation ?? rawReturnType;
236
- const vals = this.resolvePossibleStringValuesFromType(tsType);
245
+ let vals = [];
246
+ if (rawReturnType) {
247
+ // Unwrap TsTypeAnnotation wrapper if present. Explicit annotations are
248
+ // authoritative: if the author declared the return type we trust it,
249
+ // even when it resolves to [] (e.g. plain `string`). Falling back to
250
+ // body inference in that case would invent keys the author deliberately
251
+ // opted out of.
252
+ const tsType = rawReturnType.typeAnnotation ?? rawReturnType;
253
+ vals = this.resolvePossibleStringValuesFromType(tsType);
254
+ }
255
+ else {
256
+ // No annotation — infer from body. Mirrors TS's own return-type
257
+ // inference for functions like:
258
+ // function getCurrentAppType() {
259
+ // if (...) return OrganizationType.ROUTING;
260
+ // if (...) return OrganizationType.CONTRACTOR;
261
+ // }
262
+ vals = this.inferReturnValuesFromFunctionBody(fn);
263
+ }
237
264
  if (vals.length > 0) {
238
265
  this.variableTable.set(name, vals);
266
+ this.sharedFunctionReturnTable.set(name, vals);
239
267
  }
240
268
  }
241
269
  catch {
242
270
  // noop
243
271
  }
244
272
  }
273
+ /**
274
+ * Walk a function body's ReturnStatements and union the statically-resolvable
275
+ * string values of their argument expressions. Does NOT descend into nested
276
+ * function declarations (their returns belong to the inner function, not us).
277
+ *
278
+ * This is how we mirror TypeScript's implicit return-type inference for the
279
+ * purpose of extracting translation keys — we don't need exhaustiveness, just
280
+ * the set of string values any return statement could produce.
281
+ */
282
+ inferReturnValuesFromFunctionBody(fn) {
283
+ const body = fn?.body;
284
+ if (!body)
285
+ return [];
286
+ const collected = [];
287
+ const visit = (n) => {
288
+ if (!n || typeof n !== 'object')
289
+ return;
290
+ // Don't descend into nested function bodies — their returns aren't ours.
291
+ if (n !== body && (n.type === 'FunctionDeclaration' ||
292
+ n.type === 'FunctionExpression' ||
293
+ n.type === 'ArrowFunctionExpression'))
294
+ return;
295
+ if (n.type === 'ReturnStatement' && n.argument) {
296
+ const vals = this.resolvePossibleStringValuesFromExpression(n.argument);
297
+ if (vals.length > 0)
298
+ collected.push(...vals);
299
+ }
300
+ for (const key of Object.keys(n)) {
301
+ const child = n[key];
302
+ if (Array.isArray(child)) {
303
+ for (const item of child) {
304
+ if (item && typeof item === 'object')
305
+ visit(item);
306
+ }
307
+ }
308
+ else if (child && typeof child === 'object' && typeof child.type === 'string') {
309
+ visit(child);
310
+ }
311
+ }
312
+ };
313
+ // Arrow functions with an expression body (no BlockStatement) — `() => expr` —
314
+ // have their return expression directly as `body`.
315
+ if (body.type !== 'BlockStatement') {
316
+ const vals = this.resolvePossibleStringValuesFromExpression(body);
317
+ if (vals.length > 0)
318
+ return Array.from(new Set(vals));
319
+ return [];
320
+ }
321
+ visit(body);
322
+ return Array.from(new Set(collected));
323
+ }
245
324
  /**
246
325
  * Extract a raw TsType node from an identifier's type annotation.
247
326
  * SWC may wrap it in a `TsTypeAnnotation` node — this unwraps it.
@@ -501,7 +580,10 @@ class ExpressionResolver {
501
580
  catch { }
502
581
  }
503
582
  // pattern 3:
504
- // `t(fn())` — resolve to the function's known return-type union when captured.
583
+ // `t(fn())` — resolve to the function's known return-value set (either
584
+ // from an explicit annotation or inferred from the function body). Check
585
+ // the per-file variable table first (same-file capture) and fall back to
586
+ // the shared cross-file table populated during pre-scan.
505
587
  if (expression.type === 'CallExpression') {
506
588
  try {
507
589
  const callee = expression.callee;
@@ -509,6 +591,9 @@ class ExpressionResolver {
509
591
  const v = this.variableTable.get(callee.value);
510
592
  if (Array.isArray(v) && v.length > 0)
511
593
  return v;
594
+ const sv = this.sharedFunctionReturnTable.get(callee.value);
595
+ if (sv && sv.length > 0)
596
+ return sv;
512
597
  }
513
598
  }
514
599
  catch { }
@@ -5,9 +5,10 @@ import '@swc/core';
5
5
  import 'node:fs/promises';
6
6
  import { findKeys } from './extractor/core/key-finder.js';
7
7
  import 'glob';
8
- import { getNestedValue } from './utils/nested-object.js';
8
+ import { getNestedKeys, getNestedValue } from './utils/nested-object.js';
9
9
  import { loadTranslationFile, getOutputPath } from './utils/file-utils.js';
10
10
  import { safePluralRules } from './utils/plural-rules.js';
11
+ import { isContextVariantOfAcceptingKey } from './utils/context-variants.js';
11
12
  import { shouldShowFunnel, recordFunnelShown } from './utils/funnel-msg-tracker.js';
12
13
  import './extractor/parsers/jsx-parser.js';
13
14
 
@@ -80,7 +81,8 @@ async function generateStatusReport(config) {
80
81
  config.extract.primaryLanguage ||= config.locales[0] || 'en';
81
82
  config.extract.secondaryLanguages ||= config.locales.filter((l) => l !== config?.extract?.primaryLanguage);
82
83
  const { allKeys: allExtractedKeys } = await findKeys(config);
83
- const { secondaryLanguages, keySeparator = '.', defaultNS = 'translation', mergeNamespaces = false, pluralSeparator = '_', fallbackNS } = config.extract;
84
+ const { secondaryLanguages, keySeparator = '.', defaultNS = 'translation', mergeNamespaces = false, pluralSeparator = '_', contextSeparator = '_', fallbackNS } = config.extract;
85
+ const primaryLanguage = config.extract.primaryLanguage || config.locales[0] || 'en';
84
86
  const keysByNs = new Map();
85
87
  for (const key of allExtractedKeys.values()) {
86
88
  const ns = key.ns || defaultNS || 'translation';
@@ -103,6 +105,40 @@ async function generateStatusReport(config) {
103
105
  keysByNs,
104
106
  locales: new Map(),
105
107
  };
108
+ // Discover context variants that live in the primary translation file but
109
+ // are not directly extracted as keys (see issue #243). When source code uses
110
+ // a dynamic context value like `t('exportType', { context: type })`, the
111
+ // extractor can only tag the base key as "accepting context"; the actual
112
+ // context values (`_gas`, `_water`, ...) are only visible in the primary
113
+ // translation file. Without this scan, status never checks those variants
114
+ // for translation gaps in secondary locales.
115
+ const keysAcceptingContext = new Set();
116
+ for (const keys of keysByNs.values()) {
117
+ for (const k of keys) {
118
+ if (k.keyAcceptingContext)
119
+ keysAcceptingContext.add(k.keyAcceptingContext);
120
+ }
121
+ }
122
+ const contextVariantsByNs = new Map();
123
+ if (keysAcceptingContext.size > 0) {
124
+ const primaryMerged = mergeNamespaces
125
+ ? ((await loadTranslationFile(resolve(process.cwd(), getOutputPath(config.extract.output, primaryLanguage, (defaultNS === false ? 'translation' : (defaultNS || 'translation')))))) || {})
126
+ : null;
127
+ for (const ns of keysByNs.keys()) {
128
+ const primaryNsTranslations = mergeNamespaces
129
+ ? (primaryMerged?.[ns] ?? primaryMerged ?? {})
130
+ : ((await loadTranslationFile(resolve(process.cwd(), getOutputPath(config.extract.output, primaryLanguage, ns)))) || {});
131
+ const primaryKeys = getNestedKeys(primaryNsTranslations, keySeparator ?? '.');
132
+ const variants = [];
133
+ for (const primaryKey of primaryKeys) {
134
+ if (isContextVariantOfAcceptingKey(primaryKey, keysAcceptingContext, pluralSeparator, contextSeparator)) {
135
+ variants.push(primaryKey);
136
+ }
137
+ }
138
+ if (variants.length > 0)
139
+ contextVariantsByNs.set(ns, variants);
140
+ }
141
+ }
106
142
  for (const locale of secondaryLanguages) {
107
143
  let totalTranslatedForLocale = 0;
108
144
  let totalEmptyForLocale = 0;
@@ -170,6 +206,7 @@ async function generateStatusReport(config) {
170
206
  }
171
207
  return primaryState;
172
208
  };
209
+ const processedKeys = new Set();
173
210
  for (const { key: baseKey, hasCount, isOrdinal, isExpandedPlural } of keysInNs) {
174
211
  if (hasCount) {
175
212
  if (isExpandedPlural) {
@@ -183,7 +220,8 @@ async function generateStatusReport(config) {
183
220
  // Get the plural categories for this locale
184
221
  const localePluralCategories = getLocalePluralCategories(locale, isOrdinalVariant);
185
222
  // Only count this key if it's a plural form used by this locale
186
- if (localePluralCategories.includes(category)) {
223
+ if (localePluralCategories.includes(category) && !processedKeys.has(baseKey)) {
224
+ processedKeys.add(baseKey);
187
225
  totalInNs++;
188
226
  const state = resolveAndClassify(baseKey);
189
227
  if (state === 'translated')
@@ -200,10 +238,13 @@ async function generateStatusReport(config) {
200
238
  // Expand it according to THIS locale's plural rules
201
239
  const localePluralCategories = getLocalePluralCategories(locale, isOrdinal || false);
202
240
  for (const category of localePluralCategories) {
203
- totalInNs++;
204
241
  const pluralKey = isOrdinal
205
242
  ? `${baseKey}${pluralSeparator}ordinal${pluralSeparator}${category}`
206
243
  : `${baseKey}${pluralSeparator}${category}`;
244
+ if (processedKeys.has(pluralKey))
245
+ continue;
246
+ processedKeys.add(pluralKey);
247
+ totalInNs++;
207
248
  const state = resolveAndClassify(pluralKey);
208
249
  if (state === 'translated')
209
250
  translatedInNs++;
@@ -216,17 +257,37 @@ async function generateStatusReport(config) {
216
257
  }
217
258
  }
218
259
  else {
219
- totalInNs++;
220
- const state = resolveAndClassify(baseKey);
221
- if (state === 'translated')
222
- translatedInNs++;
223
- else if (state === 'empty')
224
- emptyInNs++;
225
- else
226
- absentInNs++;
227
- keyDetails.push({ key: baseKey, state });
260
+ if (!processedKeys.has(baseKey)) {
261
+ processedKeys.add(baseKey);
262
+ totalInNs++;
263
+ const state = resolveAndClassify(baseKey);
264
+ if (state === 'translated')
265
+ translatedInNs++;
266
+ else if (state === 'empty')
267
+ emptyInNs++;
268
+ else
269
+ absentInNs++;
270
+ keyDetails.push({ key: baseKey, state });
271
+ }
228
272
  }
229
273
  }
274
+ // Additionally check context variants discovered in the primary file
275
+ // (see issue #243). Skip variants already counted via extracted keys.
276
+ const contextVariants = contextVariantsByNs.get(ns) || [];
277
+ for (const variantKey of contextVariants) {
278
+ if (processedKeys.has(variantKey))
279
+ continue;
280
+ processedKeys.add(variantKey);
281
+ totalInNs++;
282
+ const state = resolveAndClassify(variantKey);
283
+ if (state === 'translated')
284
+ translatedInNs++;
285
+ else if (state === 'empty')
286
+ emptyInNs++;
287
+ else
288
+ absentInNs++;
289
+ keyDetails.push({ key: variantKey, state });
290
+ }
230
291
  namespaces.set(ns, { totalKeys: totalInNs, translatedKeys: translatedInNs, emptyKeys: emptyInNs, absentKeys: absentInNs, keyDetails });
231
292
  totalTranslatedForLocale += translatedInNs;
232
293
  totalEmptyForLocale += emptyInNs;