i18next-cli 1.33.4 → 1.34.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1102,6 +1102,7 @@ This programmatic API gives you the same power as the CLI but with full control
1102
1102
  ## Known plugins
1103
1103
 
1104
1104
  - [i18next-cli-plugin-svelte](https://github.com/dreamscached/i18next-cli-plugin-svelte) — a simple plugin to extract translation keys from Svelte components
1105
+ - [rsbuild-plugin-i18next-extractor](https://github.com/rspack-contrib/rsbuild-plugin-i18next-extractor) — A Rsbuild plugin that leverages the Rspack module graph to extract only the i18n translations that are actually imported and used in your code, preventing unused translations from being bundled.
1105
1106
 
1106
1107
  ---
1107
1108
 
package/dist/cjs/cli.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var e=require("commander"),o=require("chokidar"),t=require("glob"),n=require("minimatch"),i=require("chalk"),r=require("./config.js"),a=require("./heuristic-config.js"),c=require("./extractor/core/extractor.js");require("node:path"),require("node:fs/promises"),require("jiti"),require("@croct/json5-parser");var s=require("./types-generator.js"),l=require("./syncer.js"),d=require("./migrator.js"),u=require("./init.js"),p=require("./linter.js"),g=require("./status.js"),f=require("./locize.js"),y=require("./rename-key.js");const h=new e.Command;h.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.33.4"),h.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),h.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.").option("--sync-all","Sync primary language values with default values from code AND clear synced keys in all other locales.").action(async e=>{try{const t=h.opts().config,i=await r.ensureConfig(t),a=async()=>{const o=!!e.syncPrimary||!!e.syncAll,t=await c.runExtractor(i,{isWatchMode:!!e.watch,isDryRun:!!e.dryRun,syncPrimaryWithDefaults:o,syncAll:!!e.syncAll});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...");const e=await x(i.extract.input),t=m(i.extract.ignore),r=w(i.extract.output),c=[...t,...r].filter(Boolean),s=e.filter(e=>!c.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(s,{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)}}),h.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,o)=>{const t=h.opts().config;let n=await r.loadConfig(t);if(!n){console.log(i.blue("No config file found. Attempting to detect project structure..."));const e=await a.detectConfig();e||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),n=e}await g.runStatus(n,{detail:e,namespace:o.namespace})}),h.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 t=h.opts().config,i=await r.ensureConfig(t),a=()=>s.runTypesGenerator(i);if(await a(),e.watch){console.log("\nWatching for changes...");const e=await x(i.types?.input||[]),t=[...m(i.extract?.ignore)].filter(Boolean),r=e.filter(e=>!t.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(r,{persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}),h.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=h.opts().config,o=await r.ensureConfig(e);await l.runSyncer(o)}),h.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await d.runMigrator(e)}),h.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(u.runInit),h.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 t=h.opts().config,c=async()=>{let e=await r.loadConfig(t);if(!e){console.log(i.blue("No config file found. Attempting to detect project structure..."));const o=await a.detectConfig();o||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),e=o}await p.runLinterCli(e)};if(await c(),e.watch){console.log("\nWatching for changes...");const e=await r.loadConfig(t);if(e?.extract?.input){const t=await x(e.extract.input),i=[...m(e.extract.ignore),...w(e.extract.output)].filter(Boolean),r=t.filter(e=>!i.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(r,{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),c()})}}}),h.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.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async e=>{const o=h.opts().config,t=await r.ensureConfig(o);await f.runLocizeSync(t,e)}),h.command("locize-download").description("Download all translations from your locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async e=>{const o=h.opts().config,t=await r.ensureConfig(o);await f.runLocizeDownload(t,e)}),h.command("locize-migrate").description("Migrate local translation files to a new locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async e=>{const o=h.opts().config,t=await r.ensureConfig(o);await f.runLocizeMigrate(t,e)}),h.command("rename-key <oldKey> <newKey>").description("Rename a translation key across all source files and translation files.").option("--dry-run","Preview changes without modifying files").action(async(e,o,t)=>{try{const n=h.opts().config,a=await r.ensureConfig(n),c=await y.runRenameKey(a,e,o,t);c.success||(c.conflicts&&(console.error(i.red("\n❌ Conflicts detected:")),c.conflicts.forEach(e=>console.error(` - ${e}`))),c.error&&console.error(i.red(`\n❌ ${c.error}`)),process.exit(1));0===c.sourceFiles.reduce((e,o)=>e+o.changes,0)&&console.log(i.yellow(`\n⚠️ No usages found for "${e}"`))}catch(e){console.error(i.red("Error renaming key:"),e),process.exit(1)}}),h.parse(process.argv);const m=e=>Array.isArray(e)?e:e?[e]:[],w=e=>e&&"string"==typeof e?[e.replace(/\{\{[^}]+\}\}/g,"*")]:[],x=async(e=[])=>{const o=m(e),n=await Promise.all(o.map(e=>t.glob(e||"",{nodir:!0})));return Array.from(new Set(n.flat()))};exports.program=h;
2
+ "use strict";var e=require("commander"),o=require("chokidar"),t=require("glob"),n=require("minimatch"),i=require("chalk"),r=require("./config.js"),a=require("./heuristic-config.js"),c=require("./extractor/core/extractor.js");require("react"),require("react-i18next"),require("node:path"),require("node:fs/promises"),require("jiti"),require("@croct/json5-parser");var s=require("./types-generator.js"),l=require("./syncer.js"),u=require("./migrator.js"),d=require("./init.js"),p=require("./linter.js"),g=require("./status.js"),f=require("./locize.js"),y=require("./rename-key.js");const h=new e.Command;h.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.34.0"),h.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),h.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.").option("--sync-all","Sync primary language values with default values from code AND clear synced keys in all other locales.").action(async e=>{try{const t=h.opts().config,i=await r.ensureConfig(t),a=async()=>{const o=!!e.syncPrimary||!!e.syncAll,t=await c.runExtractor(i,{isWatchMode:!!e.watch,isDryRun:!!e.dryRun,syncPrimaryWithDefaults:o,syncAll:!!e.syncAll});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...");const e=await x(i.extract.input),t=m(i.extract.ignore),r=w(i.extract.output),c=[...t,...r].filter(Boolean),s=e.filter(e=>!c.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(s,{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)}}),h.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,o)=>{const t=h.opts().config;let n=await r.loadConfig(t);if(!n){console.log(i.blue("No config file found. Attempting to detect project structure..."));const e=await a.detectConfig();e||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),n=e}await g.runStatus(n,{detail:e,namespace:o.namespace})}),h.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 t=h.opts().config,i=await r.ensureConfig(t),a=()=>s.runTypesGenerator(i);if(await a(),e.watch){console.log("\nWatching for changes...");const e=await x(i.types?.input||[]),t=[...m(i.extract?.ignore)].filter(Boolean),r=e.filter(e=>!t.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(r,{persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}),h.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=h.opts().config,o=await r.ensureConfig(e);await l.runSyncer(o)}),h.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await u.runMigrator(e)}),h.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(d.runInit),h.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 t=h.opts().config,c=async()=>{let e=await r.loadConfig(t);if(!e){console.log(i.blue("No config file found. Attempting to detect project structure..."));const o=await a.detectConfig();o||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),e=o}await p.runLinterCli(e)};if(await c(),e.watch){console.log("\nWatching for changes...");const e=await r.loadConfig(t);if(e?.extract?.input){const t=await x(e.extract.input),i=[...m(e.extract.ignore),...w(e.extract.output)].filter(Boolean),r=t.filter(e=>!i.some(o=>n.minimatch(e,o,{dot:!0})));o.watch(r,{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),c()})}}}),h.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.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async e=>{const o=h.opts().config,t=await r.ensureConfig(o);await f.runLocizeSync(t,e)}),h.command("locize-download").description("Download all translations from your locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async e=>{const o=h.opts().config,t=await r.ensureConfig(o);await f.runLocizeDownload(t,e)}),h.command("locize-migrate").description("Migrate local translation files to a new locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async e=>{const o=h.opts().config,t=await r.ensureConfig(o);await f.runLocizeMigrate(t,e)}),h.command("rename-key <oldKey> <newKey>").description("Rename a translation key across all source files and translation files.").option("--dry-run","Preview changes without modifying files").action(async(e,o,t)=>{try{const n=h.opts().config,a=await r.ensureConfig(n),c=await y.runRenameKey(a,e,o,t);c.success||(c.conflicts&&(console.error(i.red("\n❌ Conflicts detected:")),c.conflicts.forEach(e=>console.error(` - ${e}`))),c.error&&console.error(i.red(`\n❌ ${c.error}`)),process.exit(1));0===c.sourceFiles.reduce((e,o)=>e+o.changes,0)&&console.log(i.yellow(`\n⚠️ No usages found for "${e}"`))}catch(e){console.error(i.red("Error renaming key:"),e),process.exit(1)}}),h.parse(process.argv);const m=e=>Array.isArray(e)?e:e?[e]:[],w=e=>e&&"string"==typeof e?[e.replace(/\{\{[^}]+\}\}/g,"*")]:[],x=async(e=[])=>{const o=m(e),n=await Promise.all(o.map(e=>t.glob(e||"",{nodir:!0})));return Array.from(new Set(n.flat()))};exports.program=h;
@@ -1 +1 @@
1
- "use strict";var e=require("./ast-utils.js");function t(t){if(t)return"StringLiteral"===t.type?t.value:"TemplateLiteral"===t.type&&e.isSimpleTemplateLiteral(t)?t.quasis[0].cooked:void 0}function n(t){if(t)return"StringLiteral"===t.type?t.value:"TemplateLiteral"===t.type&&e.isSimpleTemplateLiteral(t)?t.quasis?.[0]?.cooked:"ParenExpression"===t.type&&t.expr?n(t.expr):"ParenthesizedExpression"===t.type&&t.expression||"TsAsExpression"===t.type&&t.expression?n(t.expression):void 0}function i(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?t(e.value.expression):void 0}exports.extractFromTransComponent=function(r,s){const o=r.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),p=r.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),a=r.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),l=r.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let u;a||"JSXAttribute"!==l?.type||"JSXExpressionContainer"!==l.value?.type||"ObjectExpression"!==l.value.expression.type||(u=e.getObjectPropValueExpression(l.value.expression,"count"));const c=!!a||!!u,y=r.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),f="JSXAttribute"===y?.type&&"JSXExpressionContainer"===y.value?.type&&"ObjectExpression"===y.value.expression.type?y.value.expression:void 0,g=r.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),d=!!g,x=r.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===x?.type&&"JSXExpressionContainer"===x.value?.type?x.value.expression:"JSXAttribute"===x?.type&&"StringLiteral"===x.value?.type?x.value:void 0;const S=r.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let m;m="JSXAttribute"===S?.type?i(S):void 0,f&&(void 0===m&&(m=e.getObjectPropValue(f,"ns")),void 0===v&&(v=e.getObjectPropValueExpression(f,"context")));const h=function(e,i){if(!e||0===e.length)return"";const r=new Set(i.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),s=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n"),o=e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type),p=(e,t)=>{for(let n=t;n<e.length;n++){const t=e[n];if(t&&("JSXText"!==t.type||!s(t)))return t}},a=e=>{let t=e;for(;t;)if("ParenExpression"===t.type&&t.expr)t=t.expr;else if("ParenthesizedExpression"===t.type&&t.expression)t=t.expression;else{if("TsAsExpression"!==t.type||!t.expression)break;t=t.expression}return t};function l(e,i,u=!1,c=!1){if(!e||!e.length)return;const y=c&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let f=0,g=e.length-1;for(;f<=g&&s(e[f]);)f++;for(;g>=f&&s(e[g]);)g--;const d=f<=g?e.slice(f,g+1):[],x=d.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<d.length;e++){const f=d[e];if(f)if("JSXText"!==f.type){if("JSXExpressionContainer"===f.type){if(u&&!x&&f.expression){const e=a(f.expression),t=e?.type;if("ObjectExpression"===t)continue;if(void 0!==n(e))continue;if("Identifier"===t||"MemberExpression"===t||"CallExpression"===t)continue}const r=t(f.expression);if(void 0!==r){const t=/^\s*$/.test(r)&&!r.includes("\n"),n=d[e-1],a=d[e+1];if(t){const t=p(d,e+1);if(o(n)&&o(t));else{const t=d[e+2];if(a&&"JSXText"===a.type&&s(a)&&t&&("JSXElement"===t.type||"JSXFragment"===t.type)){const t=d[e-1],n=d[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&a&&"JSXText"===a.type&&s(a))continue;const r=!a||"JSXText"===a.type&&!s(a);if(n&&"JSXText"===n.type&&r){const e=i[i.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+f.expression.value;continue}}if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&a&&"JSXText"===a.type&&s(a))continue}}}i.push(f);continue}if("JSXElement"===f.type){const e=f.opening&&f.opening.name&&"Identifier"===f.opening.name.type?f.opening.name.value:void 0;if(e&&r.has(e)){const n=f.opening&&Array.isArray(f.opening.attributes)&&f.opening.attributes.length>0,o=f.children||[],p=1===o.length&&("JSXText"===o[0]?.type||"JSXExpressionContainer"===o[0]?.type&&void 0!==t(o[0].expression)),a=!o.length,u=p;if(n&&!p)i.push(f),l(f.children||[],i,!0);else if(a)i.push(f);else if(!u){if((f.children||[]).some(e=>"JSXExpressionContainer"===e.type&&void 0===t(e.expression)))i.push(f),l(f.children||[],i,!0);else if("p"===e){const e=(o||[]).some(e=>{if(!e||"JSXElement"!==e.type)return!1;const n=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0,i=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,s=e.children||[],o=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==t(s[0].expression));return!(n&&r.has(n)&&!i&&(0===s.length||o))}),n=c&&d.some(e=>e&&e!==f&&"JSXText"===e.type&&!s(e));y||c&&e&&!n?(i.push(f),l(f.children||[],i,!0,!1)):l(f.children||[],i,!1,!1)}else l(f.children||[],i,!1,!1)}continue}i.push(f),l(f.children||[],i,!0);continue}"JSXFragment"!==f.type||l(f.children||[],i,u)}else{if(u&&!x)continue;if(u&&s(f))continue;if(s(f)){const t=d[e-1],n=d[e+1];if(t&&("JSXElement"===t.type||"JSXFragment"===t.type)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type))continue;const r=i[i.length-1],s=d[e-1];if(r){if(s&&"JSXExpressionContainer"===s.type)continue;if("JSXText"===r.type&&s&&"JSXText"===s.type){r.value=String(r.value)+f.value;continue}}}if(u&&x&&0===e)continue;i.push(f)}}}const u=[];function c(e){const n=new Map;if(!e||!e.length)return n;const i=e=>e&&"JSXText"===e.type&&!s(e)&&String(e.value).length>0,o=e=>{if(!e||"JSXExpressionContainer"!==e.type)return!1;const n=e.expression;if(!n||"JSXEmptyExpression"===n.type)return!1;const i=t(n);return void 0===i||(!/^\s*$/.test(i)||!i.includes("\n"))},p=e=>"Identifier"===e?.opening?.name?.type?e.opening.name.value:void 0,a=e=>{const n=e?.children||[];return 1===n.length&&("JSXText"===n[0]?.type||"JSXExpressionContainer"===n[0]?.type&&void 0!==t(n[0].expression))},l=e=>{const t=p(e);if(!t||!r.has(t))return!0;if("p"===t)return!0;const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0;return!!((e.children||[]).length>0)&&(!a(e)&&(!!n||-1!==u.indexOf(e)))},c=e=>{const t=p(e);if(!t||!r.has(t))return!1;if(0!==(e.children||[]).length)return!1;const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0;return"br"===t&&!n};let y=0;for(const t of e)t&&(i(t)||o(t)?y+=1:"JSXElement"!==t.type||(l(t)?(n.set(t,y),y+=1):c(t)&&(y+=1)));return n}l(e,u,!1,!0);const y=new Set,f=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function g(e,i,o=!1){if(!e||0===e.length)return"";let p="";const a=e=>{if(!e)return-1;if(i&&i.has(e))return i.get(e);if(i)for(const[t,n]of i.entries())try{if(t&&e&&t.span&&e.span&&t.span.start===e.span.start&&t.span.end===e.span.end)return n}catch(e){}return u.indexOf(e)},l=t=>{if(!o)return u.indexOf(t);const n=e.indexOf(t);if(-1===n)return-1;let i=0;for(let t=0;t<n;t++){const n=e[t];n&&("JSXText"===n.type?s(n)||i++:"JSXExpressionContainer"===n.type?n.expression&&"JSXEmptyExpression"!==n.expression.type&&i++:"JSXElement"===n.type&&i++)}return i};for(let i=0;i<e.length;i++){const d=e[i];if(d){if("JSXText"===d.type){if(s(d))continue;const t=e[i+1],n=e[i-1];if(n&&"JSXElement"===n.type){const t="Identifier"===n.opening?.name?.type?n.opening.name.value:void 0,s=t&&r.has(t),o=0===(n.children||[]).length;if(s&&o&&/^\s*\n\s*/.test(d.value)){const e=d.value.replace(/^\s*\n\s*/,"");if(e){p+=e;continue}continue}if(!s&&/^\s*\n\s*/.test(d.value)){const t=d.value.replace(/^\s*\n\s*/,"");if(t){if(n&&"JSXElement"===n.type&&Array.isArray(n.children)&&0===n.children.length){p+=t;continue}const r=e[i-2];if(r&&"JSXText"===r.type){const e=r.value.replace(/\n\s*$/,""),n=/[A-Za-z0-9]$/.test(e),i=/^[A-Za-z0-9]/.test(t),s=/^[a-z]/.test(t);if(n&&i&&s){p+=t;continue}}p+=" "+t;continue}continue}}if(/\n\s*$/.test(d.value)&&t&&"JSXElement"===t.type){const n=d.value.replace(/\n\s*$/,"");if(n.trim()){const o="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,a=o&&r.has(o),l=(t.children||[]).length>0,u=/\s\n/.test(d.value),c=e[i+2],f=c&&"JSXText"===c.type&&!s(c)&&/[a-zA-Z0-9]/.test(c.value),g=!!(t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0),x=/^\s/.test(n)&&!/^\n/.test(n),v=n.trim(),S=x?" "+v:v,m=/[A-Za-z0-9]$/.test(v),h=c&&"string"==typeof c.value&&/^[A-Za-z0-9]/.test(c.value.trim()),J=c&&"string"==typeof c.value&&/^[a-z]/.test(c.value.trim()),X=c&&"string"==typeof c.value&&/^\s/.test(c.value)&&!/^\n/.test(c.value),E=g&&l&&f&&!(m&&h&&J&&!u&&!x&&!X),$=c&&"string"==typeof c.value&&/^[,;:!?.]/.test(c.value.trim()),b=E&&!$;m&&h&&J&&!u&&!x&&!X&&y.add(t),p+=u||a&&l||!a&&f&&x||b?S+" ":S;continue}}p+=d.value;continue}if("JSXExpressionContainer"===d.type){const e=d.expression;if(!e)continue;const i=t(e);if(void 0!==i)p+=i;else if("Identifier"===e.type)p+=`{{${e.value}}}`;else if("TsAsExpression"===e.type&&"ObjectExpression"===e.expression?.type){const t=e.expression,n=t.properties.filter(e=>"KeyValueProperty"===e.type&&e.key&&"Identifier"===e.key.type).map(e=>e.key.value);if(n.length>0)p+=n.map(e=>`{{${e}}}`).join("");else{const e=t.properties[0];if(!e||"Identifier"!==e.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${e.value}}}`}}else if("ObjectExpression"===e.type){const t=e.properties[0];if(t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type)p+=`{{${t.key.value}}}`;else{if(!t||"Identifier"!==t.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${t.value}}}`}}else if("MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type)p+=`{{${e.property.value}}}`;else if("CallExpression"===e.type&&"Identifier"===e.callee?.type)p+=`{{${e.callee.value}}}`;else if("ConditionalExpression"===e.type){const t=n(e.consequent),i=n(e.alternate);p+=void 0!==t&&void 0!==i?t.length>=i.length?t:i:void 0!==t?t:void 0!==i?i:""}else if("JSXEmptyExpression"!==e.type)throw new Error(`Unrecognized expression in JSX placeholder: ${e.type}`);continue}if("JSXElement"===d.type){const n=o?l(d):void 0;let s;if(d.opening&&d.opening.name&&"Identifier"===d.opening.name.type&&(s=d.opening.name.value),s&&r.has(s)){const l=d.opening&&Array.isArray(d.opening.attributes)&&d.opening.attributes.length>0,y=d.children||[],x=y.length>0,v=1===y.length&&("JSXText"===y[0]?.type||"JSXExpressionContainer"===y[0]?.type&&void 0!==t(y[0].expression)),S="p"===s;let m=0;if(S&&o&&(m=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!x||v){const t=v?g(y,void 0):"";if(""!==String(t).trim())if(r.has(s)&&"p"!==s)p+=`<${s}>${f(t)}</${s}>`;else if(S&&o&&m>1)p+=`<${s}>${f(t)}</${s}>`;else if(S){const e=o&&void 0!==n?n:u.indexOf(d);p+=`<${e}>${f(t)}</${e}>`}else{const e=o&&void 0!==n?n:u.indexOf(d);p+=`<${e}>${f(t)}</${e}>`}else{const t=e[i-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(p=p.replace(/\s+$/,"")),p+=`<${s} />`}}else if(l&&!v){const e=o&&void 0!==n?n:a(d),t=c(y),i=g(y,t.size?t:void 0);p+=`<${e}>${f(i)}</${e}>`}else{const e=u.indexOf(d);if(-1!==e){const t=void 0!==n?n:e,i=c(y),r=g(y,i.size?i:void 0,!1);p+=`<${t}>${f(r)}</${t}>`}else{const e=g(y,void 0,!1);p+=`<${s}>${f(e)}</${s}>`}}}else{const e=d.children||[],t=a(d),n=c(e),i=g(e,n.size?n:void 0);p+=`<${t}>${f(i)}</${t}>`}continue}"JSXFragment"!==d.type||(p+=g(d.children||[]))}}return p}const d=g(e,void 0,!0),x=String(d).replace(/<br \/>\s*\n\s*/g,"<br />"),v=String(x),S=new Set;if(y&&y.size>0)for(let e=0;e<u.length;e++)y.has(u[e])&&S.add(e);let m=String(v);if(S&&S.size>0)for(const e of S)try{m=m.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),m=m.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}m=m.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return S.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),m=m.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return S.has(i)?`${t}<${n}`:`${t} <${n}`}),m=m.replace(/\s*\n\s*/g," "),m=m.replace(/\s+/g," "),m=m.replace(/\s+([,;:!?.])/g,"$1");const h=m.trim();let J=String(h);if(S&&S.size>0)for(const e of S)try{J=J.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),J=J.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<u.length;e++){const t=u[e];if(!t||"JSXElement"!==t.type)continue;const n=u[e-1],i=u[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const r=String(n.value),s=String(i.value),o=r.replace(/\n\s*$/,""),p=/[A-Za-z0-9]$/.test(o),a=/^[A-Za-z0-9]/.test(s.trim()),l=/^[a-z]/.test(s.trim()),c=/\s\n/.test(r),y=s&&/^\s/.test(s)&&!/^\n/.test(s);if(p&&a&&l&&!c&&!y){const t=e;J=J.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}return J.trim()}(r.children,s);let J;const X="JSXAttribute"===p?.type?i(p):void 0;if(void 0!==X)J=X;else{const e=s.extract.defaultValue;J="string"==typeof e?e:""}let E,$;if("JSXAttribute"===o?.type){if("StringLiteral"===o.value?.type){if(E=o.value,$=E.value,!$||""===$.trim())return null;if(m&&"StringLiteral"===E.type){const e=s.extract.nsSeparator??":",t=E.value;if(e&&t.startsWith(`${m}${e}`)){if($=t.slice(`${m}${e}`.length),!$||""===$.trim())return null;E={...E,value:$}}}}else"JSXExpressionContainer"===o.value?.type&&"JSXEmptyExpression"!==o.value.expression.type&&(E=o.value.expression);if(!E)return null}return p||!$||h.trim()?!p&&h.trim()&&(J=h):J=$,{keyExpression:E,serializedChildren:h,ns:m,defaultValue:J,hasCount:c,isOrdinal:d,contextExpression:v,optionsNode:f,explicitDefault:void 0!==X||(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})(f)}};
1
+ "use strict";var e=require("./ast-utils.js"),t=require("react"),r=require("react-i18next");function n(e){var t=Object.create(null);return e&&Object.keys(e).forEach(function(r){if("default"!==r){var n=Object.getOwnPropertyDescriptor(e,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:function(){return e[r]}})}}),t.default=e,Object.freeze(t)}var i=n(t);function a(t){return"StringLiteral"===t.value?.type?t.value.value:"JSXExpressionContainer"===t.value?.type?function(t){if(t)return"StringLiteral"===t.type?t.value:"TemplateLiteral"===t.type&&e.isSimpleTemplateLiteral(t)?t.quasis[0].cooked:void 0}(t.value.expression):void 0}function s(e){const t=()=>null;return Object.defineProperty(t,"name",{value:e}),t.displayName=e,t}function u(t){switch(t.type){case"JSXEmptyExpression":return null;case"TsAsExpression":case"ParenthesisExpression":return u(t.expression);case"ConditionalExpression":{const e=u(t.consequent),r=u(t.alternate);return"string"==typeof e&&"string"==typeof r&&r.length!==e.length&&r.startsWith(e)?r:e}case"StringLiteral":return t.value;case"TemplateLiteral":if(e.isSimpleTemplateLiteral(t))return t.quasis[0].raw;break;case"Identifier":return{[t.value]:t.value};case"ObjectExpression":{const e=t.properties.map(e=>"KeyValueProperty"!==e.type||"Identifier"!==e.key.type&&"StringLiteral"!==e.key.type?"Identifier"===e.type?e.value:null:e.key.value).filter(e=>null!==e);return Object.fromEntries(e.map(e=>[e,e]))}}return i.createElement("expression",{expression:t})}function l(e){switch(e.type){case"JSXText":return r=(r=e.value).replace(/\r\n/g,"\n"),/^\s+$/.test(r)&&/\n/.test(r)?null:r=(r=(r=r.replace(/^[ \t]*\n[ \t]*/,"")).replace(/[ \t]*\n[ \t]*$/,"")).replace(/[ \t]*\n[ \t]*/g," ");case"JSXExpressionContainer":return u(e.expression);case"JSXSpreadChild":return"";case"JSXElement":return i.createElement(function(e){switch(e.type){case"Identifier":return/\p{Uppercase_Letter}/u.test(e.value)?s(e.value):e.value;case"JSXMemberExpression":return s(e.property.value);case"JSXNamespacedName":return`${e.namespace.value}:${e.name.value}`}}(e.opening.name),(t=e.opening.attributes).length?Object.fromEntries(t.map(e=>"SpreadElement"===e.type?null:"Identifier"===e.name.type?[e.name.value,""]:[`${e.name.namespace.value}:${e.name.name.value}`,""]).filter(e=>null!=e)):null,...p(e.children));case"JSXFragment":return i.createElement(i.Fragment,null,...p(e.children))}var t,r}function p(e){return e.map(l).filter(e=>null!==e)}exports.extractFromTransComponent=function(t,n){const i=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),s=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),u=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),l=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let o;u||"JSXAttribute"!==l?.type||"JSXExpressionContainer"!==l.value?.type||"ObjectExpression"!==l.value.expression.type||(o=e.getObjectPropValueExpression(l.value.expression,"count"));const c=!!u||!!o,y=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),f="JSXAttribute"===y?.type&&"JSXExpressionContainer"===y.value?.type&&"ObjectExpression"===y.value.expression.type?y.value.expression:void 0,v=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),m=!!v,d=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let x="JSXAttribute"===d?.type&&"JSXExpressionContainer"===d.value?.type?d.value.expression:"JSXAttribute"===d?.type&&"StringLiteral"===d.value?.type?d.value:void 0;const S=t.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let b;b="JSXAttribute"===S?.type?a(S):void 0,f&&(void 0===b&&(b=e.getObjectPropValue(f,"ns")),void 0===x&&(x=e.getObjectPropValueExpression(f,"context")));const g=function(e,t){const n={...r.getDefaults()};t.extract.transKeepBasicHtmlNodesFor&&(n.transKeepBasicHtmlNodesFor=t.extract.transKeepBasicHtmlNodesFor);return r.nodesToString(p(e),n)}(t.children,n);let E;const J="JSXAttribute"===s?.type?a(s):void 0;if(void 0!==J)E=J;else{const e=n.extract.defaultValue;E="string"==typeof e?e:""}let X,h;if("JSXAttribute"===i?.type){if("StringLiteral"===i.value?.type){if(X=i.value,h=X.value,!h||""===h.trim())return null;if(b&&"StringLiteral"===X.type){const e=n.extract.nsSeparator??":",t=X.value;if(e&&t.startsWith(`${b}${e}`)){if(h=t.slice(`${b}${e}`.length),!h||""===h.trim())return null;X={...X,value:h}}}}else"JSXExpressionContainer"===i.value?.type&&"JSXEmptyExpression"!==i.value.expression.type&&(X=i.value.expression);if(!X)return null}return s||!h||g.trim()?!s&&g.trim()&&(E=g):E=h,{keyExpression:X,serializedChildren:g,ns:b,defaultValue:E,hasCount:c,isOrdinal:m,contextExpression:x,optionsNode:f,explicitDefault:void 0!==J||(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})(f)}};
package/dist/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var e=require("./config.js"),r=require("./extractor/core/extractor.js"),t=require("./extractor/core/key-finder.js"),n=require("./extractor/core/translation-manager.js"),s=require("./linter.js"),o=require("./syncer.js"),a=require("./status.js"),c=require("./types-generator.js"),u=require("./rename-key.js");exports.defineConfig=e.defineConfig,exports.extract=r.extract,exports.runExtractor=r.runExtractor,exports.findKeys=t.findKeys,exports.getTranslations=n.getTranslations,exports.recommendedAcceptedAttributes=s.recommendedAcceptedAttributes,exports.recommendedAcceptedTags=s.recommendedAcceptedTags,exports.runLinter=s.runLinter,exports.runSyncer=o.runSyncer,exports.runStatus=a.runStatus,exports.runTypesGenerator=c.runTypesGenerator,exports.runRenameKey=u.runRenameKey;
1
+ "use strict";var e=require("./config.js"),r=require("./extractor/core/extractor.js"),t=require("./extractor/core/key-finder.js"),n=require("./extractor/core/translation-manager.js");require("react"),require("react-i18next");var s=require("./linter.js"),o=require("./syncer.js"),a=require("./status.js"),c=require("./types-generator.js"),i=require("./rename-key.js");exports.defineConfig=e.defineConfig,exports.extract=r.extract,exports.runExtractor=r.runExtractor,exports.findKeys=t.findKeys,exports.getTranslations=n.getTranslations,exports.recommendedAcceptedAttributes=s.recommendedAcceptedAttributes,exports.recommendedAcceptedTags=s.recommendedAcceptedTags,exports.runLinter=s.runLinter,exports.runSyncer=o.runSyncer,exports.runStatus=a.runStatus,exports.runTypesGenerator=c.runTypesGenerator,exports.runRenameKey=i.runRenameKey;
package/dist/esm/cli.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import{Command as o}from"commander";import e from"chokidar";import{glob as t}from"glob";import{minimatch as n}from"minimatch";import i from"chalk";import{ensureConfig as r,loadConfig as a}from"./config.js";import{detectConfig as c}from"./heuristic-config.js";import{runExtractor as s}from"./extractor/core/extractor.js";import"node:path";import"node:fs/promises";import"jiti";import"@croct/json5-parser";import{runTypesGenerator as l}from"./types-generator.js";import{runSyncer as p}from"./syncer.js";import{runMigrator as d}from"./migrator.js";import{runInit as f}from"./init.js";import{runLinterCli as m}from"./linter.js";import{runStatus as u}from"./status.js";import{runLocizeSync as y,runLocizeDownload as g,runLocizeMigrate as h}from"./locize.js";import{runRenameKey as w}from"./rename-key.js";const x=new o;x.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.33.4"),x.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),x.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.").option("--sync-all","Sync primary language values with default values from code AND clear synced keys in all other locales.").action(async o=>{try{const t=x.opts().config,i=await r(t),a=async()=>{const e=!!o.syncPrimary||!!o.syncAll,t=await s(i,{isWatchMode:!!o.watch,isDryRun:!!o.dryRun,syncPrimaryWithDefaults:e,syncAll:!!o.syncAll});return o.ci&&!t?(console.log("✅ No files were updated."),process.exit(0)):o.ci&&t&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),t};if(await a(),o.watch){console.log("\nWatching for changes...");const o=await z(i.extract.input),t=j(i.extract.ignore),r=v(i.extract.output),c=[...t,...r].filter(Boolean),s=o.filter(o=>!c.some(e=>n(o,e,{dot:!0})));e.watch(s,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),a()})}}catch(o){console.error("Error running extractor:",o),process.exit(1)}}),x.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(o,e)=>{const t=x.opts().config;let n=await a(t);if(!n){console.log(i.blue("No config file found. Attempting to detect project structure..."));const o=await c();o||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),n=o}await u(n,{detail:o,namespace:e.namespace})}),x.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 o=>{const t=x.opts().config,i=await r(t),a=()=>l(i);if(await a(),o.watch){console.log("\nWatching for changes...");const o=await z(i.types?.input||[]),t=[...j(i.extract?.ignore)].filter(Boolean),r=o.filter(o=>!t.some(e=>n(o,e,{dot:!0})));e.watch(r,{persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),a()})}}),x.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const o=x.opts().config,e=await r(o);await p(e)}),x.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async o=>{await d(o)}),x.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(f),x.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 o=>{const t=x.opts().config,r=async()=>{let o=await a(t);if(!o){console.log(i.blue("No config file found. Attempting to detect project structure..."));const e=await c();e||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),o=e}await m(o)};if(await r(),o.watch){console.log("\nWatching for changes...");const o=await a(t);if(o?.extract?.input){const t=await z(o.extract.input),i=[...j(o.extract.ignore),...v(o.extract.output)].filter(Boolean),a=t.filter(o=>!i.some(e=>n(o,e,{dot:!0})));e.watch(a,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),r()})}}}),x.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.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async o=>{const e=x.opts().config,t=await r(e);await y(t,o)}),x.command("locize-download").description("Download all translations from your locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async o=>{const e=x.opts().config,t=await r(e);await g(t,o)}),x.command("locize-migrate").description("Migrate local translation files to a new locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async o=>{const e=x.opts().config,t=await r(e);await h(t,o)}),x.command("rename-key <oldKey> <newKey>").description("Rename a translation key across all source files and translation files.").option("--dry-run","Preview changes without modifying files").action(async(o,e,t)=>{try{const n=x.opts().config,a=await r(n),c=await w(a,o,e,t);c.success||(c.conflicts&&(console.error(i.red("\n❌ Conflicts detected:")),c.conflicts.forEach(o=>console.error(` - ${o}`))),c.error&&console.error(i.red(`\n❌ ${c.error}`)),process.exit(1));0===c.sourceFiles.reduce((o,e)=>o+e.changes,0)&&console.log(i.yellow(`\n⚠️ No usages found for "${o}"`))}catch(o){console.error(i.red("Error renaming key:"),o),process.exit(1)}}),x.parse(process.argv);const j=o=>Array.isArray(o)?o:o?[o]:[],v=o=>o&&"string"==typeof o?[o.replace(/\{\{[^}]+\}\}/g,"*")]:[],z=async(o=[])=>{const e=j(o),n=await Promise.all(e.map(o=>t(o||"",{nodir:!0})));return Array.from(new Set(n.flat()))};export{x as program};
2
+ import{Command as o}from"commander";import e from"chokidar";import{glob as t}from"glob";import{minimatch as n}from"minimatch";import i from"chalk";import{ensureConfig as r,loadConfig as a}from"./config.js";import{detectConfig as c}from"./heuristic-config.js";import{runExtractor as s}from"./extractor/core/extractor.js";import"react";import"react-i18next";import"node:path";import"node:fs/promises";import"jiti";import"@croct/json5-parser";import{runTypesGenerator as l}from"./types-generator.js";import{runSyncer as p}from"./syncer.js";import{runMigrator as d}from"./migrator.js";import{runInit as f}from"./init.js";import{runLinterCli as m}from"./linter.js";import{runStatus as u}from"./status.js";import{runLocizeSync as y,runLocizeDownload as g,runLocizeMigrate as h}from"./locize.js";import{runRenameKey as w}from"./rename-key.js";const x=new o;x.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.34.0"),x.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),x.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.").option("--sync-all","Sync primary language values with default values from code AND clear synced keys in all other locales.").action(async o=>{try{const t=x.opts().config,i=await r(t),a=async()=>{const e=!!o.syncPrimary||!!o.syncAll,t=await s(i,{isWatchMode:!!o.watch,isDryRun:!!o.dryRun,syncPrimaryWithDefaults:e,syncAll:!!o.syncAll});return o.ci&&!t?(console.log("✅ No files were updated."),process.exit(0)):o.ci&&t&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),t};if(await a(),o.watch){console.log("\nWatching for changes...");const o=await z(i.extract.input),t=j(i.extract.ignore),r=v(i.extract.output),c=[...t,...r].filter(Boolean),s=o.filter(o=>!c.some(e=>n(o,e,{dot:!0})));e.watch(s,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),a()})}}catch(o){console.error("Error running extractor:",o),process.exit(1)}}),x.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(o,e)=>{const t=x.opts().config;let n=await a(t);if(!n){console.log(i.blue("No config file found. Attempting to detect project structure..."));const o=await c();o||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),n=o}await u(n,{detail:o,namespace:e.namespace})}),x.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 o=>{const t=x.opts().config,i=await r(t),a=()=>l(i);if(await a(),o.watch){console.log("\nWatching for changes...");const o=await z(i.types?.input||[]),t=[...j(i.extract?.ignore)].filter(Boolean),r=o.filter(o=>!t.some(e=>n(o,e,{dot:!0})));e.watch(r,{persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),a()})}}),x.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const o=x.opts().config,e=await r(o);await p(e)}),x.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async o=>{await d(o)}),x.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(f),x.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 o=>{const t=x.opts().config,r=async()=>{let o=await a(t);if(!o){console.log(i.blue("No config file found. Attempting to detect project structure..."));const e=await c();e||(console.error(i.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${i.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(i.green("Project structure detected successfully!")),o=e}await m(o)};if(await r(),o.watch){console.log("\nWatching for changes...");const o=await a(t);if(o?.extract?.input){const t=await z(o.extract.input),i=[...j(o.extract.ignore),...v(o.extract.output)].filter(Boolean),a=t.filter(o=>!i.some(e=>n(o,e,{dot:!0})));e.watch(a,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),r()})}}}),x.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.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async o=>{const e=x.opts().config,t=await r(e);await y(t,o)}),x.command("locize-download").description("Download all translations from your locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async o=>{const e=x.opts().config,t=await r(e);await g(t,o)}),x.command("locize-migrate").description("Migrate local translation files to a new locize project.").option("--cdn-type <standard|pro>","Specify the cdn endpoint that should be used (depends on which cdn type you've in your locize project)").action(async o=>{const e=x.opts().config,t=await r(e);await h(t,o)}),x.command("rename-key <oldKey> <newKey>").description("Rename a translation key across all source files and translation files.").option("--dry-run","Preview changes without modifying files").action(async(o,e,t)=>{try{const n=x.opts().config,a=await r(n),c=await w(a,o,e,t);c.success||(c.conflicts&&(console.error(i.red("\n❌ Conflicts detected:")),c.conflicts.forEach(o=>console.error(` - ${o}`))),c.error&&console.error(i.red(`\n❌ ${c.error}`)),process.exit(1));0===c.sourceFiles.reduce((o,e)=>o+e.changes,0)&&console.log(i.yellow(`\n⚠️ No usages found for "${o}"`))}catch(o){console.error(i.red("Error renaming key:"),o),process.exit(1)}}),x.parse(process.argv);const j=o=>Array.isArray(o)?o:o?[o]:[],v=o=>o&&"string"==typeof o?[o.replace(/\{\{[^}]+\}\}/g,"*")]:[],z=async(o=[])=>{const e=j(o),n=await Promise.all(e.map(o=>t(o||"",{nodir:!0})));return Array.from(new Set(n.flat()))};export{x as program};
@@ -1 +1 @@
1
- import{getObjectPropValueExpression as e,getObjectPropValue as t,isSimpleTemplateLiteral as n}from"./ast-utils.js";function i(e){if(e)return"StringLiteral"===e.type?e.value:"TemplateLiteral"===e.type&&n(e)?e.quasis[0].cooked:void 0}function r(e){if(e)return"StringLiteral"===e.type?e.value:"TemplateLiteral"===e.type&&n(e)?e.quasis?.[0]?.cooked:"ParenExpression"===e.type&&e.expr?r(e.expr):"ParenthesizedExpression"===e.type&&e.expression||"TsAsExpression"===e.type&&e.expression?r(e.expression):void 0}function s(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?i(e.value.expression):void 0}function o(n,o){const p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),a=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),l=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),u=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let c;l||"JSXAttribute"!==u?.type||"JSXExpressionContainer"!==u.value?.type||"ObjectExpression"!==u.value.expression.type||(c=e(u.value.expression,"count"));const y=!!l||!!c,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),g="JSXAttribute"===f?.type&&"JSXExpressionContainer"===f.value?.type&&"ObjectExpression"===f.value.expression.type?f.value.expression:void 0,d=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),v=!!d,x=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===x?.type&&"JSXExpressionContainer"===x.value?.type?x.value.expression:"JSXAttribute"===x?.type&&"StringLiteral"===x.value?.type?x.value:void 0;const h=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let m;m="JSXAttribute"===h?.type?s(h):void 0,g&&(void 0===m&&(m=t(g,"ns")),void 0===S&&(S=e(g,"context")));const J=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),s=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n"),o=e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type),p=(e,t)=>{for(let n=t;n<e.length;n++){const t=e[n];if(t&&("JSXText"!==t.type||!s(t)))return t}},a=e=>{let t=e;for(;t;)if("ParenExpression"===t.type&&t.expr)t=t.expr;else if("ParenthesizedExpression"===t.type&&t.expression)t=t.expression;else{if("TsAsExpression"!==t.type||!t.expression)break;t=t.expression}return t};function l(e,t,u=!1,c=!1){if(!e||!e.length)return;const y=c&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let f=0,g=e.length-1;for(;f<=g&&s(e[f]);)f++;for(;g>=f&&s(e[g]);)g--;const d=f<=g?e.slice(f,g+1):[],v=d.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<d.length;e++){const f=d[e];if(f)if("JSXText"!==f.type){if("JSXExpressionContainer"===f.type){if(u&&!v&&f.expression){const e=a(f.expression),t=e?.type;if("ObjectExpression"===t)continue;if(void 0!==r(e))continue;if("Identifier"===t||"MemberExpression"===t||"CallExpression"===t)continue}const n=i(f.expression);if(void 0!==n){const i=/^\s*$/.test(n)&&!n.includes("\n"),r=d[e-1],a=d[e+1];if(i){const n=p(d,e+1);if(o(r)&&o(n));else{const n=d[e+2];if(a&&"JSXText"===a.type&&s(a)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=d[e-1],n=d[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&a&&"JSXText"===a.type&&s(a))continue;const i=!a||"JSXText"===a.type&&!s(a);if(r&&"JSXText"===r.type&&i){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+f.expression.value;continue}}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&a&&"JSXText"===a.type&&s(a))continue}}}t.push(f);continue}if("JSXElement"===f.type){const e=f.opening&&f.opening.name&&"Identifier"===f.opening.name.type?f.opening.name.value:void 0;if(e&&n.has(e)){const r=f.opening&&Array.isArray(f.opening.attributes)&&f.opening.attributes.length>0,o=f.children||[],p=1===o.length&&("JSXText"===o[0]?.type||"JSXExpressionContainer"===o[0]?.type&&void 0!==i(o[0].expression)),a=!o.length,u=p;if(r&&!p)t.push(f),l(f.children||[],t,!0);else if(a)t.push(f);else if(!u){if((f.children||[]).some(e=>"JSXExpressionContainer"===e.type&&void 0===i(e.expression)))t.push(f),l(f.children||[],t,!0);else if("p"===e){const e=(o||[]).some(e=>{if(!e||"JSXElement"!==e.type)return!1;const t=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0,r=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,s=e.children||[],o=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==i(s[0].expression));return!(t&&n.has(t)&&!r&&(0===s.length||o))}),r=c&&d.some(e=>e&&e!==f&&"JSXText"===e.type&&!s(e));y||c&&e&&!r?(t.push(f),l(f.children||[],t,!0,!1)):l(f.children||[],t,!1,!1)}else l(f.children||[],t,!1,!1)}continue}t.push(f),l(f.children||[],t,!0);continue}"JSXFragment"!==f.type||l(f.children||[],t,u)}else{if(u&&!v)continue;if(u&&s(f))continue;if(s(f)){const n=d[e-1],i=d[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=d[e-1];if(r){if(s&&"JSXExpressionContainer"===s.type)continue;if("JSXText"===r.type&&s&&"JSXText"===s.type){r.value=String(r.value)+f.value;continue}}}if(u&&v&&0===e)continue;t.push(f)}}}const u=[];function c(e){const t=new Map;if(!e||!e.length)return t;const r=e=>e&&"JSXText"===e.type&&!s(e)&&String(e.value).length>0,o=e=>{if(!e||"JSXExpressionContainer"!==e.type)return!1;const t=e.expression;if(!t||"JSXEmptyExpression"===t.type)return!1;const n=i(t);return void 0===n||(!/^\s*$/.test(n)||!n.includes("\n"))},p=e=>"Identifier"===e?.opening?.name?.type?e.opening.name.value:void 0,a=e=>{const t=e?.children||[];return 1===t.length&&("JSXText"===t[0]?.type||"JSXExpressionContainer"===t[0]?.type&&void 0!==i(t[0].expression))},l=e=>{const t=p(e);if(!t||!n.has(t))return!0;if("p"===t)return!0;const i=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0;return!!((e.children||[]).length>0)&&(!a(e)&&(!!i||-1!==u.indexOf(e)))},c=e=>{const t=p(e);if(!t||!n.has(t))return!1;if(0!==(e.children||[]).length)return!1;const i=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0;return"br"===t&&!i};let y=0;for(const n of e)n&&(r(n)||o(n)?y+=1:"JSXElement"!==n.type||(l(n)?(t.set(n,y),y+=1):c(n)&&(y+=1)));return t}l(e,u,!1,!0);const y=new Set,f=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function g(e,t,o=!1){if(!e||0===e.length)return"";let p="";const a=e=>{if(!e)return-1;if(t&&t.has(e))return t.get(e);if(t)for(const[n,i]of t.entries())try{if(n&&e&&n.span&&e.span&&n.span.start===e.span.start&&n.span.end===e.span.end)return i}catch(e){}return u.indexOf(e)},l=t=>{if(!o)return u.indexOf(t);const n=e.indexOf(t);if(-1===n)return-1;let i=0;for(let t=0;t<n;t++){const n=e[t];n&&("JSXText"===n.type?s(n)||i++:"JSXExpressionContainer"===n.type?n.expression&&"JSXEmptyExpression"!==n.expression.type&&i++:"JSXElement"===n.type&&i++)}return i};for(let t=0;t<e.length;t++){const d=e[t];if(d){if("JSXText"===d.type){if(s(d))continue;const i=e[t+1],r=e[t-1];if(r&&"JSXElement"===r.type){const i="Identifier"===r.opening?.name?.type?r.opening.name.value:void 0,s=i&&n.has(i),o=0===(r.children||[]).length;if(s&&o&&/^\s*\n\s*/.test(d.value)){const e=d.value.replace(/^\s*\n\s*/,"");if(e){p+=e;continue}continue}if(!s&&/^\s*\n\s*/.test(d.value)){const n=d.value.replace(/^\s*\n\s*/,"");if(n){if(r&&"JSXElement"===r.type&&Array.isArray(r.children)&&0===r.children.length){p+=n;continue}const i=e[t-2];if(i&&"JSXText"===i.type){const e=i.value.replace(/\n\s*$/,""),t=/[A-Za-z0-9]$/.test(e),r=/^[A-Za-z0-9]/.test(n),s=/^[a-z]/.test(n);if(t&&r&&s){p+=n;continue}}p+=" "+n;continue}continue}}if(/\n\s*$/.test(d.value)&&i&&"JSXElement"===i.type){const r=d.value.replace(/\n\s*$/,"");if(r.trim()){const o="Identifier"===i.opening?.name?.type?i.opening.name.value:void 0,a=o&&n.has(o),l=(i.children||[]).length>0,u=/\s\n/.test(d.value),c=e[t+2],f=c&&"JSXText"===c.type&&!s(c)&&/[a-zA-Z0-9]/.test(c.value),g=!!(i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0),v=/^\s/.test(r)&&!/^\n/.test(r),x=r.trim(),S=v?" "+x:x,h=/[A-Za-z0-9]$/.test(x),m=c&&"string"==typeof c.value&&/^[A-Za-z0-9]/.test(c.value.trim()),J=c&&"string"==typeof c.value&&/^[a-z]/.test(c.value.trim()),X=c&&"string"==typeof c.value&&/^\s/.test(c.value)&&!/^\n/.test(c.value),E=g&&l&&f&&!(h&&m&&J&&!u&&!v&&!X),$=c&&"string"==typeof c.value&&/^[,;:!?.]/.test(c.value.trim()),b=E&&!$;h&&m&&J&&!u&&!v&&!X&&y.add(i),p+=u||a&&l||!a&&f&&v||b?S+" ":S;continue}}p+=d.value;continue}if("JSXExpressionContainer"===d.type){const e=d.expression;if(!e)continue;const t=i(e);if(void 0!==t)p+=t;else if("Identifier"===e.type)p+=`{{${e.value}}}`;else if("TsAsExpression"===e.type&&"ObjectExpression"===e.expression?.type){const t=e.expression,n=t.properties.filter(e=>"KeyValueProperty"===e.type&&e.key&&"Identifier"===e.key.type).map(e=>e.key.value);if(n.length>0)p+=n.map(e=>`{{${e}}}`).join("");else{const e=t.properties[0];if(!e||"Identifier"!==e.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${e.value}}}`}}else if("ObjectExpression"===e.type){const t=e.properties[0];if(t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type)p+=`{{${t.key.value}}}`;else{if(!t||"Identifier"!==t.type)throw new Error("Unrecognized expression in JSX placeholder");p+=`{{${t.value}}}`}}else if("MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type)p+=`{{${e.property.value}}}`;else if("CallExpression"===e.type&&"Identifier"===e.callee?.type)p+=`{{${e.callee.value}}}`;else if("ConditionalExpression"===e.type){const t=r(e.consequent),n=r(e.alternate);p+=void 0!==t&&void 0!==n?t.length>=n.length?t:n:void 0!==t?t:void 0!==n?n:""}else if("JSXEmptyExpression"!==e.type)throw new Error(`Unrecognized expression in JSX placeholder: ${e.type}`);continue}if("JSXElement"===d.type){const r=o?l(d):void 0;let s;if(d.opening&&d.opening.name&&"Identifier"===d.opening.name.type&&(s=d.opening.name.value),s&&n.has(s)){const l=d.opening&&Array.isArray(d.opening.attributes)&&d.opening.attributes.length>0,y=d.children||[],v=y.length>0,x=1===y.length&&("JSXText"===y[0]?.type||"JSXExpressionContainer"===y[0]?.type&&void 0!==i(y[0].expression)),S="p"===s;let h=0;if(S&&o&&(h=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!v||x){const i=x?g(y,void 0):"";if(""!==String(i).trim())if(n.has(s)&&"p"!==s)p+=`<${s}>${f(i)}</${s}>`;else if(S&&o&&h>1)p+=`<${s}>${f(i)}</${s}>`;else if(S){const e=o&&void 0!==r?r:u.indexOf(d);p+=`<${e}>${f(i)}</${e}>`}else{const e=o&&void 0!==r?r:u.indexOf(d);p+=`<${e}>${f(i)}</${e}>`}else{const n=e[t-1];n&&"JSXText"===n.type&&/\n\s*$/.test(n.value)&&(p=p.replace(/\s+$/,"")),p+=`<${s} />`}}else if(l&&!x){const e=o&&void 0!==r?r:a(d),t=c(y),n=g(y,t.size?t:void 0);p+=`<${e}>${f(n)}</${e}>`}else{const e=u.indexOf(d);if(-1!==e){const t=void 0!==r?r:e,n=c(y),i=g(y,n.size?n:void 0,!1);p+=`<${t}>${f(i)}</${t}>`}else{const e=g(y,void 0,!1);p+=`<${s}>${f(e)}</${s}>`}}}else{const e=d.children||[],t=a(d),n=c(e),i=g(e,n.size?n:void 0);p+=`<${t}>${f(i)}</${t}>`}continue}"JSXFragment"!==d.type||(p+=g(d.children||[]))}}return p}const d=g(e,void 0,!0),v=String(d).replace(/<br \/>\s*\n\s*/g,"<br />"),x=String(v),S=new Set;if(y&&y.size>0)for(let e=0;e<u.length;e++)y.has(u[e])&&S.add(e);let h=String(x);if(S&&S.size>0)for(const e of S)try{h=h.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),h=h.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}h=h.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return S.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),h=h.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return S.has(i)?`${t}<${n}`:`${t} <${n}`}),h=h.replace(/\s*\n\s*/g," "),h=h.replace(/\s+/g," "),h=h.replace(/\s+([,;:!?.])/g,"$1");const m=h.trim();let J=String(m);if(S&&S.size>0)for(const e of S)try{J=J.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),J=J.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<u.length;e++){const t=u[e];if(!t||"JSXElement"!==t.type)continue;const n=u[e-1],i=u[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const r=String(n.value),s=String(i.value),o=r.replace(/\n\s*$/,""),p=/[A-Za-z0-9]$/.test(o),a=/^[A-Za-z0-9]/.test(s.trim()),l=/^[a-z]/.test(s.trim()),c=/\s\n/.test(r),y=s&&/^\s/.test(s)&&!/^\n/.test(s);if(p&&a&&l&&!c&&!y){const t=e;J=J.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}return J.trim()}(n.children,o);let X;const E="JSXAttribute"===a?.type?s(a):void 0;if(void 0!==E)X=E;else{const e=o.extract.defaultValue;X="string"==typeof e?e:""}let $,b;if("JSXAttribute"===p?.type){if("StringLiteral"===p.value?.type){if($=p.value,b=$.value,!b||""===b.trim())return null;if(m&&"StringLiteral"===$.type){const e=o.extract.nsSeparator??":",t=$.value;if(e&&t.startsWith(`${m}${e}`)){if(b=t.slice(`${m}${e}`.length),!b||""===b.trim())return null;$={...$,value:b}}}}else"JSXExpressionContainer"===p.value?.type&&"JSXEmptyExpression"!==p.value.expression.type&&($=p.value.expression);if(!$)return null}a||!b||J.trim()?!a&&J.trim()&&(X=J):X=b;return{keyExpression:$,serializedChildren:J,ns:m,defaultValue:X,hasCount:y,isOrdinal:v,contextExpression:S,optionsNode:g,explicitDefault:void 0!==E||(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})(g)}}export{o as extractFromTransComponent};
1
+ import{getObjectPropValueExpression as e,getObjectPropValue as t,isSimpleTemplateLiteral as n}from"./ast-utils.js";import*as r from"react";import{getDefaults as i,nodesToString as a}from"react-i18next";function s(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?function(e){if(e)return"StringLiteral"===e.type?e.value:"TemplateLiteral"===e.type&&n(e)?e.quasis[0].cooked:void 0}(e.value.expression):void 0}function u(n,r){const u=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),l=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 y;p||"JSXAttribute"!==o?.type||"JSXExpressionContainer"!==o.value?.type||"ObjectExpression"!==o.value.expression.type||(y=e(o.value.expression,"count"));const f=!!p||!!y,v=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),m="JSXAttribute"===v?.type&&"JSXExpressionContainer"===v.value?.type&&"ObjectExpression"===v.value.expression.type?v.value.expression:void 0,d=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),x=!!d,S=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let b="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 J;J="JSXAttribute"===g?.type?s(g):void 0,m&&(void 0===J&&(J=t(m,"ns")),void 0===b&&(b=e(m,"context")));const X=function(e,t){const n={...i()};t.extract.transKeepBasicHtmlNodesFor&&(n.transKeepBasicHtmlNodesFor=t.extract.transKeepBasicHtmlNodesFor);return a(c(e),n)}(n.children,r);let E;const A="JSXAttribute"===l?.type?s(l):void 0;if(void 0!==A)E=A;else{const e=r.extract.defaultValue;E="string"==typeof e?e:""}let h,I;if("JSXAttribute"===u?.type){if("StringLiteral"===u.value?.type){if(h=u.value,I=h.value,!I||""===I.trim())return null;if(J&&"StringLiteral"===h.type){const e=r.extract.nsSeparator??":",t=h.value;if(e&&t.startsWith(`${J}${e}`)){if(I=t.slice(`${J}${e}`.length),!I||""===I.trim())return null;h={...h,value:I}}}}else"JSXExpressionContainer"===u.value?.type&&"JSXEmptyExpression"!==u.value.expression.type&&(h=u.value.expression);if(!h)return null}l||!I||X.trim()?!l&&X.trim()&&(E=X):E=I;return{keyExpression:h,serializedChildren:X,ns:J,defaultValue:E,hasCount:f,isOrdinal:x,contextExpression:b,optionsNode:m,explicitDefault:void 0!==A||(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})(m)}}function l(e){const t=()=>null;return Object.defineProperty(t,"name",{value:e}),t.displayName=e,t}function p(e){switch(e.type){case"JSXEmptyExpression":return null;case"TsAsExpression":case"ParenthesisExpression":return p(e.expression);case"ConditionalExpression":{const t=p(e.consequent),n=p(e.alternate);return"string"==typeof t&&"string"==typeof n&&n.length!==t.length&&n.startsWith(t)?n:t}case"StringLiteral":return e.value;case"TemplateLiteral":if(n(e))return e.quasis[0].raw;break;case"Identifier":return{[e.value]:e.value};case"ObjectExpression":{const t=e.properties.map(e=>"KeyValueProperty"!==e.type||"Identifier"!==e.key.type&&"StringLiteral"!==e.key.type?"Identifier"===e.type?e.value:null:e.key.value).filter(e=>null!==e);return Object.fromEntries(t.map(e=>[e,e]))}}return r.createElement("expression",{expression:e})}function o(e){switch(e.type){case"JSXText":return n=(n=e.value).replace(/\r\n/g,"\n"),/^\s+$/.test(n)&&/\n/.test(n)?null:n=(n=(n=n.replace(/^[ \t]*\n[ \t]*/,"")).replace(/[ \t]*\n[ \t]*$/,"")).replace(/[ \t]*\n[ \t]*/g," ");case"JSXExpressionContainer":return p(e.expression);case"JSXSpreadChild":return"";case"JSXElement":return r.createElement(function(e){switch(e.type){case"Identifier":return/\p{Uppercase_Letter}/u.test(e.value)?l(e.value):e.value;case"JSXMemberExpression":return l(e.property.value);case"JSXNamespacedName":return`${e.namespace.value}:${e.name.value}`}}(e.opening.name),(t=e.opening.attributes).length?Object.fromEntries(t.map(e=>"SpreadElement"===e.type?null:"Identifier"===e.name.type?[e.name.value,""]:[`${e.name.namespace.value}:${e.name.name.value}`,""]).filter(e=>null!=e)):null,...c(e.children));case"JSXFragment":return r.createElement(r.Fragment,null,...c(e.children))}var t,n}function c(e){return e.map(o).filter(e=>null!==e)}export{u as extractFromTransComponent};
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- export{defineConfig}from"./config.js";export{extract,runExtractor}from"./extractor/core/extractor.js";export{findKeys}from"./extractor/core/key-finder.js";export{getTranslations}from"./extractor/core/translation-manager.js";export{recommendedAcceptedAttributes,recommendedAcceptedTags,runLinter}from"./linter.js";export{runSyncer}from"./syncer.js";export{runStatus}from"./status.js";export{runTypesGenerator}from"./types-generator.js";export{runRenameKey}from"./rename-key.js";
1
+ export{defineConfig}from"./config.js";export{extract,runExtractor}from"./extractor/core/extractor.js";export{findKeys}from"./extractor/core/key-finder.js";export{getTranslations}from"./extractor/core/translation-manager.js";import"react";import"react-i18next";export{recommendedAcceptedAttributes,recommendedAcceptedTags,runLinter}from"./linter.js";export{runSyncer}from"./syncer.js";export{runStatus}from"./status.js";export{runTypesGenerator}from"./types-generator.js";export{runRenameKey}from"./rename-key.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-cli",
3
- "version": "1.33.4",
3
+ "version": "1.34.0",
4
4
  "description": "A unified, high-performance i18next CLI.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -82,6 +82,8 @@
