i18next-cli 1.11.11 → 1.11.12

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/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.11.12](https://github.com/i18next/i18next-cli/compare/v1.11.11...v1.11.12) - 2025-10-21
9
+
10
+ - further improve jsx-parser [#66](https://github.com/i18next/i18next-cli/issues/66)
11
+
8
12
  ## [1.11.11](https://github.com/i18next/i18next-cli/compare/v1.11.10...v1.11.11) - 2025-10-20
9
13
 
10
14
  - Expose TranslationResult type for working with afterSync plugin / hook [#69](https://github.com/i18next/i18next-cli/issues/69)
package/dist/cjs/cli.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var e=require("commander"),t=require("chokidar"),n=require("glob"),o=require("chalk"),i=require("./config.js"),a=require("./heuristic-config.js"),r=require("./extractor/core/extractor.js");require("node:path"),require("node:fs/promises"),require("jiti");var c=require("./types-generator.js"),s=require("./syncer.js"),l=require("./migrator.js"),u=require("./init.js"),d=require("./linter.js"),g=require("./status.js"),p=require("./locize.js");const f=new e.Command;f.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.11.11"),f.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async e=>{try{const o=await i.ensureConfig(),a=async()=>{const t=await r.runExtractor(o,{isWatchMode:!!e.watch,isDryRun:!!e.dryRun,syncPrimaryWithDefaults:!!e.syncPrimary});return e.ci&&!t?(console.log("✅ No files were updated."),process.exit(0)):e.ci&&t&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),t};if(await a(),e.watch){console.log("\nWatching for changes...");t.watch(await n.glob(o.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}catch(e){console.error("Error running extractor:",e),process.exit(1)}}),f.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(e,t)=>{let n=await i.loadConfig();if(!n){console.log(o.blue("No config file found. Attempting to detect project structure..."));const e=await a.detectConfig();e||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),n=e}await g.runStatus(n,{detail:e,namespace:t.namespace})}),f.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async e=>{const o=await i.ensureConfig(),a=()=>c.runTypesGenerator(o);if(await a(),e.watch){console.log("\nWatching for changes...");t.watch(await n.glob(o.types?.input||[]),{persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}),f.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=await i.ensureConfig();await s.runSyncer(e)}),f.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await l.runMigrator(e)}),f.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(u.runInit),f.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async e=>{const r=async()=>{let e=await i.loadConfig();if(!e){console.log(o.blue("No config file found. Attempting to detect project structure..."));const t=await a.detectConfig();t||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),e=t}await d.runLinter(e)};if(await r(),e.watch){console.log("\nWatching for changes...");const e=await i.loadConfig();if(e?.extract?.input){t.watch(await n.glob(e.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),r()})}}}),f.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeSync(t,e)}),f.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeDownload(t,e)}),f.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeMigrate(t,e)}),f.parse(process.argv);
2
+ "use strict";var e=require("commander"),t=require("chokidar"),n=require("glob"),o=require("chalk"),i=require("./config.js"),a=require("./heuristic-config.js"),r=require("./extractor/core/extractor.js");require("node:path"),require("node:fs/promises"),require("jiti");var c=require("./types-generator.js"),s=require("./syncer.js"),l=require("./migrator.js"),u=require("./init.js"),d=require("./linter.js"),g=require("./status.js"),p=require("./locize.js");const f=new e.Command;f.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.11.12"),f.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async e=>{try{const o=await i.ensureConfig(),a=async()=>{const t=await r.runExtractor(o,{isWatchMode:!!e.watch,isDryRun:!!e.dryRun,syncPrimaryWithDefaults:!!e.syncPrimary});return e.ci&&!t?(console.log("✅ No files were updated."),process.exit(0)):e.ci&&t&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),t};if(await a(),e.watch){console.log("\nWatching for changes...");t.watch(await n.glob(o.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}catch(e){console.error("Error running extractor:",e),process.exit(1)}}),f.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(e,t)=>{let n=await i.loadConfig();if(!n){console.log(o.blue("No config file found. Attempting to detect project structure..."));const e=await a.detectConfig();e||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),n=e}await g.runStatus(n,{detail:e,namespace:t.namespace})}),f.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async e=>{const o=await i.ensureConfig(),a=()=>c.runTypesGenerator(o);if(await a(),e.watch){console.log("\nWatching for changes...");t.watch(await n.glob(o.types?.input||[]),{persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}),f.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=await i.ensureConfig();await s.runSyncer(e)}),f.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await l.runMigrator(e)}),f.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(u.runInit),f.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async e=>{const r=async()=>{let e=await i.loadConfig();if(!e){console.log(o.blue("No config file found. Attempting to detect project structure..."));const t=await a.detectConfig();t||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),e=t}await d.runLinter(e)};if(await r(),e.watch){console.log("\nWatching for changes...");const e=await i.loadConfig();if(e?.extract?.input){t.watch(await n.glob(e.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),r()})}}}),f.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeSync(t,e)}),f.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeDownload(t,e)}),f.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeMigrate(t,e)}),f.parse(process.argv);
@@ -1 +1 @@
1
- "use strict";var e=require("./ast-utils.js");exports.extractFromTransComponent=function(t,n){const i=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),r=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),s=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),p=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let o;s||"JSXAttribute"!==p?.type||"JSXExpressionContainer"!==p.value?.type||"ObjectExpression"!==p.value.expression.type||(o=e.getObjectProperty(p.value.expression,"count"));const l=!!s||!!o,a=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),u="JSXAttribute"===a?.type&&"JSXExpressionContainer"===a.value?.type&&"ObjectExpression"===a.value.expression.type?a.value.expression:void 0,y=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),f=!!y,c=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===c?.type&&"JSXExpressionContainer"===c.value?.type?c.value.expression:"JSXAttribute"===c?.type&&"StringLiteral"===c.value?.type?c.value:void 0;const v=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let g;if(g="JSXAttribute"===v?.type&&"StringLiteral"===v.value?.type?v.value.value:void 0,u&&(void 0===g&&(g=e.getObjectPropValue(u,"ns")),void 0===S)){const t=e.getObjectProperty(u,"context");t?.value&&(S=t.value)}const x=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),i=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,t,s=!1){if(!e||!e.length)return;let p=0,o=e.length-1;for(;p<=o&&i(e[p]);)p++;for(;o>=p&&i(e[o]);)o--;const l=p<=o?e.slice(p,o+1):[],a=l.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<l.length;e++){const p=l[e];if(p)if("JSXText"!==p.type)if("JSXExpressionContainer"!==p.type){if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&n.has(e)){!(!p.opening||!p.opening.selfClosing)&&t.push(p),r(p.children||[],t,!1)}else t.push(p),r(p.children||[],t,!0);continue}"JSXFragment"!==p.type||r(p.children||[],t,s)}else{if(s&&!a&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}if("StringLiteral"===e){const e=String(p.expression.value||"");if(!(/^\s*$/.test(e)&&!e.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}if(p.expression&&"StringLiteral"===p.expression.type){const n=String(p.expression.value||""),r=/^\s*$/.test(n)&&!n.includes("\n"),s=l[e-1],o=l[e+1];if(r){const n=l[e+2];if(o&&"JSXText"===o.type&&i(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=l[e-1],n=l[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue;const r=!o||"JSXText"===o.type&&!i(o);if(s&&"JSXText"===s.type&&r){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue}}t.push(p)}else{if(s&&!a)continue;if(s&&i(p))continue;if(i(p)){const n=t[t.length-1],i=l[e-1];if(n){if(i&&"JSXExpressionContainer"===i.type)continue;if("JSXText"===n.type&&i&&"JSXText"===i.type){n.value=String(n.value)+p.value;continue}}}if(s&&a&&0===e)continue;t.push(p)}}}const s=[];function p(e){if(!e||0===e.length)return"";let t="",r=!1;for(let o=0;o<e.length;o++){const l=e[o];if(l)if("JSXText"!==l.type){if("JSXExpressionContainer"===l.type){const e=l.expression;if(!e)continue;if("StringLiteral"===e.type)t+=e.value;else if("Identifier"===e.type)t+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const n=e.properties[0];n&&"KeyValueProperty"===n.type&&n.key&&"Identifier"===n.key.type?t+=`{{${n.key.value}}}`:n&&"Identifier"===n.type?t+=`{{${n.value}}}`:t+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?t+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?t+=`{{${e.callee.value}}}`:t+="{{value}}";r=!1;continue}if("JSXElement"===l.type){let i;if(l.opening&&l.opening.name&&"Identifier"===l.opening.name.type&&(i=l.opening.name.value),i&&n.has(i)){const n=p(l.children||[]),s=!(!l.opening||!l.opening.selfClosing),a=""!==String(n).trim();if(s||!a){const n=e[o-1];n&&"JSXText"===n.type&&/\n\s*$/.test(n.value)&&(t=t.replace(/\s+$/,"")),t+=`<${i}/>`,r=!0}else t+=`<${i}>${n}</${i}>`,r=!1}else{const e=s.indexOf(l),n=p(l.children||[]);t+=`<${e}>${String(n).trim()}</${e}>`,r=!1}continue}"JSXFragment"!==l.type||(t+=p(l.children||[]),r=!1)}else{if(i(l))continue;r?(t+=l.value.replace(/^\s+/,""),r=!1):t+=l.value}}return t}r(e,s,!1);const o=p(e);return String(o).replace(/\s+/g," ").trim()}(t.children,n);let d,J,X;if("JSXAttribute"===r?.type&&"StringLiteral"===r.value?.type)d=r.value.value;else{const e=n.extract.defaultValue;d="string"==typeof e?e:""}if("JSXAttribute"===i?.type){if("StringLiteral"===i.value?.type){if(J=i.value,X=J.value,!X||""===X.trim())return null;if(g&&"StringLiteral"===J.type){const e=n.extract.nsSeparator??":",t=J.value;if(e&&t.startsWith(`${g}${e}`)){if(X=t.slice(`${g}${e}`.length),!X||""===X.trim())return null;J={...J,value:X}}}}else"JSXExpressionContainer"===i.value?.type&&"JSXEmptyExpression"!==i.value.expression.type&&(J=i.value.expression);if(!J)return null}return r||!X||x.trim()?!r&&x.trim()&&(d=x):d=X,{keyExpression:J,serializedChildren:x,ns:g,defaultValue:d,hasCount:l,isOrdinal:f,contextExpression:S,optionsNode:u,explicitDefault:Boolean(r&&"JSXAttribute"===r.type&&"StringLiteral"===r.value?.type||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(u))}};
1
+ "use strict";var e=require("./ast-utils.js");exports.extractFromTransComponent=function(t,n){const i=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),r=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),s=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),p=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let o;s||"JSXAttribute"!==p?.type||"JSXExpressionContainer"!==p.value?.type||"ObjectExpression"!==p.value.expression.type||(o=e.getObjectProperty(p.value.expression,"count"));const l=!!s||!!o,a=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),u="JSXAttribute"===a?.type&&"JSXExpressionContainer"===a.value?.type&&"ObjectExpression"===a.value.expression.type?a.value.expression:void 0,y=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),f=!!y,c=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===c?.type&&"JSXExpressionContainer"===c.value?.type?c.value.expression:"JSXAttribute"===c?.type&&"StringLiteral"===c.value?.type?c.value:void 0;const g=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let v;if(v="JSXAttribute"===g?.type&&"StringLiteral"===g.value?.type?g.value.value:void 0,u&&(void 0===v&&(v=e.getObjectPropValue(u,"ns")),void 0===S)){const t=e.getObjectProperty(u,"context");t?.value&&(S=t.value)}const x=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),i=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,t,s=!1){if(!e||!e.length)return;let p=0,o=e.length-1;for(;p<=o&&i(e[p]);)p++;for(;o>=p&&i(e[o]);)o--;const l=p<=o?e.slice(p,o+1):[],a=l.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<l.length;e++){const p=l[e];if(p)if("JSXText"!==p.type)if("JSXExpressionContainer"!==p.type){if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&n.has(e)){!(!p.opening||!p.opening.selfClosing)&&t.push(p),r(p.children||[],t,!1)}else t.push(p),r(p.children||[],t,!0);continue}"JSXFragment"!==p.type||r(p.children||[],t,s)}else{if(s&&!a&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}if("StringLiteral"===e){const e=String(p.expression.value||"");if(!(/^\s*$/.test(e)&&!e.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}if(p.expression&&"StringLiteral"===p.expression.type){const n=String(p.expression.value||""),r=/^\s*$/.test(n)&&!n.includes("\n"),s=l[e-1],o=l[e+1];if(r){const n=l[e+2];if(o&&"JSXText"===o.type&&i(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=l[e-1],n=l[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue;const r=!o||"JSXText"===o.type&&!i(o);if(s&&"JSXText"===s.type&&r){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue}}t.push(p)}else{if(s&&!a)continue;if(s&&i(p))continue;if(i(p)){const n=l[e-1],i=l[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const r=t[t.length-1],s=l[e-1];if(r){if(s&&"JSXExpressionContainer"===s.type)continue;if("JSXText"===r.type&&s&&"JSXText"===s.type){r.value=String(r.value)+p.value;continue}}}if(s&&a&&0===e)continue;t.push(p)}}}const s=[];r(e,s,!1);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function o(e,t){if(!e||0===e.length)return"";let r="",l=!1;for(let a=0;a<e.length;a++){const u=e[a];if(u)if("JSXText"!==u.type){if("JSXExpressionContainer"===u.type){const e=u.expression;if(!e)continue;if("StringLiteral"===e.type)r+=e.value;else if("Identifier"===e.type)r+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?r+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?r+=`{{${t.value}}}`:r+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?r+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?r+=`{{${e.callee.value}}}`:r+="{{value}}";l=!1;continue}if("JSXElement"===u.type){let i;if(u.opening&&u.opening.name&&"Identifier"===u.opening.name.type&&(i=u.opening.name.value),i&&n.has(i)){const n=o(u.children||[],t),s=!(!u.opening||!u.opening.selfClosing),p=""!==String(n).trim();if(s||!p){const t=e[a-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(r=r.replace(/\s+$/,"")),r+=`<${i}/>`,l=!0}else r+=`<${i}>${n}</${i}>`,l=!1}else{const e=u.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==s.indexOf(e))){const t=s.indexOf(u),n=o(e,void 0);r+=`<${t}>${p(n)}</${t}>`,l=!1}else{const i=new Map;let a=0;for(const t of e)if(t&&"JSXElement"===t.type){const e=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(e&&n.has(e)){!(!t.opening||!t.opening.selfClosing)&&i.set(t,a++)}else i.set(t,a++)}const y=t&&t.has(u)?t.get(u):s.indexOf(u),f=o(e,i.size?i:void 0);r+=`<${y}>${p(f)}</${y}>`,l=!1}}continue}"JSXFragment"!==u.type||(r+=o(u.children||[]),l=!1)}else{if(i(u))continue;l?(r+=u.value.replace(/^\s+/,""),l=!1):r+=u.value}}return r}const l=o(e);return String(l).replace(/\s+/g," ").trim()}(t.children,n);let d,J,X;if("JSXAttribute"===r?.type&&"StringLiteral"===r.value?.type)d=r.value.value;else{const e=n.extract.defaultValue;d="string"==typeof e?e:""}if("JSXAttribute"===i?.type){if("StringLiteral"===i.value?.type){if(J=i.value,X=J.value,!X||""===X.trim())return null;if(v&&"StringLiteral"===J.type){const e=n.extract.nsSeparator??":",t=J.value;if(e&&t.startsWith(`${v}${e}`)){if(X=t.slice(`${v}${e}`.length),!X||""===X.trim())return null;J={...J,value:X}}}}else"JSXExpressionContainer"===i.value?.type&&"JSXEmptyExpression"!==i.value.expression.type&&(J=i.value.expression);if(!J)return null}return r||!X||x.trim()?!r&&x.trim()&&(d=x):d=X,{keyExpression:J,serializedChildren:x,ns:v,defaultValue:d,hasCount:l,isOrdinal:f,contextExpression:S,optionsNode:u,explicitDefault:Boolean(r&&"JSXAttribute"===r.type&&"StringLiteral"===r.value?.type||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(u))}};
package/dist/esm/cli.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import{Command as t}from"commander";import e from"chokidar";import{glob as o}from"glob";import n from"chalk";import{ensureConfig as i,loadConfig as a}from"./config.js";import{detectConfig as c}from"./heuristic-config.js";import{runExtractor as r}from"./extractor/core/extractor.js";import"node:path";import"node:fs/promises";import"jiti";import{runTypesGenerator as s}from"./types-generator.js";import{runSyncer as l}from"./syncer.js";import{runMigrator as p}from"./migrator.js";import{runInit as m}from"./init.js";import{runLinter as d}from"./linter.js";import{runStatus as u}from"./status.js";import{runLocizeSync as f,runLocizeDownload as g,runLocizeMigrate as h}from"./locize.js";const y=new t;y.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.11.11"),y.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async t=>{try{const n=await i(),a=async()=>{const e=await r(n,{isWatchMode:!!t.watch,isDryRun:!!t.dryRun,syncPrimaryWithDefaults:!!t.syncPrimary});return t.ci&&!e?(console.log("✅ No files were updated."),process.exit(0)):t.ci&&e&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),e};if(await a(),t.watch){console.log("\nWatching for changes...");e.watch(await o(n.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),a()})}}catch(t){console.error("Error running extractor:",t),process.exit(1)}}),y.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(t,e)=>{let o=await a();if(!o){console.log(n.blue("No config file found. Attempting to detect project structure..."));const t=await c();t||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),o=t}await u(o,{detail:t,namespace:e.namespace})}),y.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async t=>{const n=await i(),a=()=>s(n);if(await a(),t.watch){console.log("\nWatching for changes...");e.watch(await o(n.types?.input||[]),{persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),a()})}}),y.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const t=await i();await l(t)}),y.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async t=>{await p(t)}),y.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(m),y.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async t=>{const i=async()=>{let t=await a();if(!t){console.log(n.blue("No config file found. Attempting to detect project structure..."));const e=await c();e||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),t=e}await d(t)};if(await i(),t.watch){console.log("\nWatching for changes...");const t=await a();if(t?.extract?.input){e.watch(await o(t.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),i()})}}}),y.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async t=>{const e=await i();await f(e,t)}),y.command("locize-download").description("Download all translations from your locize project.").action(async t=>{const e=await i();await g(e,t)}),y.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async t=>{const e=await i();await h(e,t)}),y.parse(process.argv);
2
+ import{Command as t}from"commander";import e from"chokidar";import{glob as o}from"glob";import n from"chalk";import{ensureConfig as i,loadConfig as a}from"./config.js";import{detectConfig as c}from"./heuristic-config.js";import{runExtractor as r}from"./extractor/core/extractor.js";import"node:path";import"node:fs/promises";import"jiti";import{runTypesGenerator as s}from"./types-generator.js";import{runSyncer as l}from"./syncer.js";import{runMigrator as p}from"./migrator.js";import{runInit as m}from"./init.js";import{runLinter as d}from"./linter.js";import{runStatus as u}from"./status.js";import{runLocizeSync as f,runLocizeDownload as g,runLocizeMigrate as h}from"./locize.js";const y=new t;y.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.11.12"),y.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async t=>{try{const n=await i(),a=async()=>{const e=await r(n,{isWatchMode:!!t.watch,isDryRun:!!t.dryRun,syncPrimaryWithDefaults:!!t.syncPrimary});return t.ci&&!e?(console.log("✅ No files were updated."),process.exit(0)):t.ci&&e&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),e};if(await a(),t.watch){console.log("\nWatching for changes...");e.watch(await o(n.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),a()})}}catch(t){console.error("Error running extractor:",t),process.exit(1)}}),y.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(t,e)=>{let o=await a();if(!o){console.log(n.blue("No config file found. Attempting to detect project structure..."));const t=await c();t||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),o=t}await u(o,{detail:t,namespace:e.namespace})}),y.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async t=>{const n=await i(),a=()=>s(n);if(await a(),t.watch){console.log("\nWatching for changes...");e.watch(await o(n.types?.input||[]),{persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),a()})}}),y.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const t=await i();await l(t)}),y.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async t=>{await p(t)}),y.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(m),y.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async t=>{const i=async()=>{let t=await a();if(!t){console.log(n.blue("No config file found. Attempting to detect project structure..."));const e=await c();e||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),t=e}await d(t)};if(await i(),t.watch){console.log("\nWatching for changes...");const t=await a();if(t?.extract?.input){e.watch(await o(t.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),i()})}}}),y.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async t=>{const e=await i();await f(e,t)}),y.command("locize-download").description("Download all translations from your locize project.").action(async t=>{const e=await i();await g(e,t)}),y.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async t=>{const e=await i();await h(e,t)}),y.parse(process.argv);
@@ -1 +1 @@
1
- import{getObjectProperty as e,getObjectPropValue as t}from"./ast-utils.js";function n(n,i){const r=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),s=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),o=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let l;p||"JSXAttribute"!==o?.type||"JSXExpressionContainer"!==o.value?.type||"ObjectExpression"!==o.value.expression.type||(l=e(o.value.expression,"count"));const a=!!p||!!l,u=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),y="JSXAttribute"===u?.type&&"JSXExpressionContainer"===u.value?.type&&"ObjectExpression"===u.value.expression.type?u.value.expression:void 0,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),c=!!f,S=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===S?.type&&"JSXExpressionContainer"===S.value?.type?S.value.expression:"JSXAttribute"===S?.type&&"StringLiteral"===S.value?.type?S.value:void 0;const g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let x;if(x="JSXAttribute"===g?.type&&"StringLiteral"===g.value?.type?g.value.value:void 0,y&&(void 0===x&&(x=t(y,"ns")),void 0===v)){const t=e(y,"context");t?.value&&(v=t.value)}const d=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),i=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,t,s=!1){if(!e||!e.length)return;let p=0,o=e.length-1;for(;p<=o&&i(e[p]);)p++;for(;o>=p&&i(e[o]);)o--;const l=p<=o?e.slice(p,o+1):[],a=l.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<l.length;e++){const p=l[e];if(p)if("JSXText"!==p.type)if("JSXExpressionContainer"!==p.type){if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&n.has(e)){!(!p.opening||!p.opening.selfClosing)&&t.push(p),r(p.children||[],t,!1)}else t.push(p),r(p.children||[],t,!0);continue}"JSXFragment"!==p.type||r(p.children||[],t,s)}else{if(s&&!a&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}if("StringLiteral"===e){const e=String(p.expression.value||"");if(!(/^\s*$/.test(e)&&!e.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}if(p.expression&&"StringLiteral"===p.expression.type){const n=String(p.expression.value||""),r=/^\s*$/.test(n)&&!n.includes("\n"),s=l[e-1],o=l[e+1];if(r){const n=l[e+2];if(o&&"JSXText"===o.type&&i(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=l[e-1],n=l[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue;const r=!o||"JSXText"===o.type&&!i(o);if(s&&"JSXText"===s.type&&r){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue}}t.push(p)}else{if(s&&!a)continue;if(s&&i(p))continue;if(i(p)){const n=t[t.length-1],i=l[e-1];if(n){if(i&&"JSXExpressionContainer"===i.type)continue;if("JSXText"===n.type&&i&&"JSXText"===i.type){n.value=String(n.value)+p.value;continue}}}if(s&&a&&0===e)continue;t.push(p)}}}const s=[];function p(e){if(!e||0===e.length)return"";let t="",r=!1;for(let o=0;o<e.length;o++){const l=e[o];if(l)if("JSXText"!==l.type){if("JSXExpressionContainer"===l.type){const e=l.expression;if(!e)continue;if("StringLiteral"===e.type)t+=e.value;else if("Identifier"===e.type)t+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const n=e.properties[0];n&&"KeyValueProperty"===n.type&&n.key&&"Identifier"===n.key.type?t+=`{{${n.key.value}}}`:n&&"Identifier"===n.type?t+=`{{${n.value}}}`:t+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?t+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?t+=`{{${e.callee.value}}}`:t+="{{value}}";r=!1;continue}if("JSXElement"===l.type){let i;if(l.opening&&l.opening.name&&"Identifier"===l.opening.name.type&&(i=l.opening.name.value),i&&n.has(i)){const n=p(l.children||[]),s=!(!l.opening||!l.opening.selfClosing),a=""!==String(n).trim();if(s||!a){const n=e[o-1];n&&"JSXText"===n.type&&/\n\s*$/.test(n.value)&&(t=t.replace(/\s+$/,"")),t+=`<${i}/>`,r=!0}else t+=`<${i}>${n}</${i}>`,r=!1}else{const e=s.indexOf(l),n=p(l.children||[]);t+=`<${e}>${String(n).trim()}</${e}>`,r=!1}continue}"JSXFragment"!==l.type||(t+=p(l.children||[]),r=!1)}else{if(i(l))continue;r?(t+=l.value.replace(/^\s+/,""),r=!1):t+=l.value}}return t}r(e,s,!1);const o=p(e);return String(o).replace(/\s+/g," ").trim()}(n.children,i);let J,X,m;if("JSXAttribute"===s?.type&&"StringLiteral"===s.value?.type)J=s.value.value;else{const e=i.extract.defaultValue;J="string"==typeof e?e:""}if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(X=r.value,m=X.value,!m||""===m.trim())return null;if(x&&"StringLiteral"===X.type){const e=i.extract.nsSeparator??":",t=X.value;if(e&&t.startsWith(`${x}${e}`)){if(m=t.slice(`${x}${e}`.length),!m||""===m.trim())return null;X={...X,value:m}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(X=r.value.expression);if(!X)return null}s||!m||d.trim()?!s&&d.trim()&&(J=d):J=m;return{keyExpression:X,serializedChildren:d,ns:x,defaultValue:J,hasCount:a,isOrdinal:c,contextExpression:v,optionsNode:y,explicitDefault:Boolean(s&&"JSXAttribute"===s.type&&"StringLiteral"===s.value?.type||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(y))}}export{n as extractFromTransComponent};
1
+ import{getObjectProperty as e,getObjectPropValue as t}from"./ast-utils.js";function n(n,i){const r=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),s=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),o=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let l;p||"JSXAttribute"!==o?.type||"JSXExpressionContainer"!==o.value?.type||"ObjectExpression"!==o.value.expression.type||(l=e(o.value.expression,"count"));const a=!!p||!!l,u=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),y="JSXAttribute"===u?.type&&"JSXExpressionContainer"===u.value?.type&&"ObjectExpression"===u.value.expression.type?u.value.expression:void 0,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),c=!!f,S=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===S?.type&&"JSXExpressionContainer"===S.value?.type?S.value.expression:"JSXAttribute"===S?.type&&"StringLiteral"===S.value?.type?S.value:void 0;const g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let x;if(x="JSXAttribute"===g?.type&&"StringLiteral"===g.value?.type?g.value.value:void 0,y&&(void 0===x&&(x=t(y,"ns")),void 0===v)){const t=e(y,"context");t?.value&&(v=t.value)}const d=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),i=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(e,t,s=!1){if(!e||!e.length)return;let p=0,o=e.length-1;for(;p<=o&&i(e[p]);)p++;for(;o>=p&&i(e[o]);)o--;const l=p<=o?e.slice(p,o+1):[],a=l.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<l.length;e++){const p=l[e];if(p)if("JSXText"!==p.type)if("JSXExpressionContainer"!==p.type){if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&n.has(e)){!(!p.opening||!p.opening.selfClosing)&&t.push(p),r(p.children||[],t,!1)}else t.push(p),r(p.children||[],t,!0);continue}"JSXFragment"!==p.type||r(p.children||[],t,s)}else{if(s&&!a&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}if("StringLiteral"===e){const e=String(p.expression.value||"");if(!(/^\s*$/.test(e)&&!e.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}if(p.expression&&"StringLiteral"===p.expression.type){const n=String(p.expression.value||""),r=/^\s*$/.test(n)&&!n.includes("\n"),s=l[e-1],o=l[e+1];if(r){const n=l[e+2];if(o&&"JSXText"===o.type&&i(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=l[e-1],n=l[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue;const r=!o||"JSXText"===o.type&&!i(o);if(s&&"JSXText"===s.type&&r){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&i(o))continue}}t.push(p)}else{if(s&&!a)continue;if(s&&i(p))continue;if(i(p)){const n=l[e-1],i=l[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const r=t[t.length-1],s=l[e-1];if(r){if(s&&"JSXExpressionContainer"===s.type)continue;if("JSXText"===r.type&&s&&"JSXText"===s.type){r.value=String(r.value)+p.value;continue}}}if(s&&a&&0===e)continue;t.push(p)}}}const s=[];r(e,s,!1);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function o(e,t){if(!e||0===e.length)return"";let r="",l=!1;for(let a=0;a<e.length;a++){const u=e[a];if(u)if("JSXText"!==u.type){if("JSXExpressionContainer"===u.type){const e=u.expression;if(!e)continue;if("StringLiteral"===e.type)r+=e.value;else if("Identifier"===e.type)r+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?r+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?r+=`{{${t.value}}}`:r+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?r+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?r+=`{{${e.callee.value}}}`:r+="{{value}}";l=!1;continue}if("JSXElement"===u.type){let i;if(u.opening&&u.opening.name&&"Identifier"===u.opening.name.type&&(i=u.opening.name.value),i&&n.has(i)){const n=o(u.children||[],t),s=!(!u.opening||!u.opening.selfClosing),p=""!==String(n).trim();if(s||!p){const t=e[a-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(r=r.replace(/\s+$/,"")),r+=`<${i}/>`,l=!0}else r+=`<${i}>${n}</${i}>`,l=!1}else{const e=u.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==s.indexOf(e))){const t=s.indexOf(u),n=o(e,void 0);r+=`<${t}>${p(n)}</${t}>`,l=!1}else{const i=new Map;let a=0;for(const t of e)if(t&&"JSXElement"===t.type){const e=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(e&&n.has(e)){!(!t.opening||!t.opening.selfClosing)&&i.set(t,a++)}else i.set(t,a++)}const y=t&&t.has(u)?t.get(u):s.indexOf(u),f=o(e,i.size?i:void 0);r+=`<${y}>${p(f)}</${y}>`,l=!1}}continue}"JSXFragment"!==u.type||(r+=o(u.children||[]),l=!1)}else{if(i(u))continue;l?(r+=u.value.replace(/^\s+/,""),l=!1):r+=u.value}}return r}const l=o(e);return String(l).replace(/\s+/g," ").trim()}(n.children,i);let J,X,m;if("JSXAttribute"===s?.type&&"StringLiteral"===s.value?.type)J=s.value.value;else{const e=i.extract.defaultValue;J="string"==typeof e?e:""}if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(X=r.value,m=X.value,!m||""===m.trim())return null;if(x&&"StringLiteral"===X.type){const e=i.extract.nsSeparator??":",t=X.value;if(e&&t.startsWith(`${x}${e}`)){if(m=t.slice(`${x}${e}`.length),!m||""===m.trim())return null;X={...X,value:m}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(X=r.value.expression);if(!X)return null}s||!m||d.trim()?!s&&d.trim()&&(J=d):J=m;return{keyExpression:X,serializedChildren:d,ns:x,defaultValue:J,hasCount:a,isOrdinal:c,contextExpression:v,optionsNode:y,explicitDefault:Boolean(s&&"JSXAttribute"===s.type&&"StringLiteral"===s.value?.type||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(y))}}export{n as extractFromTransComponent};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-cli",
3
- "version": "1.11.11",
3
+ "version": "1.11.12",
4
4
  "description": "A unified, high-performance i18next CLI.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli.ts CHANGED
@@ -21,7 +21,7 @@ const program = new Command()
21
21
  program
22
22
  .name('i18next-cli')
23
23
  .description('A unified, high-performance i18next CLI.')
24
- .version('1.11.11')
24
+ .version('1.11.12')
25
25
 
26
26
  program
27
27
  .command('extract')
@@ -323,6 +323,20 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
323
323
 
324
324
  // Otherwise, preserve previous behavior for boundary/formatting merging.
325
325
  if (isFormattingWhitespace(n)) {
326
+ // If this formatting whitespace sits between two element/fragment nodes
327
+ // (e.g. <br />\n <small>...), treat it as layout-only and skip it so it
328
+ // doesn't become its own slot and shift subsequent element indexes.
329
+ const prevOrig = meaningfulNodes[i - 1]
330
+ const nextOrig = meaningfulNodes[i + 1]
331
+ if (
332
+ prevOrig &&
333
+ (prevOrig.type === 'JSXElement' || prevOrig.type === 'JSXFragment') &&
334
+ nextOrig &&
335
+ (nextOrig.type === 'JSXElement' || nextOrig.type === 'JSXFragment')
336
+ ) {
337
+ continue
338
+ }
339
+
326
340
  const prevSlot = slots[slots.length - 1]
327
341
  const prevOriginal = meaningfulNodes[i - 1]
328
342
 
@@ -504,7 +518,16 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
504
518
  const globalSlots: any[] = []
505
519
  collectSlots(children, globalSlots, false)
506
520
 
507
- function visitNodes (nodes: any[]): string {
521
+ // Trim only newline-only indentation at the edges of serialized inner text.
522
+ // This preserves single leading/trailing spaces which are meaningful between inline placeholders.
523
+ const trimFormattingEdges = (s: string) =>
524
+ String(s)
525
+ // remove leading newline-only indentation
526
+ .replace(/^\s*\n\s*/g, '')
527
+ // remove trailing newline-only indentation
528
+ .replace(/\s*\n\s*$/g, '')
529
+
530
+ function visitNodes (nodes: any[], localIndexMap?: Map<any, number>): string {
508
531
  if (!nodes || nodes.length === 0) return ''
509
532
  let out = ''
510
533
  let lastWasSelfClosing = false
@@ -559,7 +582,7 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
559
582
  }
560
583
 
561
584
  if (tag && allowedTags.has(tag)) {
562
- const inner = visitNodes(node.children || [])
585
+ const inner = visitNodes(node.children || [], localIndexMap)
563
586
  // consider element self-closing for rendering when AST marks it so or it has no meaningful children
564
587
  const isAstSelfClosing = !!(node.opening && (node.opening as any).selfClosing)
565
588
  const hasMeaningfulChildren = String(inner).trim() !== ''
@@ -579,14 +602,49 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
579
602
  lastWasSelfClosing = false
580
603
  }
581
604
  } else {
582
- // Use the pre-order globalSlots index so placeholder numbers reflect
583
- // the global ordering (including nested slots collected earlier).
584
- const idx = globalSlots.indexOf(node)
585
- const inner = visitNodes(node.children || [])
586
- // Trim leading/trailing whitespace inside non-preserved element placeholders
587
- // so formatting/indentation in source doesn't leak into the output.
588
- out += `<${idx}>${String(inner).trim()}</${idx}>`
589
- lastWasSelfClosing = false
605
+ // Decide whether to use local (restarted) indexes for this element's
606
+ // immediate children or fall back to the global pre-order indexes.
607
+ // If the element's children contain non-element slots (JSXText /
608
+ // JSXExpressionContainer) that were collected as global slots, we must
609
+ // use the global indexes so those text slots keep their positions.
610
+ const children = node.children || []
611
+ const hasNonElementGlobalSlots = children.some((ch: any) =>
612
+ ch && (ch.type === 'JSXText' || ch.type === 'JSXExpressionContainer') && globalSlots.indexOf(ch) !== -1
613
+ )
614
+
615
+ // If there are non-element global slots among the children, render using
616
+ // global indexes. Otherwise build a compact local index map so nested
617
+ // placeholders restart at 0 inside this parent element.
618
+ if (hasNonElementGlobalSlots) {
619
+ const idx = globalSlots.indexOf(node)
620
+ const inner = visitNodes(children, undefined)
621
+ out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
622
+ lastWasSelfClosing = false
623
+ } else {
624
+ const childrenLocalMap = new Map<any, number>()
625
+ let localIdxCounter = 0
626
+ for (const ch of children) {
627
+ if (!ch) continue
628
+ if (ch.type === 'JSXElement') {
629
+ const chTag = ch.opening && ch.opening.name && ch.opening.name.type === 'Identifier'
630
+ ? ch.opening.name.value
631
+ : undefined
632
+ if (chTag && allowedTags.has(chTag)) {
633
+ const isChSelfClosing = !!(ch.opening && (ch.opening as any).selfClosing)
634
+ if (isChSelfClosing) {
635
+ childrenLocalMap.set(ch, localIdxCounter++)
636
+ }
637
+ } else {
638
+ childrenLocalMap.set(ch, localIdxCounter++)
639
+ }
640
+ }
641
+ }
642
+
643
+ const idx = localIndexMap && localIndexMap.has(node) ? localIndexMap.get(node) : globalSlots.indexOf(node)
644
+ const inner = visitNodes(children, childrenLocalMap.size ? childrenLocalMap : undefined)
645
+ out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
646
+ lastWasSelfClosing = false
647
+ }
590
648
  }
591
649
  continue
592
650
  }