i18next-cli 1.82.3 → 1.82.5
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 +0 -2
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/extractor/core/translation-manager.js +1 -1
- package/dist/cjs/extractor/parsers/ast-utils.js +13 -0
- package/dist/cjs/extractor/parsers/call-expression-handler.js +1 -1
- package/dist/cjs/extractor/parsers/jsx-parser.js +1 -1
- package/dist/cjs/rename-key.js +9 -16
- package/dist/cjs/syncer.js +1 -1
- package/dist/cjs/utils/file-utils.js +16 -0
- package/dist/cjs/utils/message-format.js +1 -1
- package/dist/esm/cli.js +1 -1
- package/dist/esm/extractor/core/translation-manager.js +3 -3
- package/dist/esm/extractor/parsers/ast-utils.js +13 -1
- package/dist/esm/extractor/parsers/call-expression-handler.js +2 -2
- package/dist/esm/extractor/parsers/jsx-parser.js +2 -2
- package/dist/esm/rename-key.js +11 -18
- package/dist/esm/syncer.js +3 -3
- package/dist/esm/utils/file-utils.js +17 -2
- package/dist/esm/utils/message-format.js +3 -3
- package/package.json +1 -1
- package/types/extractor/parsers/ast-utils.d.ts +8 -0
- package/types/extractor/parsers/ast-utils.d.ts.map +1 -1
- package/types/utils/file-utils.d.ts +6 -0
- package/types/utils/file-utils.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -2145,8 +2145,6 @@ A translation management system built around the i18next ecosystem - [Locize](ht
|
|
|
2145
2145
|
|
|
2146
2146
|
**Now with a [Free plan](https://www.locize.com/pricing?utm_source=i18next_cli_readme&utm_medium=github&utm_campaign=readme) for small projects!** Perfect for hobbyists or getting started.
|
|
2147
2147
|
|
|
2148
|
-

|
|
2149
|
-
|
|
2150
2148
|
With using [Locize](https://www.locize.com/?utm_source=i18next_cli_readme&utm_medium=github&utm_campaign=readme) you directly support the future of i18next.
|
|
2151
2149
|
|
|
2152
2150
|
---
|
package/dist/cjs/cli.js
CHANGED
|
@@ -39,7 +39,7 @@ const program = new commander.Command();
|
|
|
39
39
|
program
|
|
40
40
|
.name('i18next-cli')
|
|
41
41
|
.description('A unified, high-performance i18next CLI.')
|
|
42
|
-
.version('1.82.
|
|
42
|
+
.version('1.82.5') // This string is replaced with the actual version at build time by rollup
|
|
43
43
|
.addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
|
|
44
44
|
// new: global config override option
|
|
45
45
|
program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
|
|
@@ -1245,7 +1245,7 @@ async function getTranslations(keys, objectKeys, config, { syncPrimaryWithDefaul
|
|
|
1245
1245
|
const existingNsGlobPattern = existingNsPattern.replace(/\\/g, '/');
|
|
1246
1246
|
const existingNsFiles = await glob.glob(existingNsGlobPattern, { ignore: userIgnore });
|
|
1247
1247
|
for (const file of existingNsFiles) {
|
|
1248
|
-
const ns =
|
|
1248
|
+
const ns = fileUtils.getNamespaceFromOutputPath(config.extract.output, locale, file);
|
|
1249
1249
|
if (ns) {
|
|
1250
1250
|
namespacesToProcess.add(ns);
|
|
1251
1251
|
}
|
|
@@ -342,6 +342,18 @@ function getObjectPropValueExpression(object, propName) {
|
|
|
342
342
|
// For shorthand properties like { ns }.
|
|
343
343
|
(p) => p.type === 'Identifier' && p.value === propName));
|
|
344
344
|
}
|
|
345
|
+
/**
|
|
346
|
+
* Checks if the options use the interval postProcessor (i18next-intervalPlural-postProcessor),
|
|
347
|
+
* as `postProcess: 'interval'` or `postProcess: ['interval']`. There `count` picks an interval
|
|
348
|
+
* from the plain key, so it gets no plural keys: i18next would find those first and break it.
|
|
349
|
+
*
|
|
350
|
+
* @private
|
|
351
|
+
*/
|
|
352
|
+
function usesIntervalPostProcessor(options) {
|
|
353
|
+
const postProcess = options && getObjectPropValueExpression(options, 'postProcess');
|
|
354
|
+
const postProcessors = postProcess?.type === 'ArrayExpression' ? postProcess.elements.map(e => e?.expression) : [postProcess];
|
|
355
|
+
return postProcessors.some(e => e?.type === 'StringLiteral' && e.value === 'interval');
|
|
356
|
+
}
|
|
345
357
|
/**
|
|
346
358
|
* Checks if the given template literal has no interpolation expressions
|
|
347
359
|
*
|
|
@@ -397,3 +409,4 @@ exports.lineColumnFromOffset = lineColumnFromOffset;
|
|
|
397
409
|
exports.normalizeASTSpans = normalizeASTSpans;
|
|
398
410
|
exports.normalizeSpansToCharIndices = normalizeSpansToCharIndices;
|
|
399
411
|
exports.unwrapParens = unwrapParens;
|
|
412
|
+
exports.usesIntervalPostProcessor = usesIntervalPostProcessor;
|
|
@@ -381,7 +381,7 @@ class CallExpressionHandler {
|
|
|
381
381
|
return undefined;
|
|
382
382
|
};
|
|
383
383
|
const hasCount = (() => {
|
|
384
|
-
if (!options || !Array.isArray(options.properties))
|
|
384
|
+
if (astUtils.usesIntervalPostProcessor(options) || !options || !Array.isArray(options.properties))
|
|
385
385
|
return false;
|
|
386
386
|
for (const p of options.properties) {
|
|
387
387
|
const name = propNameFromNode(p);
|
|
@@ -174,13 +174,13 @@ function extractFromTransComponent(node, config) {
|
|
|
174
174
|
// extraction time we can only inspect the AST shape, so we look for any
|
|
175
175
|
// ObjectExpression interpolation that declares a `count` key.
|
|
176
176
|
const hasInlineCount = !countAttr && !valuesCountProperty && childrenHaveInlineCount(node.children);
|
|
177
|
-
const hasCount = !!countAttr || !!valuesCountProperty || hasInlineCount;
|
|
178
177
|
const tOptionsAttr = node.opening.attributes?.find((attr) => attr.type === 'JSXAttribute' &&
|
|
179
178
|
attr.name.type === 'Identifier' &&
|
|
180
179
|
attr.name.value === 'tOptions');
|
|
181
180
|
const optionsNode = (tOptionsAttr?.type === 'JSXAttribute' && tOptionsAttr.value?.type === 'JSXExpressionContainer' && tOptionsAttr.value.expression.type === 'ObjectExpression')
|
|
182
181
|
? tOptionsAttr.value.expression
|
|
183
182
|
: undefined;
|
|
183
|
+
const hasCount = (!!countAttr || !!valuesCountProperty || hasInlineCount) && !astUtils.usesIntervalPostProcessor(optionsNode);
|
|
184
184
|
// Find isOrdinal prop on the <Trans> component
|
|
185
185
|
const ordinalAttr = node.opening.attributes?.find((attr) => attr.type === 'JSXAttribute' &&
|
|
186
186
|
attr.name.type === 'Identifier' &&
|
package/dist/cjs/rename-key.js
CHANGED
|
@@ -207,27 +207,20 @@ async function checkOldKeyExists(oldParts, config) {
|
|
|
207
207
|
async function buildNamespaceKeyMap(config) {
|
|
208
208
|
// Map namespace -> set of flattened keys present in that namespace (union across locales)
|
|
209
209
|
const map = new Map();
|
|
210
|
-
//
|
|
211
|
-
|
|
212
|
-
const
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
.replace(/\{\{language\}\}/g, '*')
|
|
219
|
-
.replace(/\{\{namespace\}\}/g, '*');
|
|
220
|
-
// glob expects unix-style
|
|
221
|
-
const files = await glob.glob([pat.replace(/\\/g, '/')], { nodir: true });
|
|
210
|
+
// glob each locale's files, so the namespace can be read from where the output template has it
|
|
211
|
+
const files = [];
|
|
212
|
+
for (const locale of config.locales) {
|
|
213
|
+
// glob expects unix-style
|
|
214
|
+
for (const f of await glob.glob(fileUtils.getOutputPath(config.extract.output, locale, '*').replace(/\\/g, '/'), { nodir: true })) {
|
|
215
|
+
files.push([f, fileUtils.getNamespaceFromOutputPath(config.extract.output, locale, f)]);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
222
218
|
const keySeparator = config.extract.keySeparator ?? '.';
|
|
223
|
-
for (const f of files) {
|
|
219
|
+
for (const [f, ns] of files) {
|
|
224
220
|
try {
|
|
225
221
|
const translations = await fileUtils.loadTranslationFile(node_path.resolve(process.cwd(), f));
|
|
226
222
|
if (!translations)
|
|
227
223
|
continue;
|
|
228
|
-
// derive namespace name from filename: basename without extension (platform-safe)
|
|
229
|
-
const base = node_path.basename(f);
|
|
230
|
-
const ns = base.replace(/\.[^.]+$/, ''); // remove extension
|
|
231
224
|
const set = map.get(ns) ?? new Set();
|
|
232
225
|
// flatten keys recursively
|
|
233
226
|
const collect = (obj, prefix = '') => {
|
package/dist/cjs/syncer.js
CHANGED
|
@@ -78,7 +78,7 @@ async function runSyncer(config, options = {}) {
|
|
|
78
78
|
const ignoreNamespaces = new Set(config.extract.ignoreNamespaces ?? []);
|
|
79
79
|
// 2. Loop through each primary namespace file
|
|
80
80
|
for (const primaryPath of primaryNsFiles) {
|
|
81
|
-
const ns =
|
|
81
|
+
const ns = fileUtils.getNamespaceFromOutputPath(output, primaryLanguage, primaryPath);
|
|
82
82
|
// Skip ignored namespaces
|
|
83
83
|
if (ignoreNamespaces.has(ns))
|
|
84
84
|
continue;
|
|
@@ -67,6 +67,21 @@ function getOutputPath(outputTemplate, language, namespace) {
|
|
|
67
67
|
out = out.replace(/\/\/+/g, '/');
|
|
68
68
|
return node_path.normalize(out);
|
|
69
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* The namespace of a file found with `getOutputPath(outputTemplate, language, '*')`: the part of its path
|
|
72
|
+
* where the template has the namespace, so `locales/{{namespace}}/{{language}}.json` works like
|
|
73
|
+
* `locales/{{language}}/{{namespace}}.json`. Falls back to the file name without its extension.
|
|
74
|
+
*/
|
|
75
|
+
function getNamespaceFromOutputPath(outputTemplate, language, filePath) {
|
|
76
|
+
const marker = '\u0000';
|
|
77
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
78
|
+
// compare absolute paths: glob returns relative or absolute paths depending on the pattern
|
|
79
|
+
const [first, ...rest] = node_path.resolve(getOutputPath(outputTemplate, language, marker)).replace(/\\/g, '/').split(marker);
|
|
80
|
+
// a namespace that appears more than once in the template must be the same each time;
|
|
81
|
+
// case-insensitive like glob on macOS and Windows
|
|
82
|
+
const match = rest.length > 0 && new RegExp(`^${escape(first)}([^/]+)${rest.map(escape).join('\\1')}$`, 'i').exec(node_path.resolve(filePath).replace(/\\/g, '/'));
|
|
83
|
+
return match ? match[1] : node_path.basename(filePath, node_path.extname(filePath));
|
|
84
|
+
}
|
|
70
85
|
/**
|
|
71
86
|
* Dynamically loads a translation file, supporting .json, .js, and .ts formats.
|
|
72
87
|
* @param filePath - The path to the translation file.
|
|
@@ -204,6 +219,7 @@ function inferFormatFromPath(filePath, defaultFormat = 'json') {
|
|
|
204
219
|
}
|
|
205
220
|
|
|
206
221
|
exports.ParseTranslationFileError = ParseTranslationFileError;
|
|
222
|
+
exports.getNamespaceFromOutputPath = getNamespaceFromOutputPath;
|
|
207
223
|
exports.getOutputPath = getOutputPath;
|
|
208
224
|
exports.inferFormatFromPath = inferFormatFromPath;
|
|
209
225
|
exports.loadRawJson5Content = loadRawJson5Content;
|
|
@@ -249,7 +249,7 @@ async function primaryNamespaces(config, primary) {
|
|
|
249
249
|
if (typeof output === 'string' && !output.includes('{{namespace}}'))
|
|
250
250
|
return [undefined];
|
|
251
251
|
return (await glob.glob(fileUtils.getOutputPath(output, primary, '*').replace(/\\/g, '/')))
|
|
252
|
-
.map(file =>
|
|
252
|
+
.map(file => fileUtils.getNamespaceFromOutputPath(output, primary, file))
|
|
253
253
|
.filter(ns => !ignoreNamespaces.includes(ns));
|
|
254
254
|
}
|
|
255
255
|
/**
|
package/dist/esm/cli.js
CHANGED
|
@@ -33,7 +33,7 @@ const program = new Command();
|
|
|
33
33
|
program
|
|
34
34
|
.name('i18next-cli')
|
|
35
35
|
.description('A unified, high-performance i18next CLI.')
|
|
36
|
-
.version('1.82.
|
|
36
|
+
.version('1.82.5') // This string is replaced with the actual version at build time by rollup
|
|
37
37
|
.addHelpText('after', '\nUsing an AI coding agent? Install the i18next-localization skill: npx skills add i18next/i18next-cli\n');
|
|
38
38
|
// new: global config override option
|
|
39
39
|
program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)');
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { resolve
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
2
|
import { glob } from 'glob';
|
|
3
3
|
import { naturalLanguageRegexFor, getNestedKeys, getNestedValue, setNestedValue, storedKeySeparator } from '../../utils/nested-object.js';
|
|
4
|
-
import { getOutputPath, loadTranslationFile } from '../../utils/file-utils.js';
|
|
4
|
+
import { getOutputPath, loadTranslationFile, getNamespaceFromOutputPath } 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';
|
|
@@ -1243,7 +1243,7 @@ async function getTranslations(keys, objectKeys, config, { syncPrimaryWithDefaul
|
|
|
1243
1243
|
const existingNsGlobPattern = existingNsPattern.replace(/\\/g, '/');
|
|
1244
1244
|
const existingNsFiles = await glob(existingNsGlobPattern, { ignore: userIgnore });
|
|
1245
1245
|
for (const file of existingNsFiles) {
|
|
1246
|
-
const ns =
|
|
1246
|
+
const ns = getNamespaceFromOutputPath(config.extract.output, locale, file);
|
|
1247
1247
|
if (ns) {
|
|
1248
1248
|
namespacesToProcess.add(ns);
|
|
1249
1249
|
}
|
|
@@ -340,6 +340,18 @@ function getObjectPropValueExpression(object, propName) {
|
|
|
340
340
|
// For shorthand properties like { ns }.
|
|
341
341
|
(p) => p.type === 'Identifier' && p.value === propName));
|
|
342
342
|
}
|
|
343
|
+
/**
|
|
344
|
+
* Checks if the options use the interval postProcessor (i18next-intervalPlural-postProcessor),
|
|
345
|
+
* as `postProcess: 'interval'` or `postProcess: ['interval']`. There `count` picks an interval
|
|
346
|
+
* from the plain key, so it gets no plural keys: i18next would find those first and break it.
|
|
347
|
+
*
|
|
348
|
+
* @private
|
|
349
|
+
*/
|
|
350
|
+
function usesIntervalPostProcessor(options) {
|
|
351
|
+
const postProcess = options && getObjectPropValueExpression(options, 'postProcess');
|
|
352
|
+
const postProcessors = postProcess?.type === 'ArrayExpression' ? postProcess.elements.map(e => e?.expression) : [postProcess];
|
|
353
|
+
return postProcessors.some(e => e?.type === 'StringLiteral' && e.value === 'interval');
|
|
354
|
+
}
|
|
343
355
|
/**
|
|
344
356
|
* Checks if the given template literal has no interpolation expressions
|
|
345
357
|
*
|
|
@@ -383,4 +395,4 @@ function getObjectPropValue(object, propName, identifierResolver, expressionReso
|
|
|
383
395
|
return expressionResolver?.(val) ?? ''; // Indicate presence for other types
|
|
384
396
|
}
|
|
385
397
|
|
|
386
|
-
export { buildByteToCharMap, collectIgnoredLineRanges, convertSpansToCharIndices, findFirstTokenIndex, getObjectPropValue, getObjectPropValueExpression, getObjectProperty, isSimpleTemplateLiteral, lineColumnFromOffset, normalizeASTSpans, normalizeSpansToCharIndices, unwrapParens };
|
|
398
|
+
export { buildByteToCharMap, collectIgnoredLineRanges, convertSpansToCharIndices, findFirstTokenIndex, getObjectPropValue, getObjectPropValueExpression, getObjectProperty, isSimpleTemplateLiteral, lineColumnFromOffset, normalizeASTSpans, normalizeSpansToCharIndices, unwrapParens, usesIntervalPostProcessor };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { safePluralRules } from '../../utils/plural-rules.js';
|
|
2
2
|
import { parseNestedReferences } from '../../utils/nesting.js';
|
|
3
3
|
import { usesMessageFormat } from '../../utils/message-format.js';
|
|
4
|
-
import { getObjectPropValue, unwrapParens, lineColumnFromOffset, isSimpleTemplateLiteral, getObjectPropValueExpression } from './ast-utils.js';
|
|
4
|
+
import { getObjectPropValue, unwrapParens, lineColumnFromOffset, isSimpleTemplateLiteral, getObjectPropValueExpression, usesIntervalPostProcessor } from './ast-utils.js';
|
|
5
5
|
import { matchesFunctionPattern } from '../utils/function-matcher.js';
|
|
6
6
|
|
|
7
7
|
// Helper to escape regex characters
|
|
@@ -379,7 +379,7 @@ class CallExpressionHandler {
|
|
|
379
379
|
return undefined;
|
|
380
380
|
};
|
|
381
381
|
const hasCount = (() => {
|
|
382
|
-
if (!options || !Array.isArray(options.properties))
|
|
382
|
+
if (usesIntervalPostProcessor(options) || !options || !Array.isArray(options.properties))
|
|
383
383
|
return false;
|
|
384
384
|
for (const p of options.properties) {
|
|
385
385
|
const name = propNameFromNode(p);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { getObjectPropValueExpression, getObjectPropValue, isSimpleTemplateLiteral } from './ast-utils.js';
|
|
1
|
+
import { getObjectPropValueExpression, usesIntervalPostProcessor, getObjectPropValue, isSimpleTemplateLiteral } from './ast-utils.js';
|
|
2
2
|
import { toMessageFormatPlaceholders } from '../../utils/message-format.js';
|
|
3
3
|
import { createRequire } from 'node:module';
|
|
4
4
|
import { parseSync } from '@swc/core';
|
|
@@ -171,13 +171,13 @@ function extractFromTransComponent(node, config) {
|
|
|
171
171
|
// extraction time we can only inspect the AST shape, so we look for any
|
|
172
172
|
// ObjectExpression interpolation that declares a `count` key.
|
|
173
173
|
const hasInlineCount = !countAttr && !valuesCountProperty && childrenHaveInlineCount(node.children);
|
|
174
|
-
const hasCount = !!countAttr || !!valuesCountProperty || hasInlineCount;
|
|
175
174
|
const tOptionsAttr = node.opening.attributes?.find((attr) => attr.type === 'JSXAttribute' &&
|
|
176
175
|
attr.name.type === 'Identifier' &&
|
|
177
176
|
attr.name.value === 'tOptions');
|
|
178
177
|
const optionsNode = (tOptionsAttr?.type === 'JSXAttribute' && tOptionsAttr.value?.type === 'JSXExpressionContainer' && tOptionsAttr.value.expression.type === 'ObjectExpression')
|
|
179
178
|
? tOptionsAttr.value.expression
|
|
180
179
|
: undefined;
|
|
180
|
+
const hasCount = (!!countAttr || !!valuesCountProperty || hasInlineCount) && !usesIntervalPostProcessor(optionsNode);
|
|
181
181
|
// Find isOrdinal prop on the <Trans> component
|
|
182
182
|
const ordinalAttr = node.opening.attributes?.find((attr) => attr.type === 'JSXAttribute' &&
|
|
183
183
|
attr.name.type === 'Identifier' &&
|
package/dist/esm/rename-key.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { glob } from 'glob';
|
|
2
2
|
import { readFile, writeFile } from 'node:fs/promises';
|
|
3
|
-
import { resolve
|
|
3
|
+
import { resolve } from 'node:path';
|
|
4
4
|
import { ConsoleLogger } from './utils/logger.js';
|
|
5
|
-
import { getOutputPath, loadTranslationFile, serializeTranslationFile } from './utils/file-utils.js';
|
|
5
|
+
import { getOutputPath, loadTranslationFile, getNamespaceFromOutputPath, serializeTranslationFile } from './utils/file-utils.js';
|
|
6
6
|
import { getNestedValue, naturalLanguageRegexFor, storedKeySeparator, setNestedValue } from './utils/nested-object.js';
|
|
7
7
|
import { shouldShowFunnel, recordFunnelShown } from './utils/funnel-msg-tracker.js';
|
|
8
8
|
import { styleText } from 'node:util';
|
|
@@ -205,27 +205,20 @@ async function checkOldKeyExists(oldParts, config) {
|
|
|
205
205
|
async function buildNamespaceKeyMap(config) {
|
|
206
206
|
// Map namespace -> set of flattened keys present in that namespace (union across locales)
|
|
207
207
|
const map = new Map();
|
|
208
|
-
//
|
|
209
|
-
|
|
210
|
-
const
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
.replace(/\{\{language\}\}/g, '*')
|
|
217
|
-
.replace(/\{\{namespace\}\}/g, '*');
|
|
218
|
-
// glob expects unix-style
|
|
219
|
-
const files = await glob([pat.replace(/\\/g, '/')], { nodir: true });
|
|
208
|
+
// glob each locale's files, so the namespace can be read from where the output template has it
|
|
209
|
+
const files = [];
|
|
210
|
+
for (const locale of config.locales) {
|
|
211
|
+
// glob expects unix-style
|
|
212
|
+
for (const f of await glob(getOutputPath(config.extract.output, locale, '*').replace(/\\/g, '/'), { nodir: true })) {
|
|
213
|
+
files.push([f, getNamespaceFromOutputPath(config.extract.output, locale, f)]);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
220
216
|
const keySeparator = config.extract.keySeparator ?? '.';
|
|
221
|
-
for (const f of files) {
|
|
217
|
+
for (const [f, ns] of files) {
|
|
222
218
|
try {
|
|
223
219
|
const translations = await loadTranslationFile(resolve(process.cwd(), f));
|
|
224
220
|
if (!translations)
|
|
225
221
|
continue;
|
|
226
|
-
// derive namespace name from filename: basename without extension (platform-safe)
|
|
227
|
-
const base = basename(f);
|
|
228
|
-
const ns = base.replace(/\.[^.]+$/, ''); // remove extension
|
|
229
222
|
const set = map.get(ns) ?? new Set();
|
|
230
223
|
// flatten keys recursively
|
|
231
224
|
const collect = (obj, prefix = '') => {
|
package/dist/esm/syncer.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { styleText } from 'node:util';
|
|
2
2
|
import { glob } from 'glob';
|
|
3
3
|
import { mkdir, writeFile } from 'node:fs/promises';
|
|
4
|
-
import {
|
|
4
|
+
import { resolve, dirname } from 'node:path';
|
|
5
5
|
import { createSpinnerLike } from './utils/wrap-ora.js';
|
|
6
6
|
import { ConsoleLogger } from './utils/logger.js';
|
|
7
7
|
import { resolveDefaultValue } from './utils/default-value.js';
|
|
8
|
-
import { getOutputPath, loadTranslationFile, inferFormatFromPath, loadRawJson5Content, serializeTranslationFile } from './utils/file-utils.js';
|
|
8
|
+
import { getOutputPath, getNamespaceFromOutputPath, loadTranslationFile, inferFormatFromPath, loadRawJson5Content, serializeTranslationFile } from './utils/file-utils.js';
|
|
9
9
|
import { printUntranslatedFunnel } from './utils/locize-funnel.js';
|
|
10
10
|
import { getNestedKeys, getNestedValue, setNestedValue } from './utils/nested-object.js';
|
|
11
11
|
import { safePluralRules } from './utils/plural-rules.js';
|
|
@@ -76,7 +76,7 @@ async function runSyncer(config, options = {}) {
|
|
|
76
76
|
const ignoreNamespaces = new Set(config.extract.ignoreNamespaces ?? []);
|
|
77
77
|
// 2. Loop through each primary namespace file
|
|
78
78
|
for (const primaryPath of primaryNsFiles) {
|
|
79
|
-
const ns =
|
|
79
|
+
const ns = getNamespaceFromOutputPath(output, primaryLanguage, primaryPath);
|
|
80
80
|
// Skip ignored namespaces
|
|
81
81
|
if (ignoreNamespaces.has(ns))
|
|
82
82
|
continue;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { access, readFile } from 'node:fs/promises';
|
|
2
|
-
import { resolve, normalize, extname } from 'node:path';
|
|
2
|
+
import { resolve, normalize, basename, extname } from 'node:path';
|
|
3
3
|
import { createJiti } from 'jiti';
|
|
4
4
|
import { getTsConfigAliases } from '../config.js';
|
|
5
5
|
import { JsonParser, JsonObjectNode } from '@croct/json5-parser';
|
|
@@ -61,6 +61,21 @@ function getOutputPath(outputTemplate, language, namespace) {
|
|
|
61
61
|
out = out.replace(/\/\/+/g, '/');
|
|
62
62
|
return normalize(out);
|
|
63
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* The namespace of a file found with `getOutputPath(outputTemplate, language, '*')`: the part of its path
|
|
66
|
+
* where the template has the namespace, so `locales/{{namespace}}/{{language}}.json` works like
|
|
67
|
+
* `locales/{{language}}/{{namespace}}.json`. Falls back to the file name without its extension.
|
|
68
|
+
*/
|
|
69
|
+
function getNamespaceFromOutputPath(outputTemplate, language, filePath) {
|
|
70
|
+
const marker = '\u0000';
|
|
71
|
+
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
72
|
+
// compare absolute paths: glob returns relative or absolute paths depending on the pattern
|
|
73
|
+
const [first, ...rest] = resolve(getOutputPath(outputTemplate, language, marker)).replace(/\\/g, '/').split(marker);
|
|
74
|
+
// a namespace that appears more than once in the template must be the same each time;
|
|
75
|
+
// case-insensitive like glob on macOS and Windows
|
|
76
|
+
const match = rest.length > 0 && new RegExp(`^${escape(first)}([^/]+)${rest.map(escape).join('\\1')}$`, 'i').exec(resolve(filePath).replace(/\\/g, '/'));
|
|
77
|
+
return match ? match[1] : basename(filePath, extname(filePath));
|
|
78
|
+
}
|
|
64
79
|
/**
|
|
65
80
|
* Dynamically loads a translation file, supporting .json, .js, and .ts formats.
|
|
66
81
|
* @param filePath - The path to the translation file.
|
|
@@ -197,4 +212,4 @@ function inferFormatFromPath(filePath, defaultFormat = 'json') {
|
|
|
197
212
|
return defaultFormat || 'json';
|
|
198
213
|
}
|
|
199
214
|
|
|
200
|
-
export { ParseTranslationFileError, getOutputPath, inferFormatFromPath, loadRawJson5Content, loadTranslationFile, serializeTranslationFile };
|
|
215
|
+
export { ParseTranslationFileError, getNamespaceFromOutputPath, getOutputPath, inferFormatFromPath, loadRawJson5Content, loadTranslationFile, serializeTranslationFile };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
|
-
import {
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
3
|
import { glob } from 'glob';
|
|
4
4
|
import { parseTree, findNodeAtLocation } from 'jsonc-parser';
|
|
5
|
-
import { getOutputPath, loadTranslationFile } from './file-utils.js';
|
|
5
|
+
import { getOutputPath, loadTranslationFile, getNamespaceFromOutputPath } from './file-utils.js';
|
|
6
6
|
import { parseNestedReferences } from './nesting.js';
|
|
7
7
|
|
|
8
8
|
const escapeRegExp = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
@@ -247,7 +247,7 @@ async function primaryNamespaces(config, primary) {
|
|
|
247
247
|
if (typeof output === 'string' && !output.includes('{{namespace}}'))
|
|
248
248
|
return [undefined];
|
|
249
249
|
return (await glob(getOutputPath(output, primary, '*').replace(/\\/g, '/')))
|
|
250
|
-
.map(file =>
|
|
250
|
+
.map(file => getNamespaceFromOutputPath(output, primary, file))
|
|
251
251
|
.filter(ns => !ignoreNamespaces.includes(ns));
|
|
252
252
|
}
|
|
253
253
|
/**
|
package/package.json
CHANGED
|
@@ -130,6 +130,14 @@ export declare function getObjectProperty(object: ObjectExpression, propName: st
|
|
|
130
130
|
* @returns The matching value node if found, otherwise undefined.
|
|
131
131
|
*/
|
|
132
132
|
export declare function getObjectPropValueExpression(object: ObjectExpression, propName: string): Expression | undefined;
|
|
133
|
+
/**
|
|
134
|
+
* Checks if the options use the interval postProcessor (i18next-intervalPlural-postProcessor),
|
|
135
|
+
* as `postProcess: 'interval'` or `postProcess: ['interval']`. There `count` picks an interval
|
|
136
|
+
* from the plain key, so it gets no plural keys: i18next would find those first and break it.
|
|
137
|
+
*
|
|
138
|
+
* @private
|
|
139
|
+
*/
|
|
140
|
+
export declare function usesIntervalPostProcessor(options?: ObjectExpression): boolean;
|
|
133
141
|
/**
|
|
134
142
|
* Checks if the given template literal has no interpolation expressions
|
|
135
143
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast-utils.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/ast-utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAc,gBAAgB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAE1F;;;;;;;;;GASG;AACH,wBAAgB,mBAAmB,CAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CA2BzD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CA0BhE;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAQhH;AAID;;;;;;;;;;GAUG;AACH,wBAAgB,kBAAkB,CAAE,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,IAAI,CA0BpE;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAE,IAAI,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,EAAE,GAAG,IAAI,CAwBhF;AAED;;;;;;;;;GASG;AACH,wBAAgB,2BAA2B,CAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAKzE;AAgBD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,wBAAwB,CAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CA4D7E;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,YAAY,CAAC,CAAC,EAAG,IAAI,EAAE,CAAC,GAAG,CAAC,C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kBAAkB,EAAE,kBAAkB,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,MAAM,GAAG,SAAS,GAAG,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,SAAS,CAU7N"}
|
|
@@ -60,6 +60,12 @@ export declare function writeFileAsync(filePath: string, data: string): Promise<
|
|
|
60
60
|
* - Normalizes duplicate slashes and returns a platform-correct path.
|
|
61
61
|
*/
|
|
62
62
|
export declare function getOutputPath(outputTemplate: string | ((language: string, namespace?: string) => string) | undefined, language: string, namespace?: string): string;
|
|
63
|
+
/**
|
|
64
|
+
* The namespace of a file found with `getOutputPath(outputTemplate, language, '*')`: the part of its path
|
|
65
|
+
* where the template has the namespace, so `locales/{{namespace}}/{{language}}.json` works like
|
|
66
|
+
* `locales/{{language}}/{{namespace}}.json`. Falls back to the file name without its extension.
|
|
67
|
+
*/
|
|
68
|
+
export declare function getNamespaceFromOutputPath(outputTemplate: Parameters<typeof getOutputPath>[0], language: string, filePath: string): string;
|
|
63
69
|
/**
|
|
64
70
|
* Dynamically loads a translation file, supporting .json, .js, and .ts formats.
|
|
65
71
|
* @param filePath - The path to the translation file.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-utils.d.ts","sourceRoot":"","sources":["../../src/utils/file-utils.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAKvD;;;;;;GAMG;AACH,qBAAa,yBAA0B,SAAQ,KAAK;aACrB,QAAQ,EAAE,MAAM;aAAkB,KAAK,CAAC,EAAE,OAAO;gBAAjD,QAAQ,EAAE,MAAM,EAAkB,KAAK,CAAC,EAAE,OAAO,YAAA;CAK/E;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,qBAAqB,CAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAG5E;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,aAAa,CAAE,QAAQ,EAAE,MAAM,GAAG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