82
82
  "jsonc-parser": "3.3.1",
83
83
  "minimatch": "10.1.1",
84
84
  "ora": "9.0.0",
85
+ "react": "^19.2.3",
86
+ "react-i18next": "^16.5.1",
85
87
  "swc-walk": "1.0.1"
86
88
  }
87
89
  }
@@ -1 +1 @@
1
- {"version":3,"file":"extractor.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/extractor.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAKtF,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAK5C;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,oBAAoB,EAC5B,EACE,WAAmB,EACnB,QAAgB,EAChB,uBAA+B,EAC/B,OAAe,EAChB,GAAE;IACD,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,OAAO,CAAC,EAAE,OAAO,CAAC;CACd,EACN,MAAM,GAAE,MAA4B,GACnC,OAAO,CAAC,OAAO,CAAC,CA8DlB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EAAE,EACjB,WAAW,EAAE,WAAW,EACxB,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,MAAM,GAAE,MAA4B,GACnC,OAAO,CAAC,IAAI,CAAC,CAiGf;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,OAAO,CAAE,MAAM,EAAE,oBAAoB,EAAE,EAAE,uBAA+B,EAAE,GAAE;IAAE,uBAAuB,CAAC,EAAE,OAAO,CAAA;CAAO,sDAO3I"}
1
+ {"version":3,"file":"extractor.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/extractor.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAKtF,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAK5C;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,YAAY,CAChC,MAAM,EAAE,oBAAoB,EAC5B,EACE,WAAmB,EACnB,QAAgB,EAChB,uBAA+B,EAC/B,OAAe,EAChB,GAAE;IACD,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAClC,OAAO,CAAC,EAAE,OAAO,CAAC;CACd,EACN,MAAM,GAAE,MAA4B,GACnC,OAAO,CAAC,OAAO,CAAC,CA8DlB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,MAAM,EAAE,EACjB,WAAW,EAAE,WAAW,EACxB,aAAa,EAAE,aAAa,EAC5B,MAAM,EAAE,IAAI,CAAC,oBAAoB,EAAE,SAAS,CAAC,EAC7C,MAAM,GAAE,MAA4B,GACnC,OAAO,CAAC,IAAI,CAAC,CAiGf;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,OAAO,CAAE,MAAM,EAAE,oBAAoB,EAAE,EAAE,uBAA+B,EAAE,GAAE;IAAE,uBAAuB,CAAC,EAAE,OAAO,CAAA;CAAO,sDAO3I"}
@@ -1 +1 @@
1
- {"version":3,"file":"jsx-parser.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/jsx-parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAgB,UAAU,EAAiB,gBAAgB,EAAE,MAAM,WAAW,CAAA;AACtG,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAGvD,MAAM,WAAW,sBAAsB;IACrC,gDAAgD;IAChD,aAAa,CAAC,EAAE,UAAU,CAAC;IAE3B,qDAAqD;IACrD,kBAAkB,EAAE,MAAM,CAAC;IAE3B,mDAAmD;IACnD,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB,8DAA8D;IAC9D,EAAE,CAAC,EAAE,MAAM,CAAC;IAEZ,oEAAoE;IACpE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,0DAA0D;IAC1D,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,8EAA8E;IAC9E,WAAW,CAAC,EAAE,gBAAgB,CAAC;IAE/B,mDAAmD;IACnD,iBAAiB,CAAC,EAAE,UAAU,CAAC;IAE/B,kHAAkH;IAClH,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AAuDD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,yBAAyB,CAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,oBAAoB,GAAG,sBAAsB,GAAG,IAAI,CA2LxH"}
1
+ {"version":3,"file":"jsx-parser.d.ts","sourceRoot":"","sources":["../../../src/extractor/parsers/jsx-parser.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAsC,UAAU,EAAkD,gBAAgB,EAAE,MAAM,WAAW,CAAA;AAC7J,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAKvD,MAAM,WAAW,sBAAsB;IACrC,gDAAgD;IAChD,aAAa,CAAC,EAAE,UAAU,CAAC;IAE3B,qDAAqD;IACrD,kBAAkB,EAAE,MAAM,CAAC;IAE3B,mDAAmD;IACnD,YAAY,CAAC,EAAE,MAAM,CAAC;IAEtB,8DAA8D;IAC9D,EAAE,CAAC,EAAE,MAAM,CAAC;IAEZ,oEAAoE;IACpE,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB,0DAA0D;IAC1D,SAAS,CAAC,EAAE,OAAO,CAAC;IAEpB,8EAA8E;IAC9E,WAAW,CAAC,EAAE,gBAAgB,CAAC;IAE/B,mDAAmD;IACnD,iBAAiB,CAAC,EAAE,UAAU,CAAC;IAE/B,kHAAkH;IAClH,eAAe,CAAC,EAAE,OAAO,CAAC;CAC3B;AA4BD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,yBAAyB,CAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,oBAAoB,GAAG,sBAAsB,GAAG,IAAI,CA2LxH"}