i18next-cli 1.23.6 → 1.23.7
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 +4 -0
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/extractor/parsers/jsx-parser.js +1 -1
- package/dist/esm/cli.js +1 -1
- package/dist/esm/extractor/parsers/jsx-parser.js +1 -1
- package/package.json +1 -1
- package/src/cli.ts +1 -1
- package/src/extractor/parsers/jsx-parser.ts +250 -29
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.23.7](https://github.com/i18next/i18next-cli/compare/v1.23.6...v1.23.7) - 2025-11-12
|
|
9
|
+
|
|
10
|
+
- improved Trans component parsing further [102](https://github.com/i18next/i18next-cli/issues/102)
|
|
11
|
+
|
|
8
12
|
## [1.23.6](https://github.com/i18next/i18next-cli/compare/v1.23.5...v1.23.6) - 2025-11-12
|
|
9
13
|
|
|
10
14
|
- fix jsx extraction [#108](https://github.com/i18next/i18next-cli/issues/108)
|
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"),o=require("glob"),n=require("minimatch"),i=require("chalk"),a=require("./config.js"),r=require("./heuristic-config.js"),c=require("./extractor/core/extractor.js");require("node:path"),require("node:fs/promises"),require("jiti");var s=require("./types-generator.js"),l=require("./syncer.js"),u=require("./migrator.js"),g=require("./init.js"),d=require("./linter.js"),p=require("./status.js"),f=require("./locize.js");const m=new e.Command;m.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.23.
|
|
2
|
+
"use strict";var e=require("commander"),t=require("chokidar"),o=require("glob"),n=require("minimatch"),i=require("chalk"),a=require("./config.js"),r=require("./heuristic-config.js"),c=require("./extractor/core/extractor.js");require("node:path"),require("node:fs/promises"),require("jiti");var s=require("./types-generator.js"),l=require("./syncer.js"),u=require("./migrator.js"),g=require("./init.js"),d=require("./linter.js"),p=require("./status.js"),f=require("./locize.js");const m=new e.Command;m.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.23.7"),m.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),m.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=m.opts().config,i=await a.ensureConfig(o),r=async()=>{const t=await c.runExtractor(i,{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 r(),e.watch){console.log("\nWatching for changes...");const e=await w(i.extract.input),o=y(i.extract.ignore),a=h(i.extract.output),c=[...o,...a].filter(Boolean),s=e.filter(e=>!c.some(t=>n.minimatch(e,t,{dot:!0})));t.watch(s,{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),r()})}}catch(e){console.error("Error running extractor:",e),process.exit(1)}}),m.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)=>{const o=m.opts().config;let n=await a.loadConfig(o);if(!n){console.log(i.blue("No config file found. Attempting to detect project structure..."));const e=await r.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 p.runStatus(n,{detail:e,namespace:t.namespace})}),m.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=m.opts().config,i=await a.ensureConfig(o),r=()=>s.runTypesGenerator(i);if(await r(),e.watch){console.log("\nWatching for changes...");const e=await w(i.types?.input||[]),o=[...y(i.extract?.ignore)].filter(Boolean),a=e.filter(e=>!o.some(t=>n.minimatch(e,t,{dot:!0})));t.watch(a,{persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),r()})}}),m.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=m.opts().config,t=await a.ensureConfig(e);await l.runSyncer(t)}),m.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await u.runMigrator(e)}),m.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(g.runInit),m.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 o=m.opts().config,c=async()=>{let e=await a.loadConfig(o);if(!e){console.log(i.blue("No config file found. Attempting to detect project structure..."));const t=await r.detectConfig();t||(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=t}await d.runLinterCli(e)};if(await c(),e.watch){console.log("\nWatching for changes...");const e=await a.loadConfig(o);if(e?.extract?.input){const o=await w(e.extract.input),i=[...y(e.extract.ignore),...h(e.extract.output)].filter(Boolean),a=o.filter(e=>!i.some(t=>n.minimatch(e,t,{dot:!0})));t.watch(a,{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),c()})}}}),m.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=m.opts().config,o=await a.ensureConfig(t);await f.runLocizeSync(o,e)}),m.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const t=m.opts().config,o=await a.ensureConfig(t);await f.runLocizeDownload(o,e)}),m.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const t=m.opts().config,o=await a.ensureConfig(t);await f.runLocizeMigrate(o,e)}),m.parse(process.argv);const y=e=>Array.isArray(e)?e:e?[e]:[],h=e=>e&&"string"==typeof e?[e.replace(/\{\{[^}]+\}\}/g,"*")]:[],w=async(e=[])=>{const t=y(e),n=await Promise.all(t.map(e=>o.glob(e||"",{nodir:!0})));return Array.from(new Set(n.flat()))};
|
|
@@ -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(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?t(e.value.expression):void 0}exports.extractFromTransComponent=function(i,r){const s=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),o=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),p=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),
|
|
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(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?t(e.value.expression):void 0}exports.extractFromTransComponent=function(i,r){const s=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),o=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),p=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),a=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let l;p||"JSXAttribute"!==a?.type||"JSXExpressionContainer"!==a.value?.type||"ObjectExpression"!==a.value.expression.type||(l=e.getObjectPropValueExpression(a.value.expression,"count"));const u=!!p||!!l,y=i.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,c=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),g=!!c,d=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===d?.type&&"JSXExpressionContainer"===d.value?.type?d.value.expression:"JSXAttribute"===d?.type&&"StringLiteral"===d.value?.type?d.value:void 0;const S=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let x;x="JSXAttribute"===S?.type?n(S):void 0,f&&(void 0===x&&(x=e.getObjectPropValue(f,"ns")),void 0===v&&(v=e.getObjectPropValueExpression(f,"context")));const J=function(e,n){if(!e||0===e.length)return"";const i=new Set(n.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),r=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function s(e,n,o=!1,p=!1){if(!e||!e.length)return;const a=p&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let l=0,u=e.length-1;for(;l<=u&&r(e[l]);)l++;for(;u>=l&&r(e[u]);)u--;const y=l<=u?e.slice(l,u+1):[],f=y.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<y.length;e++){const p=y[e];if(p)if("JSXText"!==p.type){if("JSXExpressionContainer"===p.type){if(o&&!f&&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}const n=t(p.expression);if(void 0!==n){if(!(/^\s*$/.test(n)&&!n.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const i=t(p.expression);if(void 0!==i){const t=/^\s*$/.test(i)&&!i.includes("\n"),s=y[e-1],o=y[e+1];if(t){const t=y[e+2];if(o&&"JSXText"===o.type&&r(o)&&t&&("JSXElement"===t.type||"JSXFragment"===t.type)){const t=y[e-1],n=y[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&r(o))continue;const i=!o||"JSXText"===o.type&&!r(o);if(s&&"JSXText"===s.type&&i){const e=n[n.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&&r(o))continue}}n.push(p);continue}if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&i.has(e)){const i=p.opening&&Array.isArray(p.opening.attributes)&&p.opening.attributes.length>0,r=p.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression)),l=!r.length,u=o;i&&!o?(n.push(p),s(p.children||[],n,!0)):l?n.push(p):u||("p"===e&&a?(n.push(p),s(p.children||[],n,!0,!1)):s(p.children||[],n,!1,!1));continue}n.push(p),s(p.children||[],n,!0);continue}"JSXFragment"!==p.type||s(p.children||[],n,o)}else{if(o&&!f)continue;if(o&&r(p))continue;if(r(p)){const t=y[e-1],i=y[e+1];if(t&&("JSXElement"===t.type||"JSXFragment"===t.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const r=n[n.length-1],s=y[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(o&&f&&0===e)continue;n.push(p)}}}const o=[];s(e,o,!1,!0);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function a(e,n,s=!1){if(!e||0===e.length)return"";let l="",u=0;for(let y=0;y<e.length;y++){const f=e[y];if(f)if("JSXText"!==f.type){if("JSXExpressionContainer"===f.type){const e=f.expression;if(!e)continue;const n=t(e);if(void 0!==n)l+=n;else if("Identifier"===e.type)l+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?l+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?l+=`{{${t.value}}}`:l+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?l+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?l+=`{{${e.callee.value}}}`:l+="{{value}}";continue}if("JSXElement"===f.type){let c;f.opening&&f.opening.name&&"Identifier"===f.opening.name.type&&(c=f.opening.name.value);const g=s?u:void 0;if(s&&"JSXElement"===f.type&&u++,c&&i.has(c)){const s=f.opening&&Array.isArray(f.opening.attributes)&&f.opening.attributes.length>0,u=f.children||[],d=u.length>0,v=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==t(u[0].expression));if(!d||v){const t=v?a(u,void 0):"";if(""!==String(t).trim())l+=`<${c}>${t}</${c}>`;else{const t=e[y-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(l=l.replace(/\s+$/,"")),l+=`<${c} />`}}else if(s&&!v){const e=u;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const t=new Map;let r=0;for(const n of e)if(n&&"JSXElement"===n.type){const e=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(e&&i.has(e)){const e=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,i=n.children||[],s=1===i.length&&"JSXText"===i[0]?.type;(e||i.length&&!s)&&t.set(n,r++)}else t.set(n,r++)}const s=n&&n.has(f)?n.get(f):o.indexOf(f),u=a(e,t.size?t:void 0);l+=`<${s}>${p(u)}</${s}>`}}else{const e=o.indexOf(f);if(-1!==e){const n=void 0!==g?g:e;if((()=>{let e=!1;for(const t of u)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==o.indexOf(t))return e;if("JSXText"===t.type&&-1!==o.indexOf(t)){if(r(t))continue;if(!e)return!0;if(u.slice(u.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=a(u,void 0,!1);l+=`<${n}>${p(e)}</${n}>`;continue}const s=new Map;let y=n;for(const e of u)if(e&&"JSXElement"===e.type){const n=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(n&&i.has(n)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,i=e.children||[],r=1===i.length&&("JSXText"===i[0]?.type||"JSXExpressionContainer"===i[0]?.type&&void 0!==t(i[0].expression));!n&&(!i.length||r)||s.set(e,y++)}else s.set(e,y++)}const f=a(u,s.size>0?s:void 0,!1);l+=`<${n}>${p(f)}</${n}>`}else{const e=a(u,void 0,!1);l+=`<${c}>${p(e)}</${c}>`}}}else{const e=f.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const t=new Map;let r=0;for(const n of e)if(n&&"JSXElement"===n.type){const e=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(e&&i.has(e)){const e=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,i=n.children||[],s=1===i.length&&"JSXText"===i[0]?.type;!e&&(!i.length||s)||t.set(n,r++)}else t.set(n,r++)}const s=n&&n.has(f)?n.get(f):o.indexOf(f),u=a(e,t.size?t:void 0);l+=`<${s}>${p(u)}</${s}>`}}continue}"JSXFragment"!==f.type||(l+=a(f.children||[]))}else{if(r(f))continue;l+=f.value}}return l}const l=a(e,void 0,!0),u=String(l).replace(/<br \/>\s*\n\s*/g,"<br />").replace(/\s+/g," ");return u.replace(/\s+\./g,".").trim()}(i.children,r);let X;const m="JSXAttribute"===o?.type?n(o):void 0;if(void 0!==m)X=m;else{const e=r.extract.defaultValue;X="string"==typeof e?e:""}let h,b;if("JSXAttribute"===s?.type){if("StringLiteral"===s.value?.type){if(h=s.value,b=h.value,!b||""===b.trim())return null;if(x&&"StringLiteral"===h.type){const e=r.extract.nsSeparator??":",t=h.value;if(e&&t.startsWith(`${x}${e}`)){if(b=t.slice(`${x}${e}`.length),!b||""===b.trim())return null;h={...h,value:b}}}}else"JSXExpressionContainer"===s.value?.type&&"JSXEmptyExpression"!==s.value.expression.type&&(h=s.value.expression);if(!h)return null}return o||!b||J.trim()?!o&&J.trim()&&(X=J):X=b,{keyExpression:h,serializedChildren:J,ns:x,defaultValue:X,hasCount:u,isOrdinal:g,contextExpression:v,optionsNode:f,explicitDefault:void 0!==m||(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/esm/cli.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{Command as t}from"commander";import o from"chokidar";import{glob as e}from"glob";import{minimatch as i}from"minimatch";import n from"chalk";import{ensureConfig as a,loadConfig as r}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{runTypesGenerator as l}from"./types-generator.js";import{runSyncer as p}from"./syncer.js";import{runMigrator as m}from"./migrator.js";import{runInit as f}from"./init.js";import{runLinterCli as d}from"./linter.js";import{runStatus as g}from"./status.js";import{runLocizeSync as u,runLocizeDownload as y,runLocizeMigrate as h}from"./locize.js";const w=new t;w.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.23.
|
|
2
|
+
import{Command as t}from"commander";import o from"chokidar";import{glob as e}from"glob";import{minimatch as i}from"minimatch";import n from"chalk";import{ensureConfig as a,loadConfig as r}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{runTypesGenerator as l}from"./types-generator.js";import{runSyncer as p}from"./syncer.js";import{runMigrator as m}from"./migrator.js";import{runInit as f}from"./init.js";import{runLinterCli as d}from"./linter.js";import{runStatus as g}from"./status.js";import{runLocizeSync as u,runLocizeDownload as y,runLocizeMigrate as h}from"./locize.js";const w=new t;w.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.23.7"),w.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),w.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 e=w.opts().config,n=await a(e),r=async()=>{const o=await s(n,{isWatchMode:!!t.watch,isDryRun:!!t.dryRun,syncPrimaryWithDefaults:!!t.syncPrimary});return t.ci&&!o?(console.log("✅ No files were updated."),process.exit(0)):t.ci&&o&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),o};if(await r(),t.watch){console.log("\nWatching for changes...");const t=await z(n.extract.input),e=x(n.extract.ignore),a=j(n.extract.output),c=[...e,...a].filter(Boolean),s=t.filter(t=>!c.some(o=>i(t,o,{dot:!0})));o.watch(s,{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),r()})}}catch(t){console.error("Error running extractor:",t),process.exit(1)}}),w.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,o)=>{const e=w.opts().config;let i=await r(e);if(!i){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!")),i=t}await g(i,{detail:t,namespace:o.namespace})}),w.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 e=w.opts().config,n=await a(e),r=()=>l(n);if(await r(),t.watch){console.log("\nWatching for changes...");const t=await z(n.types?.input||[]),e=[...x(n.extract?.ignore)].filter(Boolean),a=t.filter(t=>!e.some(o=>i(t,o,{dot:!0})));o.watch(a,{persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),r()})}}),w.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const t=w.opts().config,o=await a(t);await p(o)}),w.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async t=>{await m(t)}),w.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(f),w.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 e=w.opts().config,a=async()=>{let t=await r(e);if(!t){console.log(n.blue("No config file found. Attempting to detect project structure..."));const o=await c();o||(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=o}await d(t)};if(await a(),t.watch){console.log("\nWatching for changes...");const t=await r(e);if(t?.extract?.input){const e=await z(t.extract.input),n=[...x(t.extract.ignore),...j(t.extract.output)].filter(Boolean),r=e.filter(t=>!n.some(o=>i(t,o,{dot:!0})));o.watch(r,{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),a()})}}}),w.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 o=w.opts().config,e=await a(o);await u(e,t)}),w.command("locize-download").description("Download all translations from your locize project.").action(async t=>{const o=w.opts().config,e=await a(o);await y(e,t)}),w.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async t=>{const o=w.opts().config,e=await a(o);await h(e,t)}),w.parse(process.argv);const x=t=>Array.isArray(t)?t:t?[t]:[],j=t=>t&&"string"==typeof t?[t.replace(/\{\{[^}]+\}\}/g,"*")]:[],z=async(t=[])=>{const o=x(t),i=await Promise.all(o.map(t=>e(t||"",{nodir:!0})));return Array.from(new Set(i.flat()))};
|
|
@@ -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){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?i(e.value.expression):void 0}function
|
|
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){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?i(e.value.expression):void 0}function o(n,o){const s=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),a=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),l=n.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(l.value.expression,"count"));const y=!!a||!!u,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),c="JSXAttribute"===f?.type&&"JSXExpressionContainer"===f.value?.type&&"ObjectExpression"===f.value.expression.type?f.value.expression:void 0,g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),d=!!g,v=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===v?.type&&"JSXExpressionContainer"===v.value?.type?v.value.expression:"JSXAttribute"===v?.type&&"StringLiteral"===v.value?.type?v.value:void 0;const x=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let J;J="JSXAttribute"===x?.type?r(x):void 0,c&&(void 0===J&&(J=t(c,"ns")),void 0===S&&(S=e(c,"context")));const X=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),r=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function o(e,t,s=!1,p=!1){if(!e||!e.length)return;const a=p&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let l=0,u=e.length-1;for(;l<=u&&r(e[l]);)l++;for(;u>=l&&r(e[u]);)u--;const y=l<=u?e.slice(l,u+1):[],f=y.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<y.length;e++){const p=y[e];if(p)if("JSXText"!==p.type){if("JSXExpressionContainer"===p.type){if(s&&!f&&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}const t=i(p.expression);if(void 0!==t){if(!(/^\s*$/.test(t)&&!t.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const n=i(p.expression);if(void 0!==n){const i=/^\s*$/.test(n)&&!n.includes("\n"),o=y[e-1],s=y[e+1];if(i){const n=y[e+2];if(s&&"JSXText"===s.type&&r(s)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=y[e-1],n=y[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(o&&("JSXElement"===o.type||"JSXFragment"===o.type)&&s&&"JSXText"===s.type&&r(s))continue;const i=!s||"JSXText"===s.type&&!r(s);if(o&&"JSXText"===o.type&&i){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(o&&("JSXElement"===o.type||"JSXFragment"===o.type)&&s&&"JSXText"===s.type&&r(s))continue}}t.push(p);continue}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)){const n=p.opening&&Array.isArray(p.opening.attributes)&&p.opening.attributes.length>0,r=p.children||[],s=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression)),l=!r.length,u=s;n&&!s?(t.push(p),o(p.children||[],t,!0)):l?t.push(p):u||("p"===e&&a?(t.push(p),o(p.children||[],t,!0,!1)):o(p.children||[],t,!1,!1));continue}t.push(p),o(p.children||[],t,!0);continue}"JSXFragment"!==p.type||o(p.children||[],t,s)}else{if(s&&!f)continue;if(s&&r(p))continue;if(r(p)){const n=y[e-1],i=y[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const r=t[t.length-1],o=y[e-1];if(r){if(o&&"JSXExpressionContainer"===o.type)continue;if("JSXText"===r.type&&o&&"JSXText"===o.type){r.value=String(r.value)+p.value;continue}}}if(s&&f&&0===e)continue;t.push(p)}}}const s=[];o(e,s,!1,!0);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function a(e,t,o=!1){if(!e||0===e.length)return"";let l="",u=0;for(let y=0;y<e.length;y++){const f=e[y];if(f)if("JSXText"!==f.type){if("JSXExpressionContainer"===f.type){const e=f.expression;if(!e)continue;const t=i(e);if(void 0!==t)l+=t;else if("Identifier"===e.type)l+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?l+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?l+=`{{${t.value}}}`:l+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?l+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?l+=`{{${e.callee.value}}}`:l+="{{value}}";continue}if("JSXElement"===f.type){let c;f.opening&&f.opening.name&&"Identifier"===f.opening.name.type&&(c=f.opening.name.value);const g=o?u:void 0;if(o&&"JSXElement"===f.type&&u++,c&&n.has(c)){const o=f.opening&&Array.isArray(f.opening.attributes)&&f.opening.attributes.length>0,u=f.children||[],d=u.length>0,v=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==i(u[0].expression));if(!d||v){const t=v?a(u,void 0):"";if(""!==String(t).trim())l+=`<${c}>${t}</${c}>`;else{const t=e[y-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(l=l.replace(/\s+$/,"")),l+=`<${c} />`}}else if(o&&!v){const e=u;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==s.indexOf(e))){const t=s.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const i=new Map;let r=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)){const e=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,n=t.children||[],o=1===n.length&&"JSXText"===n[0]?.type;(e||n.length&&!o)&&i.set(t,r++)}else i.set(t,r++)}const o=t&&t.has(f)?t.get(f):s.indexOf(f),u=a(e,i.size?i:void 0);l+=`<${o}>${p(u)}</${o}>`}}else{const e=s.indexOf(f);if(-1!==e){const t=void 0!==g?g:e;if((()=>{let e=!1;for(const t of u)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==s.indexOf(t))return e;if("JSXText"===t.type&&-1!==s.indexOf(t)){if(r(t))continue;if(!e)return!0;if(u.slice(u.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=a(u,void 0,!1);l+=`<${t}>${p(e)}</${t}>`;continue}const o=new Map;let y=t;for(const e of u)if(e&&"JSXElement"===e.type){const t=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(t&&n.has(t)){const t=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,n=e.children||[],r=1===n.length&&("JSXText"===n[0]?.type||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!t&&(!n.length||r)||o.set(e,y++)}else o.set(e,y++)}const f=a(u,o.size>0?o:void 0,!1);l+=`<${t}>${p(f)}</${t}>`}else{const e=a(u,void 0,!1);l+=`<${c}>${p(e)}</${c}>`}}}else{const e=f.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==s.indexOf(e))){const t=s.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const i=new Map;let r=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)){const e=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,n=t.children||[],o=1===n.length&&"JSXText"===n[0]?.type;!e&&(!n.length||o)||i.set(t,r++)}else i.set(t,r++)}const o=t&&t.has(f)?t.get(f):s.indexOf(f),u=a(e,i.size?i:void 0);l+=`<${o}>${p(u)}</${o}>`}}continue}"JSXFragment"!==f.type||(l+=a(f.children||[]))}else{if(r(f))continue;l+=f.value}}return l}const l=a(e,void 0,!0),u=String(l).replace(/<br \/>\s*\n\s*/g,"<br />").replace(/\s+/g," ");return u.replace(/\s+\./g,".").trim()}(n.children,o);let m;const h="JSXAttribute"===p?.type?r(p):void 0;if(void 0!==h)m=h;else{const e=o.extract.defaultValue;m="string"==typeof e?e:""}let b,E;if("JSXAttribute"===s?.type){if("StringLiteral"===s.value?.type){if(b=s.value,E=b.value,!E||""===E.trim())return null;if(J&&"StringLiteral"===b.type){const e=o.extract.nsSeparator??":",t=b.value;if(e&&t.startsWith(`${J}${e}`)){if(E=t.slice(`${J}${e}`.length),!E||""===E.trim())return null;b={...b,value:E}}}}else"JSXExpressionContainer"===s.value?.type&&"JSXEmptyExpression"!==s.value.expression.type&&(b=s.value.expression);if(!b)return null}p||!E||X.trim()?!p&&X.trim()&&(m=X):m=E;return{keyExpression:b,serializedChildren:X,ns:J,defaultValue:m,hasCount:y,isOrdinal:d,contextExpression:S,optionsNode:c,explicitDefault:void 0!==h||(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})(c)}}export{o as extractFromTransComponent};
|
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -22,7 +22,7 @@ const program = new Command()
|
|
|
22
22
|
program
|
|
23
23
|
.name('i18next-cli')
|
|
24
24
|
.description('A unified, high-performance i18next CLI.')
|
|
25
|
-
.version('1.23.
|
|
25
|
+
.version('1.23.7')
|
|
26
26
|
|
|
27
27
|
// new: global config override option
|
|
28
28
|
program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)')
|
|
@@ -295,9 +295,14 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
295
295
|
n.value.includes('\n')
|
|
296
296
|
|
|
297
297
|
// Build deterministic global slot list (pre-order)
|
|
298
|
-
function collectSlots (nodes: any[], slots: any[], parentIsNonPreserved = false) {
|
|
298
|
+
function collectSlots (nodes: any[], slots: any[], parentIsNonPreserved = false, isRootLevel = false) {
|
|
299
299
|
if (!nodes || !nodes.length) return
|
|
300
300
|
|
|
301
|
+
// Check if there are multiple <p> elements at root level
|
|
302
|
+
const multiplePAtRoot = isRootLevel && nodes.filter((n: any) =>
|
|
303
|
+
n && n.type === 'JSXElement' && n.opening?.name?.value === 'p'
|
|
304
|
+
).length > 1
|
|
305
|
+
|
|
301
306
|
// First, identify boundary whitespace nodes (start and end of sibling list)
|
|
302
307
|
// We trim ONLY pure-whitespace JSXText nodes from the boundaries
|
|
303
308
|
let startIdx = 0
|
|
@@ -504,16 +509,50 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
504
509
|
? n.opening.name.value
|
|
505
510
|
: undefined
|
|
506
511
|
if (tagName && allowedTags.has(tagName)) {
|
|
507
|
-
//
|
|
508
|
-
//
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
512
|
+
// Check if this preserved tag will actually be preserved as literal HTML
|
|
513
|
+
// or if it will be indexed (has complex children or attributes)
|
|
514
|
+
const hasAttrs =
|
|
515
|
+
n.opening &&
|
|
516
|
+
Array.isArray((n.opening as any).attributes) &&
|
|
517
|
+
(n.opening as any).attributes.length > 0
|
|
518
|
+
const children = n.children || []
|
|
519
|
+
// Check for single PURE text child (JSXText OR simple string expression)
|
|
520
|
+
const isSinglePureTextChild =
|
|
521
|
+
children.length === 1 && (
|
|
522
|
+
children[0]?.type === 'JSXText' ||
|
|
523
|
+
(children[0]?.type === 'JSXExpressionContainer' &&
|
|
524
|
+
getStringLiteralFromExpression(children[0].expression) !== undefined)
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
// Self-closing tags (no children) should be added to slots but rendered as literal HTML
|
|
528
|
+
// Tags with single pure text child should NOT be added to slots and rendered as literal HTML
|
|
529
|
+
const isSelfClosing = !children.length
|
|
530
|
+
const hasTextContent = isSinglePureTextChild
|
|
531
|
+
|
|
532
|
+
if (hasAttrs && !isSinglePureTextChild) {
|
|
533
|
+
// Has attributes AND complex children -> will be indexed, add to slots
|
|
534
|
+
slots.push(n)
|
|
535
|
+
collectSlots(n.children || [], slots, true)
|
|
536
|
+
} else if (isSelfClosing) {
|
|
537
|
+
// Self-closing tag with no attributes: add to slots (affects indexes) but will render as literal
|
|
514
538
|
slots.push(n)
|
|
539
|
+
} else if (!hasTextContent) {
|
|
540
|
+
// Has complex children but no attributes
|
|
541
|
+
// For <p> tags at root level with multiple <p> siblings, index them
|
|
542
|
+
// For other preserved tags, preserve as literal (don't add to slots)
|
|
543
|
+
if (tagName === 'p' && multiplePAtRoot) {
|
|
544
|
+
slots.push(n)
|
|
545
|
+
collectSlots(n.children || [], slots, true, false)
|
|
546
|
+
} else {
|
|
547
|
+
// Other preserved tags: preserve as literal, don't add to slots
|
|
548
|
+
// But DO process children to add them to slots
|
|
549
|
+
collectSlots(n.children || [], slots, false, false)
|
|
550
|
+
}
|
|
551
|
+
} else {
|
|
552
|
+
// Has single pure text child and no attributes: preserve as literal, don't add to slots
|
|
553
|
+
// Don't process children either - they're part of the preserved tag
|
|
515
554
|
}
|
|
516
|
-
|
|
555
|
+
continue
|
|
517
556
|
} else {
|
|
518
557
|
// non-preserved element: the element itself is a single slot.
|
|
519
558
|
// Pre-order: allocate the parent's slot first, then descend into its
|
|
@@ -537,7 +576,7 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
537
576
|
|
|
538
577
|
// prepare the global slot list for the whole subtree
|
|
539
578
|
const globalSlots: any[] = []
|
|
540
|
-
collectSlots(children, globalSlots, false)
|
|
579
|
+
collectSlots(children, globalSlots, false, true)
|
|
541
580
|
|
|
542
581
|
// Trim only newline-only indentation at the edges of serialized inner text.
|
|
543
582
|
// This preserves single leading/trailing spaces which are meaningful between inline placeholders.
|
|
@@ -548,10 +587,13 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
548
587
|
// remove trailing newline-only indentation
|
|
549
588
|
.replace(/\s*\n\s*$/g, '')
|
|
550
589
|
|
|
551
|
-
function visitNodes (nodes: any[], localIndexMap?: Map<any, number
|
|
590
|
+
function visitNodes (nodes: any[], localIndexMap?: Map<any, number>, isRootLevel = false): string {
|
|
552
591
|
if (!nodes || nodes.length === 0) return ''
|
|
553
592
|
let out = ''
|
|
554
593
|
|
|
594
|
+
// At root level, build index based on element position among siblings
|
|
595
|
+
let rootElementIndex = 0
|
|
596
|
+
|
|
555
597
|
for (let i = 0; i < nodes.length; i++) {
|
|
556
598
|
const node = nodes[i]
|
|
557
599
|
if (!node) continue
|
|
@@ -596,24 +638,195 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
596
638
|
tag = node.opening.name.value
|
|
597
639
|
}
|
|
598
640
|
|
|
641
|
+
// Track root element index for root-level elements
|
|
642
|
+
const myRootIndex = isRootLevel ? rootElementIndex : undefined
|
|
643
|
+
if (isRootLevel && node.type === 'JSXElement') {
|
|
644
|
+
rootElementIndex++
|
|
645
|
+
}
|
|
646
|
+
|
|
599
647
|
if (tag && allowedTags.has(tag)) {
|
|
600
|
-
|
|
601
|
-
//
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
648
|
+
// Match react-i18next behavior: only preserve as literal HTML when:
|
|
649
|
+
// 1. No children (!childChildren) AND no attributes (!childPropsCount)
|
|
650
|
+
// 2. OR: Has children but only the children prop (childPropsCount === 1) AND children is a simple string (isString(childChildren))
|
|
651
|
+
|
|
652
|
+
const hasAttrs =
|
|
653
|
+
node.opening &&
|
|
654
|
+
Array.isArray((node.opening as any).attributes) &&
|
|
655
|
+
(node.opening as any).attributes.length > 0
|
|
656
|
+
|
|
657
|
+
const children = node.children || []
|
|
658
|
+
const hasChildren = children.length > 0
|
|
659
|
+
|
|
660
|
+
// Check if children is a single PURE text node (JSXText OR simple string expression)
|
|
661
|
+
const isSinglePureTextChild =
|
|
662
|
+
children.length === 1 && (
|
|
663
|
+
children[0]?.type === 'JSXText' ||
|
|
664
|
+
(children[0]?.type === 'JSXExpressionContainer' &&
|
|
665
|
+
getStringLiteralFromExpression(children[0].expression) !== undefined)
|
|
666
|
+
)
|
|
667
|
+
|
|
668
|
+
// Preserve as literal HTML in two cases:
|
|
669
|
+
// 1. No children and no attributes: <br />
|
|
670
|
+
// 2. Single pure text child (with or without attributes): <strong>text</strong> or <strong title="...">text</strong>
|
|
671
|
+
if ((!hasChildren || isSinglePureTextChild)) {
|
|
672
|
+
const inner = isSinglePureTextChild ? visitNodes(children, undefined) : ''
|
|
673
|
+
const hasMeaningfulChildren = String(inner).trim() !== ''
|
|
674
|
+
|
|
675
|
+
if (!hasMeaningfulChildren) {
|
|
676
|
+
// Self-closing
|
|
677
|
+
const prevOriginal = nodes[i - 1]
|
|
678
|
+
if (prevOriginal && prevOriginal.type === 'JSXText' && /\n\s*$/.test(prevOriginal.value)) {
|
|
679
|
+
out = out.replace(/\s+$/, '')
|
|
680
|
+
}
|
|
681
|
+
out += `<${tag} />`
|
|
682
|
+
} else {
|
|
683
|
+
// Preserve with content: <strong>text</strong>
|
|
684
|
+
out += `<${tag}>${inner}</${tag}>`
|
|
685
|
+
}
|
|
686
|
+
} else if (hasAttrs && !isSinglePureTextChild) {
|
|
687
|
+
// Has attributes -> treat as indexed element with numeric placeholder
|
|
688
|
+
const childrenLocal = children
|
|
689
|
+
const hasNonElementGlobalSlots = childrenLocal.some((ch: any) =>
|
|
690
|
+
ch && (ch.type === 'JSXText' || ch.type === 'JSXExpressionContainer') && globalSlots.indexOf(ch) !== -1
|
|
691
|
+
)
|
|
692
|
+
|
|
693
|
+
if (hasNonElementGlobalSlots) {
|
|
694
|
+
const idx = globalSlots.indexOf(node)
|
|
695
|
+
const inner = visitNodes(childrenLocal, undefined)
|
|
696
|
+
out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
|
|
697
|
+
} else {
|
|
698
|
+
const childrenLocalMap = new Map<any, number>()
|
|
699
|
+
let localIdxCounter = 0
|
|
700
|
+
for (const ch of childrenLocal) {
|
|
701
|
+
if (!ch) continue
|
|
702
|
+
if (ch.type === 'JSXElement') {
|
|
703
|
+
const chTag = ch.opening && ch.opening.name && ch.opening.name.type === 'Identifier'
|
|
704
|
+
? ch.opening.name.value
|
|
705
|
+
: undefined
|
|
706
|
+
if (chTag && allowedTags.has(chTag)) {
|
|
707
|
+
const chHasAttrs =
|
|
708
|
+
ch.opening &&
|
|
709
|
+
Array.isArray((ch.opening as any).attributes) &&
|
|
710
|
+
(ch.opening as any).attributes.length > 0
|
|
711
|
+
const chChildren = ch.children || []
|
|
712
|
+
const chIsSingleText = chChildren.length === 1 && chChildren[0]?.type === 'JSXText'
|
|
713
|
+
// Only skip indexing if it would be preserved as literal
|
|
714
|
+
if (!chHasAttrs && (!chChildren.length || chIsSingleText)) {
|
|
715
|
+
// Will be preserved, don't index
|
|
716
|
+
} else {
|
|
717
|
+
childrenLocalMap.set(ch, localIdxCounter++)
|
|
718
|
+
}
|
|
719
|
+
} else {
|
|
720
|
+
childrenLocalMap.set(ch, localIdxCounter++)
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
const idx = localIndexMap && localIndexMap.has(node) ? localIndexMap.get(node) : globalSlots.indexOf(node)
|
|
726
|
+
const inner = visitNodes(childrenLocal, childrenLocalMap.size ? childrenLocalMap : undefined)
|
|
727
|
+
out += `<${idx}>${trimFormattingEdges(inner)}</${idx}>`
|
|
613
728
|
}
|
|
614
|
-
out += `<${tag} />`
|
|
615
729
|
} else {
|
|
616
|
-
|
|
730
|
+
// Has complex children but no attributes -> preserve tag as literal but index children
|
|
731
|
+
// Check if this tag is in globalSlots - if so, index it
|
|
732
|
+
const idx = globalSlots.indexOf(node)
|
|
733
|
+
if (idx !== -1) {
|
|
734
|
+
// This tag is in globalSlots, so index it
|
|
735
|
+
// At root level, use the element's position among root elements
|
|
736
|
+
const indexToUse = myRootIndex !== undefined ? myRootIndex : idx
|
|
737
|
+
|
|
738
|
+
// Check if children have text/expression nodes in globalSlots
|
|
739
|
+
// that appear BEFORE or BETWEEN element children (not just trailing)
|
|
740
|
+
// Exclude formatting whitespace (newline-only text) from this check
|
|
741
|
+
const hasNonElementGlobalSlots = (() => {
|
|
742
|
+
let foundElement = false
|
|
743
|
+
|
|
744
|
+
for (const ch of children) {
|
|
745
|
+
if (!ch) continue
|
|
746
|
+
|
|
747
|
+
if (ch.type === 'JSXElement') {
|
|
748
|
+
foundElement = true
|
|
749
|
+
continue
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
if (ch.type === 'JSXExpressionContainer' && globalSlots.indexOf(ch) !== -1) {
|
|
753
|
+
// Only count if before/between elements, not trailing
|
|
754
|
+
return foundElement // false if before first element, true if after
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
if (ch.type === 'JSXText' && globalSlots.indexOf(ch) !== -1) {
|
|
758
|
+
// Exclude formatting whitespace
|
|
759
|
+
if (isFormattingWhitespace(ch)) continue
|
|
760
|
+
|
|
761
|
+
// Only count text that appears BEFORE the first element
|
|
762
|
+
// Trailing text after all elements should not force global indexing
|
|
763
|
+
if (!foundElement) {
|
|
764
|
+
// Text before first element - counts
|
|
765
|
+
return true
|
|
766
|
+
}
|
|
767
|
+
// Text after an element - check if there are more elements after this text
|
|
768
|
+
const remainingNodes = children.slice(children.indexOf(ch) + 1)
|
|
769
|
+
const hasMoreElements = remainingNodes.some((n: any) => n && n.type === 'JSXElement')
|
|
770
|
+
if (hasMoreElements) {
|
|
771
|
+
// Text between elements - counts
|
|
772
|
+
return true
|
|
773
|
+
}
|
|
774
|
+
// Trailing text after last element - doesn't count
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
return false
|
|
779
|
+
})()
|
|
780
|
+
|
|
781
|
+
// If children have non-element global slots, use global indexes
|
|
782
|
+
// Otherwise use local indexes starting from parent's index + 1
|
|
783
|
+
if (hasNonElementGlobalSlots) {
|
|
784
|
+
const inner = visitNodes(children, undefined, false)
|
|
785
|
+
out += `<${indexToUse}>${trimFormattingEdges(inner)}</${indexToUse}>`
|
|
786
|
+
continue
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
// Build local index map for children of this indexed element
|
|
790
|
+
const childrenLocalMap = new Map<any, number>()
|
|
791
|
+
let localIdxCounter = indexToUse // Start from parent index (reuse parent's index for first child)
|
|
792
|
+
for (const ch of children) {
|
|
793
|
+
if (!ch) continue
|
|
794
|
+
if (ch.type === 'JSXElement') {
|
|
795
|
+
const chTag = ch.opening && ch.opening.name && ch.opening.name.type === 'Identifier'
|
|
796
|
+
? ch.opening.name.value
|
|
797
|
+
: undefined
|
|
798
|
+
|
|
799
|
+
if (chTag && allowedTags.has(chTag)) {
|
|
800
|
+
// Check if this child will be preserved as literal HTML
|
|
801
|
+
const chHasAttrs =
|
|
802
|
+
ch.opening &&
|
|
803
|
+
Array.isArray((ch.opening as any).attributes) &&
|
|
804
|
+
(ch.opening as any).attributes.length > 0
|
|
805
|
+
const chChildren = ch.children || []
|
|
806
|
+
const chIsSinglePureText =
|
|
807
|
+
chChildren.length === 1 && (
|
|
808
|
+
chChildren[0]?.type === 'JSXText' ||
|
|
809
|
+
(chChildren[0]?.type === 'JSXExpressionContainer' &&
|
|
810
|
+
getStringLiteralFromExpression(chChildren[0].expression) !== undefined)
|
|
811
|
+
)
|
|
812
|
+
const chWillBePreserved = !chHasAttrs && (!chChildren.length || chIsSinglePureText)
|
|
813
|
+
if (!chWillBePreserved) {
|
|
814
|
+
// Will be indexed, add to local map
|
|
815
|
+
childrenLocalMap.set(ch, localIdxCounter++)
|
|
816
|
+
}
|
|
817
|
+
} else {
|
|
818
|
+
// Non-preserved tag, always indexed
|
|
819
|
+
childrenLocalMap.set(ch, localIdxCounter++)
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
const inner = visitNodes(children, childrenLocalMap.size > 0 ? childrenLocalMap : undefined, false)
|
|
824
|
+
out += `<${indexToUse}>${trimFormattingEdges(inner)}</${indexToUse}>`
|
|
825
|
+
} else {
|
|
826
|
+
// Not in globalSlots, preserve as literal HTML
|
|
827
|
+
const inner = visitNodes(children, undefined, false)
|
|
828
|
+
out += `<${tag}>${trimFormattingEdges(inner)}</${tag}>`
|
|
829
|
+
}
|
|
617
830
|
}
|
|
618
831
|
} else {
|
|
619
832
|
// Decide whether to use local (restarted) indexes for this element's
|
|
@@ -643,8 +856,16 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
643
856
|
? ch.opening.name.value
|
|
644
857
|
: undefined
|
|
645
858
|
if (chTag && allowedTags.has(chTag)) {
|
|
646
|
-
|
|
647
|
-
|
|
859
|
+
// Check if this child will be preserved as literal HTML
|
|
860
|
+
const chHasAttrs =
|
|
861
|
+
ch.opening &&
|
|
862
|
+
Array.isArray((ch.opening as any).attributes) &&
|
|
863
|
+
(ch.opening as any).attributes.length > 0
|
|
864
|
+
const chChildren = ch.children || []
|
|
865
|
+
const chIsSinglePureText = chChildren.length === 1 && chChildren[0]?.type === 'JSXText'
|
|
866
|
+
const chWillBePreserved = !chHasAttrs && (!chChildren.length || chIsSinglePureText)
|
|
867
|
+
if (!chWillBePreserved) {
|
|
868
|
+
// Will be indexed, add to local map
|
|
648
869
|
childrenLocalMap.set(ch, localIdxCounter++)
|
|
649
870
|
}
|
|
650
871
|
} else {
|
|
@@ -672,7 +893,7 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
672
893
|
return out
|
|
673
894
|
}
|
|
674
895
|
|
|
675
|
-
const result = visitNodes(children)
|
|
896
|
+
const result = visitNodes(children, undefined, true)
|
|
676
897
|
|
|
677
898
|
// Final cleanup in correct order:
|
|
678
899
|
// 1. First, handle <br /> followed by whitespace+newline (boundary formatting)
|