i18next-cli 1.26.1 → 1.27.1
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 +2 -0
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/extractor/core/extractor.js +1 -1
- package/dist/cjs/extractor/parsers/jsx-parser.js +1 -1
- package/dist/cjs/heuristic-config.js +1 -1
- package/dist/cjs/syncer.js +1 -1
- package/dist/cjs/utils/file-utils.js +1 -1
- package/dist/esm/cli.js +1 -1
- package/dist/esm/extractor/core/extractor.js +1 -1
- package/dist/esm/extractor/parsers/jsx-parser.js +1 -1
- package/dist/esm/heuristic-config.js +1 -1
- package/dist/esm/syncer.js +1 -1
- package/dist/esm/utils/file-utils.js +1 -1
- package/package.json +2 -1
- package/types/extractor/core/extractor.d.ts.map +1 -1
- package/types/heuristic-config.d.ts.map +1 -1
- package/types/syncer.d.ts.map +1 -1
- package/types/types.d.ts +2 -1
- package/types/types.d.ts.map +1 -1
- package/types/utils/file-utils.d.ts +3 -5
- package/types/utils/file-utils.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -346,6 +346,8 @@ export default defineConfig({
|
|
|
346
346
|
ignore: ['node_modules/**'],
|
|
347
347
|
|
|
348
348
|
// Use '.ts' files with `export default` instead of '.json'
|
|
349
|
+
// Or use 'json5' to enable JSON5 features (comments, trailing commas, formatting are tried to be preserved)
|
|
350
|
+
// if the file ending is .json5 it automatically uses json5 format
|
|
349
351
|
outputFormat: 'ts',
|
|
350
352
|
|
|
351
353
|
// Combine all namespaces into a single file per language (e.g., locales/en.ts)
|
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");var s=require("./types-generator.js"),l=require("./syncer.js"),u=require("./migrator.js"),d=require("./init.js"),g=require("./linter.js"),f=require("./status.js"),p=require("./locize.js"),y=require("./rename-key.js");const m=new e.Command;m.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.
|
|
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"),u=require("./migrator.js"),d=require("./init.js"),g=require("./linter.js"),f=require("./status.js"),p=require("./locize.js"),y=require("./rename-key.js");const m=new e.Command;m.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.27.1"),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.").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=m.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=h(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)}}),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,o)=>{const t=m.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 f.runStatus(n,{detail:e,namespace:o.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 t=m.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=[...h(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()})}}),m.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=m.opts().config,o=await r.ensureConfig(e);await l.runSyncer(o)}),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(d.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 t=m.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 g.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=[...h(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()})}}}),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 o=m.opts().config,t=await r.ensureConfig(o);await p.runLocizeSync(t,e)}),m.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const o=m.opts().config,t=await r.ensureConfig(o);await p.runLocizeDownload(t,e)}),m.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const o=m.opts().config,t=await r.ensureConfig(o);await p.runLocizeMigrate(t,e)}),m.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=m.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)}}),m.parse(process.argv);const h=e=>Array.isArray(e)?e:e?[e]:[],w=e=>e&&"string"==typeof e?[e.replace(/\{\{[^}]+\}\}/g,"*")]:[],x=async(e=[])=>{const o=h(e),n=await Promise.all(o.map(e=>t.glob(e||"",{nodir:!0})));return Array.from(new Set(n.flat()))};exports.program=m;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("ora"),e=require("chalk"),
|
|
1
|
+
"use strict";var t=require("ora"),e=require("chalk"),a=require("@swc/core"),r=require("node:fs/promises"),n=require("node:path"),o=require("./key-finder.js"),s=require("./translation-manager.js"),i=require("../../utils/validation.js"),c=require("../parsers/comment-parser.js"),l=require("../../utils/logger.js"),u=require("../../utils/file-utils.js"),d=require("../../utils/funnel-msg-tracker.js");exports.extract=async function(t,{syncPrimaryWithDefaults:e=!1}={}){t.extract.primaryLanguage||=t.locales[0]||"en",t.extract.secondaryLanguages||=t.locales.filter(e=>e!==t?.extract?.primaryLanguage),t.extract.functions||=["t","*.t"],t.extract.transComponents||=["Trans"];const{allKeys:a,objectKeys:r}=await o.findKeys(t);return s.getTranslations(a,r,t,{syncPrimaryWithDefaults:e})},exports.processFile=async function(t,e,o,s,u,d=new l.ConsoleLogger){try{let l=await r.readFile(t,"utf-8");for(const a of e)try{const e=await(a.onLoad?.(l,t));void 0!==e&&(l=e)}catch(t){d.warn(`Plugin ${a.name} onLoad failed:`,t)}const y=n.extname(t).toLowerCase(),p=".ts"===y||".tsx"===y||".mts"===y||".cts"===y,g=".tsx"===y,m=".jsx"===y;let f;try{f=await a.parse(l,{syntax:p?"typescript":"ecmascript",tsx:g,jsx:m,decorators:!0,dynamicImport:!0,comments:!0})}catch(e){if(".ts"!==y||g)throw new i.ExtractorError("Failed to process file",t,e);try{f=await a.parse(l,{syntax:"typescript",tsx:!0,decorators:!0,dynamicImport:!0,comments:!0}),d.info?.(`Parsed ${t} using TSX fallback`)}catch(e){throw new i.ExtractorError("Failed to process file",t,e)}}s.getVarFromScope=o.getVarFromScope.bind(o),o.setCurrentFile(t,l),o.visit(f),c.extractKeysFromComments(l,s,u,o.getVarFromScope.bind(o))}catch(e){throw new i.ExtractorError("Failed to process file",t,e)}},exports.runExtractor=async function(a,{isWatchMode:c=!1,isDryRun:y=!1,syncPrimaryWithDefaults:p=!1,syncAll:g=!1}={},m=new l.ConsoleLogger){a.extract.primaryLanguage||=a.locales[0]||"en",a.extract.secondaryLanguages||=a.locales.filter(t=>t!==a?.extract?.primaryLanguage),a.extract.functions||=["t","*.t"],a.extract.transComponents||=["Trans"],i.validateExtractorConfig(a);const f=a.plugins||[],x=t("Running i18next key extractor...\n").start();try{const{allKeys:t,objectKeys:i}=await o.findKeys(a,m);x.text=`Found ${t.size} unique keys. Updating translation files...`;const c=await s.getTranslations(t,i,a,{syncPrimaryWithDefaults:p,syncAll:g});let l=!1;for(const t of c)if(t.updated&&(l=!0,!y)){const o=a.extract.outputFormat??(t.path.endsWith(".json5")?"json5":"json"),s="json5"===o?await u.loadRawJson5Content(t.path)??void 0:void 0,i=u.serializeTranslationFile(t.newTranslations,o,a.extract.indentation,s);await r.mkdir(n.dirname(t.path),{recursive:!0}),await r.writeFile(t.path,i),m.info(e.green(`Updated: ${t.path}`))}if(f.length>0){x.text="Running post-extraction plugins...";for(const t of f)await(t.afterSync?.(c,a))}return x.succeed(e.bold("Extraction complete!")),l&&await async function(){if(!await d.shouldShowFunnel("extract"))return;return console.log(e.yellow.bold("\n💡 Tip: Tired of running the extractor manually?")),console.log(' Discover a real-time "push" workflow with `saveMissing` and Locize AI,'),console.log(" where keys are created and translated automatically as you code."),console.log(` Learn more: ${e.cyan("https://www.locize.com/blog/i18next-savemissing-ai-automation")}`),console.log(` Watch the video: ${e.cyan("https://youtu.be/joPsZghT3wM")}`),d.recordFunnelShown("extract")}(),l}catch(t){throw x.fail(e.red("Extraction failed.")),t}};
|
|
@@ -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,s){const r=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,c=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),f="JSXAttribute"===c?.type&&"JSXExpressionContainer"===c.value?.type&&"ObjectExpression"===c.value.expression.type?c.value.expression:void 0,y=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),g=!!y,d=i.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 h=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let v;v="JSXAttribute"===h?.type?n(h):void 0,f&&(void 0===v&&(v=e.getObjectPropValue(f,"ns")),void 0===x&&(x=e.getObjectPropValueExpression(f,"context")));const S=function(e,n){if(!e||0===e.length)return"";const i=new Set(n.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),s=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(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&&s(e[l]);)l++;for(;u>=l&&s(e[u]);)u--;const c=l<=u?e.slice(l,u+1):[],f=c.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<c.length;e++){const p=c[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}if(void 0!==t(p.expression))continue;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"),r=c[e-1],o=c[e+1];if(t){const t=c[e+2];if(o&&"JSXText"===o.type&&s(o)&&t&&("JSXElement"===t.type||"JSXFragment"===t.type)){const t=c[e-1],n=c[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue;const i=!o||"JSXText"===o.type&&!s(o);if(r&&"JSXText"===r.type&&i){const e=n[n.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(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,s=p.children||[],o=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==t(s[0].expression)),l=!s.length,u=o;i&&!o?(n.push(p),r(p.children||[],n,!0)):l?n.push(p):u||("p"===e&&a?(n.push(p),r(p.children||[],n,!0,!1)):r(p.children||[],n,!1,!1));continue}n.push(p),r(p.children||[],n,!0);continue}"JSXFragment"!==p.type||r(p.children||[],n,o)}else{if(o&&!f)continue;if(o&&s(p))continue;if(s(p)){const t=c[e-1],i=c[e+1];if(t&&("JSXElement"===t.type||"JSXFragment"===t.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const s=n[n.length-1],r=c[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+p.value;continue}}}if(o&&f&&0===e)continue;n.push(p)}}}const o=[];function p(e){if(!e||!e.length)return!1;let t=!1;for(const n of e)if(n)if("JSXElement"!==n.type){if("JSXExpressionContainer"===n.type&&-1!==o.indexOf(n))return t;if("JSXText"===n.type&&-1!==o.indexOf(n)){if(s(n))continue;if(!t)return!0;const i=e.indexOf(n);if(e.slice(i+1).some(e=>e&&"JSXElement"===e.type))return!0}}else t=!0;return!1}r(e,o,!1,!0);const a=new Set,l=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function u(e,n,r=!1){if(!e||0===e.length)return"";let c="";const f=e=>{if(!e)return-1;if(n&&n.has(e))return n.get(e);if(n)for(const[t,i]of n.entries())try{if(t&&e&&t.span&&e.span&&t.span.start===e.span.start&&t.span.end===e.span.end)return i}catch(e){}return o.indexOf(e)};let y=0;for(let n=0;n<e.length;n++){const g=e[n];if(g){if("JSXText"===g.type){if(s(g))continue;const t=e[n+1],r=e[n-1];if(r&&"JSXElement"===r.type){const t="Identifier"===r.opening?.name?.type?r.opening.name.value:void 0,s=t&&i.has(t),o=0===(r.children||[]).length;if(s&&o&&/^\s*\n\s*/.test(g.value)){const e=g.value.replace(/^\s*\n\s*/,"");if(e){c+=e;continue}continue}if(!s&&/^\s*\n\s*/.test(g.value)){const t=g.value.replace(/^\s*\n\s*/,"");if(t){if(r&&"JSXElement"===r.type&&Array.isArray(r.children)&&0===r.children.length){c+=t;continue}const i=e[n-2];if(i&&"JSXText"===i.type){const e=i.value.replace(/\n\s*$/,""),n=/[A-Za-z0-9]$/.test(e),s=/^[A-Za-z0-9]/.test(t),r=/^[a-z]/.test(t);if(n&&s&&r){c+=t;continue}}c+=" "+t;continue}continue}}if(/\n\s*$/.test(g.value)&&t&&"JSXElement"===t.type){const r=g.value.replace(/\n\s*$/,"");if(r.trim()){const o="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,p=o&&i.has(o),l=(t.children||[]).length>0,u=/\s\n/.test(g.value),f=e[n+2],y=f&&"JSXText"===f.type&&!s(f)&&/[a-zA-Z0-9]/.test(f.value),d=!!(t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0),x=/^\s/.test(r)&&!/^\n/.test(r),h=r.trim(),v=x?" "+h:h,S=/[A-Za-z0-9]$/.test(h),m=f&&"string"==typeof f.value&&/^[A-Za-z0-9]/.test(f.value.trim()),J=f&&"string"==typeof f.value&&/^[a-z]/.test(f.value.trim()),X=f&&"string"==typeof f.value&&/^\s/.test(f.value)&&!/^\n/.test(f.value),$=d&&l&&y&&!(S&&m&&J&&!u&&!x&&!X),E=f&&"string"==typeof f.value&&/^[,;:!?.]/.test(f.value.trim()),b=$&&!E;S&&m&&J&&!u&&!x&&!X&&a.add(t),c+=u||p&&l||!p&&y&&x||b?v+" ":v;continue}}c+=g.value;continue}if("JSXExpressionContainer"===g.type){const e=g.expression;if(!e)continue;const n=t(e);if(void 0!==n)c+=n;else if("Identifier"===e.type)c+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?c+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?c+=`{{${t.value}}}`:c+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?c+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?c+=`{{${e.callee.value}}}`:c+="{{value}}";continue}if("JSXElement"===g.type){let a;g.opening&&g.opening.name&&"Identifier"===g.opening.name.type&&(a=g.opening.name.value);const d=r?y:void 0;if(r&&"JSXElement"===g.type&&y++,a&&i.has(a)){const x=g.opening&&Array.isArray(g.opening.attributes)&&g.opening.attributes.length>0,h=g.children||[],v=h.length>0,S=1===h.length&&("JSXText"===h[0]?.type||"JSXExpressionContainer"===h[0]?.type&&void 0!==t(h[0].expression)),m="p"===a;let J=0;if(m&&r&&(J=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!v||S){const t=S?u(h,void 0):"";if(""!==String(t).trim())if(i.has(a)&&"p"!==a)c+=`<${a}>${l(t)}</${a}>`;else if(m&&r&&J>1)c+=`<${a}>${l(t)}</${a}>`;else if(m){const e=r?y-1:o.indexOf(g);c+=`<${e}>${l(t)}</${e}>`}else{const e=r?y-1:o.indexOf(g);c+=`<${e}>${l(t)}</${e}>`}else{const t=e[n-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(c=c.replace(/\s+$/,"")),c+=`<${a} />`}}else if(x&&!S){const e=h,n=f(g);if(p(e)){const s=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||[],o=1===i.length&&("JSXText"===i[0]?.type||"JSXExpressionContainer"===i[0]?.type&&void 0!==t(i[0].expression));!e&&(!i.length||o)||s.set(n,r++)}else s.set(n,r++)}const o=u(e,s.size?s:void 0);c+=`<${n}>${l(o)}</${n}>`}else{const e=new Map;let s=0;for(const n of h)if(n&&"JSXElement"===n.type){const r=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(r&&i.has(r)){const i=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,r=n.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression));!i&&(!r.length||o)||e.set(n,s++)}else e.set(n,s++)}const r=u(h,e.size?e:void 0);c+=`<${n}>${l(r)}</${n}>`}}else{const e=o.indexOf(g);if(-1!==e){const n=void 0!==d?d:e;if((()=>{let e=!1;for(const t of h)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(s(t))continue;if(!e)return!0;if(h.slice(h.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){if(0===n){const e=u(h,void 0,!1);c+=`<${n}>${l(e)}</${n}>`;continue}const e=new Map;let s=0;for(const n of h)if(n&&"JSXElement"===n.type){const r=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(r&&i.has(r)){const i=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,r=n.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression));!i&&(!r.length||o)||e.set(n,s++)}else e.set(n,s++)}const r=u(h,e.size?e:void 0,!1);c+=`<${n}>${l(r)}</${n}>`;continue}const r=new Map;let p=0;for(const e of h)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||[],s=1===i.length&&("JSXText"===i[0]?.type||"JSXExpressionContainer"===i[0]?.type&&void 0!==t(i[0].expression));!n&&(!i.length||s)||r.set(e,p++)}else r.set(e,p++)}const a=u(h,r.size>0?r:void 0,!1);c+=`<${n}>${l(a)}</${n}>`}else{const e=u(h,void 0,!1);c+=`<${a}>${l(e)}</${a}>`}}}else{const e=g.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(g),n=u(e,void 0);c+=`<${t}>${l(n)}</${t}>`}else{const t=new Map,n=f(g);let s=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||[],r=1===i.length&&"JSXText"===i[0]?.type;!e&&(!i.length||r)||t.set(n,s++)}else t.set(n,s++)}const r=u(e,t.size?t:void 0);c+=`<${n}>${l(r)}</${n}>`}}continue}"JSXFragment"!==g.type||(c+=u(g.children||[]))}}return c}const c=u(e,void 0,!0),f=String(c).replace(/<br \/>\s*\n\s*/g,"<br />"),y=String(f),g=new Set;if(a&&a.size>0)for(let e=0;e<o.length;e++)a.has(o[e])&&g.add(e);let d=String(y);if(g&&g.size>0)for(const e of g)try{d=d.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),d=d.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}d=d.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return g.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),d=d.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return g.has(i)?`${t}<${n}`:`${t} <${n}`}),d=d.replace(/\s*\n\s*/g," "),d=d.replace(/\s+/g," "),d=d.replace(/\s+([,;:!?.])/g,"$1");const x=d.trim();let h=String(x);if(g&&g.size>0)for(const e of g)try{h=h.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),h=h.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<o.length;e++){const t=o[e];if(!t||"JSXElement"!==t.type)continue;const n=o[e-1],i=o[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),p=s.replace(/\n\s*$/,""),a=/[A-Za-z0-9]$/.test(p),l=/^[A-Za-z0-9]/.test(r.trim()),u=/^[a-z]/.test(r.trim()),c=/\s\n/.test(s),f=r&&/^\s/.test(r)&&!/^\n/.test(r);if(a&&l&&u&&!c&&!f){const t=e;h=h.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}function v(e){if(!e||!e.includes("<"))return e;function t(e){const t=[],n=[];let i=0;const s=/<\/?(\d+)>|<[^>]+>/g;let r;for(;r=s.exec(e);){const o=r[0],p=r.index;if(p>i){const s={type:"text",text:e.slice(i,p)};n.length?n[n.length-1].node.children.push(s):t.push(s)}const a=/^<\/(\d+)>$/.exec(o),l=/^<(\d+)>$/.exec(o);if(l){const e=Number(l[1]),i={type:"ph",idx:e,children:[]};n.length?n[n.length-1].node.children.push(i):t.push(i),n.push({node:i,idx:e})}else if(a)n.length&&n.pop();else{const e={type:"text",text:o};n.length?n[n.length-1].node.children.push(e):t.push(e)}i=s.lastIndex}if(i<e.length){const s={type:"text",text:e.slice(i)};n.length?n[n.length-1].node.children.push(s):t.push(s)}return t}function n(e,t=null){let s="";for(const r of e)if("text"===r.type)s+=r.text;else{const e=r.children.filter(e=>"ph"===e.type),p=e.map(e=>e.idx);if(!(p.length<=1||p.every((e,t)=>0===t||e===p[t-1]+1))){const e=r.children.map(e=>"text"===e.type?e.text:`<${e.idx}>${n(e.children,e.idx)}</${e.idx}>`).join("");s+=`<${r.idx}>${e}</${r.idx}>`;continue}const a=new Map;let l=0;try{const n=o[r.idx];if(n&&n.span){const s=n.span.start,p=n.span.end;let a=-1;for(let e=r.idx+1;e<o.length;e++){const t=o[e];if(t&&t.span&&(t.span.start>=s&&t.span.end<=p&&"JSXElement"===t.type)){a=e;break}}let u=0;if(-1!==a)for(let e=r.idx+1;e<a;e++){const t=o[e];t&&t.span&&(t.span.start>=s&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||u++))}else for(let e=r.idx+1;e<o.length;e++){const t=o[e];t&&t.span&&(t.span.start>=s&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||u++))}if("number"==typeof r.idx){const n=o[r.idx],s=n?.opening?.name?.value,p=s&&i.has(s),a=null===t;l=1===e.length&&(!a||p||0!==e[0].idx||"a"!==s&&"p"!==s)?0===r.idx?0:Math.max(1,u+1):0}}}catch(e){}for(const t of e)a.has(t.idx)||a.set(t.idx,l++);const u=r.children.map(e=>{if("text"===e.type)return e.text;const t=e.idx,i=a.has(t)?a.get(t):t;return`<${i}>${n(e.children,i)}</${i}>`}).join("");s+=`<${r.idx}>${u}</${r.idx}>`}return s}try{return n(t(e))}catch(t){return e}}return h=v(h),h.trim()}(i.children,s);let m;const J="JSXAttribute"===o?.type?n(o):void 0;if(void 0!==J)m=J;else{const e=s.extract.defaultValue;m="string"==typeof e?e:""}let X,$;if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(X=r.value,$=X.value,!$||""===$.trim())return null;if(v&&"StringLiteral"===X.type){const e=s.extract.nsSeparator??":",t=X.value;if(e&&t.startsWith(`${v}${e}`)){if($=t.slice(`${v}${e}`.length),!$||""===$.trim())return null;X={...X,value:$}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(X=r.value.expression);if(!X)return null}return o||!$||S.trim()?!o&&S.trim()&&(m=S):m=$,{keyExpression:X,serializedChildren:S,ns:v,defaultValue:m,hasCount:u,isOrdinal:g,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)}};
|
|
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,s){const r=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,c=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),f="JSXAttribute"===c?.type&&"JSXExpressionContainer"===c.value?.type&&"ObjectExpression"===c.value.expression.type?c.value.expression:void 0,y=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),g=!!y,d=i.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 h=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let v;v="JSXAttribute"===h?.type?n(h):void 0,f&&(void 0===v&&(v=e.getObjectPropValue(f,"ns")),void 0===x&&(x=e.getObjectPropValueExpression(f,"context")));const S=function(e,n){if(!e||0===e.length)return"";const i=new Set(n.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),s=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function r(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&&s(e[l]);)l++;for(;u>=l&&s(e[u]);)u--;const c=l<=u?e.slice(l,u+1):[],f=c.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<c.length;e++){const l=c[e];if(l)if("JSXText"!==l.type){if("JSXExpressionContainer"===l.type){if(o&&!f&&l.expression){const e=l.expression.type;if("ObjectExpression"===e){const e=l.expression.properties&&l.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}if(void 0!==t(l.expression))continue;if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const i=t(l.expression);if(void 0!==i){const t=/^\s*$/.test(i)&&!i.includes("\n"),r=c[e-1],o=c[e+1];if(t){const t=c[e+2];if(o&&"JSXText"===o.type&&s(o)&&t&&("JSXElement"===t.type||"JSXFragment"===t.type)){const t=c[e-1],n=c[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue;const i=!o||"JSXText"===o.type&&!s(o);if(r&&"JSXText"===r.type&&i){const e=n[n.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+l.expression.value;continue}}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue}}n.push(l);continue}if("JSXElement"===l.type){const e=l.opening&&l.opening.name&&"Identifier"===l.opening.name.type?l.opening.name.value:void 0;if(e&&i.has(e)){const o=l.opening&&Array.isArray(l.opening.attributes)&&l.opening.attributes.length>0,u=l.children||[],f=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==t(u[0].expression)),y=!u.length,g=f;if(o&&!f)n.push(l),r(l.children||[],n,!0);else if(y)n.push(l);else if(!g)if("p"===e){const e=(u||[]).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,s=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,r=e.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression));return!(n&&i.has(n)&&!s&&(0===r.length||o))}),o=p&&c.some(e=>e&&e!==l&&"JSXText"===e.type&&!s(e));a||p&&e&&!o?(n.push(l),r(l.children||[],n,!0,!1)):r(l.children||[],n,!1,!1)}else r(l.children||[],n,!1,!1);continue}n.push(l),r(l.children||[],n,!0);continue}"JSXFragment"!==l.type||r(l.children||[],n,o)}else{if(o&&!f)continue;if(o&&s(l))continue;if(s(l)){const t=c[e-1],i=c[e+1];if(t&&("JSXElement"===t.type||"JSXFragment"===t.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const s=n[n.length-1],r=c[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+l.value;continue}}}if(o&&f&&0===e)continue;n.push(l)}}}const o=[];function p(e){if(!e||!e.length)return!1;let t=!1;for(const n of e)if(n)if("JSXElement"!==n.type){if("JSXExpressionContainer"===n.type&&-1!==o.indexOf(n))return t;if("JSXText"===n.type&&-1!==o.indexOf(n)){if(s(n))continue;if(!t)return!0;const i=e.indexOf(n);if(e.slice(i+1).some(e=>e&&"JSXElement"===e.type))return!0}}else t=!0;return!1}r(e,o,!1,!0);const a=new Set,l=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function u(e,n,r=!1){if(!e||0===e.length)return"";let c="";const f=e=>{if(!e)return-1;if(n&&n.has(e))return n.get(e);if(n)for(const[t,i]of n.entries())try{if(t&&e&&t.span&&e.span&&t.span.start===e.span.start&&t.span.end===e.span.end)return i}catch(e){}return o.indexOf(e)};let y=0;for(let n=0;n<e.length;n++){const g=e[n];if(g){if("JSXText"===g.type){if(s(g))continue;const t=e[n+1],r=e[n-1];if(r&&"JSXElement"===r.type){const t="Identifier"===r.opening?.name?.type?r.opening.name.value:void 0,s=t&&i.has(t),o=0===(r.children||[]).length;if(s&&o&&/^\s*\n\s*/.test(g.value)){const e=g.value.replace(/^\s*\n\s*/,"");if(e){c+=e;continue}continue}if(!s&&/^\s*\n\s*/.test(g.value)){const t=g.value.replace(/^\s*\n\s*/,"");if(t){if(r&&"JSXElement"===r.type&&Array.isArray(r.children)&&0===r.children.length){c+=t;continue}const i=e[n-2];if(i&&"JSXText"===i.type){const e=i.value.replace(/\n\s*$/,""),n=/[A-Za-z0-9]$/.test(e),s=/^[A-Za-z0-9]/.test(t),r=/^[a-z]/.test(t);if(n&&s&&r){c+=t;continue}}c+=" "+t;continue}continue}}if(/\n\s*$/.test(g.value)&&t&&"JSXElement"===t.type){const r=g.value.replace(/\n\s*$/,"");if(r.trim()){const o="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,p=o&&i.has(o),l=(t.children||[]).length>0,u=/\s\n/.test(g.value),f=e[n+2],y=f&&"JSXText"===f.type&&!s(f)&&/[a-zA-Z0-9]/.test(f.value),d=!!(t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0),x=/^\s/.test(r)&&!/^\n/.test(r),h=r.trim(),v=x?" "+h:h,S=/[A-Za-z0-9]$/.test(h),m=f&&"string"==typeof f.value&&/^[A-Za-z0-9]/.test(f.value.trim()),J=f&&"string"==typeof f.value&&/^[a-z]/.test(f.value.trim()),X=f&&"string"==typeof f.value&&/^\s/.test(f.value)&&!/^\n/.test(f.value),$=d&&l&&y&&!(S&&m&&J&&!u&&!x&&!X),E=f&&"string"==typeof f.value&&/^[,;:!?.]/.test(f.value.trim()),b=$&&!E;S&&m&&J&&!u&&!x&&!X&&a.add(t),c+=u||p&&l||!p&&y&&x||b?v+" ":v;continue}}c+=g.value;continue}if("JSXExpressionContainer"===g.type){const e=g.expression;if(!e)continue;const n=t(e);if(void 0!==n)c+=n;else if("Identifier"===e.type)c+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?c+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?c+=`{{${t.value}}}`:c+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?c+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?c+=`{{${e.callee.value}}}`:c+="{{value}}";continue}if("JSXElement"===g.type){let a;g.opening&&g.opening.name&&"Identifier"===g.opening.name.type&&(a=g.opening.name.value);const d=r?y:void 0;if(r&&"JSXElement"===g.type&&y++,a&&i.has(a)){const x=g.opening&&Array.isArray(g.opening.attributes)&&g.opening.attributes.length>0,h=g.children||[],v=h.length>0,S=1===h.length&&("JSXText"===h[0]?.type||"JSXExpressionContainer"===h[0]?.type&&void 0!==t(h[0].expression)),m="p"===a;let J=0;if(m&&r&&(J=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!v||S){const t=S?u(h,void 0):"";if(""!==String(t).trim())if(i.has(a)&&"p"!==a)c+=`<${a}>${l(t)}</${a}>`;else if(m&&r&&J>1)c+=`<${a}>${l(t)}</${a}>`;else if(m){const e=r?y-1:o.indexOf(g);c+=`<${e}>${l(t)}</${e}>`}else{const e=r?y-1:o.indexOf(g);c+=`<${e}>${l(t)}</${e}>`}else{const t=e[n-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(c=c.replace(/\s+$/,"")),c+=`<${a} />`}}else if(x&&!S){const e=h,n=f(g);if(p(e)){const s=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||[],o=1===i.length&&("JSXText"===i[0]?.type||"JSXExpressionContainer"===i[0]?.type&&void 0!==t(i[0].expression));!e&&(!i.length||o)||s.set(n,r++)}else s.set(n,r++)}const o=u(e,s.size?s:void 0);c+=`<${n}>${l(o)}</${n}>`}else{const e=new Map;let s=0;for(const n of h)if(n&&"JSXElement"===n.type){const r=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(r&&i.has(r)){const i=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,r=n.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression));!i&&(!r.length||o)||e.set(n,s++)}else e.set(n,s++)}const r=u(h,e.size?e:void 0);c+=`<${n}>${l(r)}</${n}>`}}else{const e=o.indexOf(g);if(-1!==e){const n=void 0!==d?d:e;if((()=>{let e=!1;for(const t of h)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(s(t))continue;if(!e)return!0;if(h.slice(h.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){if(0===n){const e=u(h,void 0,!1);c+=`<${n}>${l(e)}</${n}>`;continue}const e=new Map;let s=0;for(const n of h)if(n&&"JSXElement"===n.type){const r=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(r&&i.has(r)){const i=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,r=n.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression));!i&&(!r.length||o)||e.set(n,s++)}else e.set(n,s++)}const r=u(h,e.size?e:void 0,!1);c+=`<${n}>${l(r)}</${n}>`;continue}const r=new Map;let p=0;for(const e of h)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||[],s=1===i.length&&("JSXText"===i[0]?.type||"JSXExpressionContainer"===i[0]?.type&&void 0!==t(i[0].expression));!n&&(!i.length||s)||r.set(e,p++)}else r.set(e,p++)}const a=u(h,r.size>0?r:void 0,!1);c+=`<${n}>${l(a)}</${n}>`}else{const e=u(h,void 0,!1);c+=`<${a}>${l(e)}</${a}>`}}}else{const e=g.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(g),n=u(e,void 0);c+=`<${t}>${l(n)}</${t}>`}else{const t=new Map,n=f(g);let s=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||[],r=1===i.length&&"JSXText"===i[0]?.type;!e&&(!i.length||r)||t.set(n,s++)}else t.set(n,s++)}const r=u(e,t.size?t:void 0);c+=`<${n}>${l(r)}</${n}>`}}continue}"JSXFragment"!==g.type||(c+=u(g.children||[]))}}return c}const c=u(e,void 0,!0),f=String(c).replace(/<br \/>\s*\n\s*/g,"<br />"),y=String(f),g=new Set;if(a&&a.size>0)for(let e=0;e<o.length;e++)a.has(o[e])&&g.add(e);let d=String(y);if(g&&g.size>0)for(const e of g)try{d=d.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),d=d.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}d=d.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return g.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),d=d.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return g.has(i)?`${t}<${n}`:`${t} <${n}`}),d=d.replace(/\s*\n\s*/g," "),d=d.replace(/\s+/g," "),d=d.replace(/\s+([,;:!?.])/g,"$1");const x=d.trim();let h=String(x);if(g&&g.size>0)for(const e of g)try{h=h.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),h=h.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<o.length;e++){const t=o[e];if(!t||"JSXElement"!==t.type)continue;const n=o[e-1],i=o[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),p=s.replace(/\n\s*$/,""),a=/[A-Za-z0-9]$/.test(p),l=/^[A-Za-z0-9]/.test(r.trim()),u=/^[a-z]/.test(r.trim()),c=/\s\n/.test(s),f=r&&/^\s/.test(r)&&!/^\n/.test(r);if(a&&l&&u&&!c&&!f){const t=e;h=h.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}function v(e){if(!e||!e.includes("<"))return e;function t(e){const t=[],n=[];let i=0;const s=/<\/?(\d+)>|<[^>]+>/g;let r;for(;r=s.exec(e);){const o=r[0],p=r.index;if(p>i){const s={type:"text",text:e.slice(i,p)};n.length?n[n.length-1].node.children.push(s):t.push(s)}const a=/^<\/(\d+)>$/.exec(o),l=/^<(\d+)>$/.exec(o);if(l){const e=Number(l[1]),i={type:"ph",idx:e,children:[]};n.length?n[n.length-1].node.children.push(i):t.push(i),n.push({node:i,idx:e})}else if(a)n.length&&n.pop();else{const e={type:"text",text:o};n.length?n[n.length-1].node.children.push(e):t.push(e)}i=s.lastIndex}if(i<e.length){const s={type:"text",text:e.slice(i)};n.length?n[n.length-1].node.children.push(s):t.push(s)}return t}function n(e,t=null){let s="";for(const r of e)if("text"===r.type)s+=r.text;else{const e=r.children.filter(e=>"ph"===e.type),p=e.map(e=>e.idx);if(!(p.length<=1||p.every((e,t)=>0===t||e===p[t-1]+1))){const e=r.children.map(e=>"text"===e.type?e.text:`<${e.idx}>${n(e.children,e.idx)}</${e.idx}>`).join("");s+=`<${r.idx}>${e}</${r.idx}>`;continue}const a=new Map;let l=0;try{const n=o[r.idx];if(n&&n.span){const s=n.span.start,p=n.span.end;let a=-1;for(let e=r.idx+1;e<o.length;e++){const t=o[e];if(t&&t.span&&(t.span.start>=s&&t.span.end<=p&&"JSXElement"===t.type)){a=e;break}}let u=0;if(-1!==a)for(let e=r.idx+1;e<a;e++){const t=o[e];t&&t.span&&(t.span.start>=s&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||u++))}else for(let e=r.idx+1;e<o.length;e++){const t=o[e];t&&t.span&&(t.span.start>=s&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||u++))}if("number"==typeof r.idx){const n=o[r.idx],s=n?.opening?.name?.value,p=s&&i.has(s),a=null===t;1===e.length?(l=Math.max(1,u+1),!a||p||0!==e[0].idx||"a"!==s&&"p"!==s||(l=0)):l=0}}}catch(e){}for(const t of e)a.has(t.idx)||a.set(t.idx,l++);const u=r.children.map(e=>{if("text"===e.type)return e.text;const t=e.idx,i=a.has(t)?a.get(t):t;return`<${i}>${n(e.children,i)}</${i}>`}).join("");s+=`<${r.idx}>${u}</${r.idx}>`}return s}try{return n(t(e))}catch(t){return e}}return h=v(h),h.trim()}(i.children,s);let m;const J="JSXAttribute"===o?.type?n(o):void 0;if(void 0!==J)m=J;else{const e=s.extract.defaultValue;m="string"==typeof e?e:""}let X,$;if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(X=r.value,$=X.value,!$||""===$.trim())return null;if(v&&"StringLiteral"===X.type){const e=s.extract.nsSeparator??":",t=X.value;if(e&&t.startsWith(`${v}${e}`)){if($=t.slice(`${v}${e}`.length),!$||""===$.trim())return null;X={...X,value:$}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(X=r.value.expression);if(!X)return null}return o||!$||S.trim()?!o&&S.trim()&&(m=S):m=$,{keyExpression:X,serializedChildren:S,ns:v,defaultValue:m,hasCount:u,isOrdinal:g,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)}};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var s=require("glob"),e=require("node:fs/promises"),n=require("node:path");const o=["public/locales/dev/*.json","locales/dev/*.json","src/locales/dev/*.json","src/assets/locales/dev/*.json","app/i18n/locales/dev/*.json","src/i18n/locales/dev/*.json","public/locales/en/*.json","locales/en/*.json","src/locales/en/*.json","src/assets/locales/en/*.json","app/i18n/locales/en/*.json","src/i18n/locales/en/*.json","public/locales/en-*/*.json","locales/en-*/*.json","src/locales/en-*/*.json","src/assets/locales/en-*/*.json","app/i18n/locales/en-*/*.json","src/i18n/locales/en-*/*.json"];exports.detectConfig=async function(){for(const l of o){const o=await s.glob(l,{ignore:"node_modules/**"});if(o.length>0){const s=o[0],l=n.dirname(n.dirname(s)),
|
|
1
|
+
"use strict";var s=require("glob"),e=require("node:fs/promises"),n=require("node:path");const o=["public/locales/dev/*.json","locales/dev/*.json","src/locales/dev/*.json","src/assets/locales/dev/*.json","app/i18n/locales/dev/*.json","src/i18n/locales/dev/*.json","public/locales/en/*.json","locales/en/*.json","src/locales/en/*.json","src/assets/locales/en/*.json","app/i18n/locales/en/*.json","src/i18n/locales/en/*.json","public/locales/en/*.json5","locales/en/*.json5","src/locales/en/*.json5","src/assets/locales/en/*.json5","app/i18n/locales/en/*.json5","src/i18n/locales/en/*.json5","public/locales/en-*/*.json","locales/en-*/*.json","src/locales/en-*/*.json","src/assets/locales/en-*/*.json","app/i18n/locales/en-*/*.json","src/i18n/locales/en-*/*.json"];exports.detectConfig=async function(){for(const l of o){const o=await s.glob(l,{ignore:"node_modules/**"});if(o.length>0){const s=o.find(s=>".json5"===n.extname(s))||o[0],l=n.dirname(n.dirname(s)),c=n.extname(s);let a="json";".ts"===c?a="ts":".js"===c?a="js":".json5"===c&&(a="json5");try{let s=(await e.readdir(l)).filter(s=>/^(dev|[a-z]{2}(-[A-Z]{2})?)$/.test(s));if(s.length>0)return s.sort(),s.includes("dev")&&(s=["dev",...s.filter(s=>"dev"!==s)]),s.includes("en")&&(s=["en",...s.filter(s=>"en"!==s)]),{locales:s,extract:{input:["src/**/*.{js,jsx,ts,tsx}","app/**/*.{js,jsx,ts,tsx}","pages/**/*.{js,jsx,ts,tsx}","components/**/*.{js,jsx,ts,tsx}"],output:n.join(l,"{{language}}",`{{namespace}}${c}`),outputFormat:a,primaryLanguage:s.includes("en")?"en":s[0]}}}catch{continue}}}return null};
|
package/dist/cjs/syncer.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("chalk"),t=require("glob"),o=require("node:fs/promises"),n=require("node:path"),r=require("ora"),a=require("./utils/default-value.js"),l=require("./utils/file-utils.js"),i=require("./utils/funnel-msg-tracker.js"),s=require("./utils/nested-object.js");exports.runSyncer=async function(c){const u=r("Running i18next locale synchronizer...\n").start();try{const r=c.extract.primaryLanguage||c.locales[0]||"en",d=c.locales.filter(e=>e!==r),{output:f,keySeparator:y=".",outputFormat:g="json",indentation:h=2,defaultValue:p=""}=c.extract,w=[];let m=!1;const S=l.getOutputPath(f,r,"*"),
|
|
1
|
+
"use strict";var e=require("chalk"),t=require("glob"),o=require("node:fs/promises"),n=require("node:path"),r=require("ora"),a=require("./utils/default-value.js"),l=require("./utils/file-utils.js"),i=require("./utils/funnel-msg-tracker.js"),s=require("./utils/nested-object.js");exports.runSyncer=async function(c){const u=r("Running i18next locale synchronizer...\n").start();try{const r=c.extract.primaryLanguage||c.locales[0]||"en",d=c.locales.filter(e=>e!==r),{output:f,keySeparator:y=".",outputFormat:g="json",indentation:h=2,defaultValue:p=""}=c.extract,w=[];let m=!1;const S=l.getOutputPath(f,r,"*"),j=await t.glob(S);if(0===j.length)return void u.warn(`No translation files found for primary language "${r}". Nothing to sync.`);for(const t of j){const r=n.basename(t).split(".")[0],i=await l.loadTranslationFile(t);if(!i){w.push(` ${e.yellow("-")} Could not read primary file: ${t}`);continue}const u=s.getNestedKeys(i,y??".");for(const t of d){const d=l.getOutputPath(f,t,r),S=n.resolve(process.cwd(),d),j=await l.loadTranslationFile(S)||{},q={};for(const e of u){const o=s.getNestedValue(i,e,y??"."),n=s.getNestedValue(j,e,y??".")??a.resolveDefaultValue(p,e,r,t,o);s.setNestedValue(q,e,n,y??".")}const v=JSON.stringify(j);if(JSON.stringify(q)!==v){m=!0;const t=c.extract.outputFormat??(S.endsWith(".json5")?"json5":g),r="json5"===t?await l.loadRawJson5Content(S)??void 0:void 0,a=l.serializeTranslationFile(q,t,h,r);await o.mkdir(n.dirname(S),{recursive:!0}),await o.writeFile(S,a),w.push(` ${e.green("✓")} Synchronized: ${d}`)}else w.push(` ${e.gray("-")} Already in sync: ${d}`)}}u.succeed(e.bold("Synchronization complete!")),w.forEach(e=>console.log(e)),m?await async function(){if(!await i.shouldShowFunnel("syncer"))return;return console.log(e.green.bold("\n✅ Sync complete.")),console.log(e.yellow("🚀 Ready to collaborate with translators? Move your files to the cloud.")),console.log(` Get started with the official TMS for i18next: ${e.cyan("npx i18next-cli locize-migrate")}`),i.recordFunnelShown("syncer")}():console.log(e.green.bold("\n✅ All locales are already in sync."))}catch(t){u.fail(e.red("Synchronization failed.")),console.error(t)}};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("node:fs/promises"),t=require("node:path"),r=require("jiti"),n=require("../config.js");exports.getOutputPath=function(e,r,n){if(!e)return t.normalize(`locales/${r}/${n??"translation"}.json`);if("function"==typeof e)try{const
|
|
1
|
+
"use strict";var e=require("node:fs/promises"),t=require("node:path"),r=require("jiti"),n=require("../config.js"),s=require("@croct/json5-parser");exports.getOutputPath=function(e,r,n){if(!e)return t.normalize(`locales/${r}/${n??"translation"}.json`);if("function"==typeof e)try{const s=String(e(r,n));return t.normalize(s.replace(/\/\/+/g,"/"))}catch{return t.normalize(`locales/${r}/${n??"translation"}.json`)}let s=String(e);return s=s.replace(/\{\{language\}\}|\{\{lng\}\}/g,r),s=null!=n?s.replace(/\{\{namespace\}\}/g,n):s.replace(/\/?\{\{namespace\}\}/g,""),s=s.replace(/\/\/+/g,"/"),t.normalize(s)},exports.loadRawJson5Content=async function(r){const n=t.resolve(process.cwd(),r);try{return await e.access(n),await e.readFile(n,"utf-8")}catch{return null}},exports.loadTranslationFile=async function(a){const o=t.resolve(process.cwd(),a);try{await e.access(o)}catch{return null}try{const a=t.extname(o).toLowerCase();if(".json5"===a){const t=await e.readFile(o,"utf-8");return s.JsonParser.parse(t,s.JsonObjectNode).toJSON()}if(".json"===a){const t=await e.readFile(o,"utf-8");return JSON.parse(t)}if(".ts"===a||".js"===a){const e=await n.getTsConfigAliases(),t=r.createJiti(process.cwd(),{alias:e,interopDefault:!0});return await t.import(o,{default:!0})}return null}catch(e){return console.warn(`Could not parse translation file ${a}:`,e),null}},exports.serializeTranslationFile=function(e,t="json",r=2,n){const a=JSON.stringify(e,null,r);switch(t){case"json5":if(n){const t=s.JsonParser.parse(n,s.JsonObjectNode);return t.update(e),t.toString({object:{indentationSize:Number(r)??2}})}return s.JsonParser.parse(a,s.JsonObjectNode).toString({object:{indentationSize:Number(r)??2}});case"js":case"js-esm":return`export default ${a};\n`;case"js-cjs":return`module.exports = ${a};\n`;case"ts":return`export default ${a} as const;\n`;default:return`${a}\n`}};
|
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 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 f}from"./migrator.js";import{runInit as
|
|
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 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"@croct/json5-parser";import{runTypesGenerator as l}from"./types-generator.js";import{runSyncer as p}from"./syncer.js";import{runMigrator as f}from"./migrator.js";import{runInit as m}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";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.27.1"),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 a(t),r=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 r(),o.watch){console.log("\nWatching for changes...");const o=await v(i.extract.input),t=j(i.extract.ignore),a=k(i.extract.output),c=[...t,...a].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}`),r()})}}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 r(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 g(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 a(t),r=()=>l(i);if(await r(),o.watch){console.log("\nWatching for changes...");const o=await v(i.types?.input||[]),t=[...j(i.extract?.ignore)].filter(Boolean),a=o.filter(o=>!t.some(e=>n(o,e,{dot:!0})));e.watch(a,{persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),r()})}}),x.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const o=x.opts().config,e=await a(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 f(o)}),x.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(m),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,a=async()=>{let o=await r(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 d(o)};if(await a(),o.watch){console.log("\nWatching for changes...");const o=await r(t);if(o?.extract?.input){const t=await v(o.extract.input),i=[...j(o.extract.ignore),...k(o.extract.output)].filter(Boolean),r=t.filter(o=>!i.some(e=>n(o,e,{dot:!0})));e.watch(r,{ignored:/node_modules/,persistent:!0}).on("change",o=>{console.log(`\nFile changed: ${o}`),a()})}}}),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.").action(async o=>{const e=x.opts().config,t=await a(e);await u(t,o)}),x.command("locize-download").description("Download all translations from your locize project.").action(async o=>{const e=x.opts().config,t=await a(e);await y(t,o)}),x.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async o=>{const e=x.opts().config,t=await a(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,r=await a(n),c=await w(r,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]:[],k=o=>o&&"string"==typeof o?[o.replace(/\{\{[^}]+\}\}/g,"*")]:[],v=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 t from"ora";import a from"chalk";import{parse as e}from"@swc/core";import{mkdir as o,writeFile as r,readFile as n}from"node:fs/promises";import{dirname as s,extname as i}from"node:path";import{findKeys as c}from"./key-finder.js";import{getTranslations as l}from"./translation-manager.js";import{validateExtractorConfig as m,ExtractorError as
|
|
1
|
+
import t from"ora";import a from"chalk";import{parse as e}from"@swc/core";import{mkdir as o,writeFile as r,readFile as n}from"node:fs/promises";import{dirname as s,extname as i}from"node:path";import{findKeys as c}from"./key-finder.js";import{getTranslations as l}from"./translation-manager.js";import{validateExtractorConfig as m,ExtractorError as p}from"../../utils/validation.js";import{extractKeysFromComments as f}from"../parsers/comment-parser.js";import{ConsoleLogger as u}from"../../utils/logger.js";import{loadRawJson5Content as y,serializeTranslationFile as d}from"../../utils/file-utils.js";import{shouldShowFunnel as g,recordFunnelShown as w}from"../../utils/funnel-msg-tracker.js";async function h(e,{isWatchMode:n=!1,isDryRun:i=!1,syncPrimaryWithDefaults:p=!1,syncAll:f=!1}={},h=new u){e.extract.primaryLanguage||=e.locales[0]||"en",e.extract.secondaryLanguages||=e.locales.filter(t=>t!==e?.extract?.primaryLanguage),e.extract.functions||=["t","*.t"],e.extract.transComponents||=["Trans"],m(e);const x=e.plugins||[],j=t("Running i18next key extractor...\n").start();try{const{allKeys:t,objectKeys:n}=await c(e,h);j.text=`Found ${t.size} unique keys. Updating translation files...`;const m=await l(t,n,e,{syncPrimaryWithDefaults:p,syncAll:f});let u=!1;for(const t of m)if(t.updated&&(u=!0,!i)){const n=e.extract.outputFormat??(t.path.endsWith(".json5")?"json5":"json"),i="json5"===n?await y(t.path)??void 0:void 0,c=d(t.newTranslations,n,e.extract.indentation,i);await o(s(t.path),{recursive:!0}),await r(t.path,c),h.info(a.green(`Updated: ${t.path}`))}if(x.length>0){j.text="Running post-extraction plugins...";for(const t of x)await(t.afterSync?.(m,e))}return j.succeed(a.bold("Extraction complete!")),u&&await async function(){if(!await g("extract"))return;return console.log(a.yellow.bold("\n💡 Tip: Tired of running the extractor manually?")),console.log(' Discover a real-time "push" workflow with `saveMissing` and Locize AI,'),console.log(" where keys are created and translated automatically as you code."),console.log(` Learn more: ${a.cyan("https://www.locize.com/blog/i18next-savemissing-ai-automation")}`),console.log(` Watch the video: ${a.cyan("https://youtu.be/joPsZghT3wM")}`),w("extract")}(),u}catch(t){throw j.fail(a.red("Extraction failed.")),t}}async function x(t,a,o,r,s,c=new u){try{let l=await n(t,"utf-8");for(const e of a)try{const a=await(e.onLoad?.(l,t));void 0!==a&&(l=a)}catch(t){c.warn(`Plugin ${e.name} onLoad failed:`,t)}const m=i(t).toLowerCase(),u=".ts"===m||".tsx"===m||".mts"===m||".cts"===m,y=".tsx"===m,d=".jsx"===m;let g;try{g=await e(l,{syntax:u?"typescript":"ecmascript",tsx:y,jsx:d,decorators:!0,dynamicImport:!0,comments:!0})}catch(a){if(".ts"!==m||y)throw new p("Failed to process file",t,a);try{g=await e(l,{syntax:"typescript",tsx:!0,decorators:!0,dynamicImport:!0,comments:!0}),c.info?.(`Parsed ${t} using TSX fallback`)}catch(a){throw new p("Failed to process file",t,a)}}r.getVarFromScope=o.getVarFromScope.bind(o),o.setCurrentFile(t,l),o.visit(g),f(l,r,s,o.getVarFromScope.bind(o))}catch(a){throw new p("Failed to process file",t,a)}}async function j(t,{syncPrimaryWithDefaults:a=!1}={}){t.extract.primaryLanguage||=t.locales[0]||"en",t.extract.secondaryLanguages||=t.locales.filter(a=>a!==t?.extract?.primaryLanguage),t.extract.functions||=["t","*.t"],t.extract.transComponents||=["Trans"];const{allKeys:e,objectKeys:o}=await c(t);return l(e,o,t,{syncPrimaryWithDefaults:a})}export{j as extract,x as processFile,h as runExtractor};
|
|
@@ -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 s(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?i(e.value.expression):void 0}function r(n,r){const o=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 c=!!a||!!u,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),y="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,x=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let h="JSXAttribute"===x?.type&&"JSXExpressionContainer"===x.value?.type?x.value.expression:"JSXAttribute"===x?.type&&"StringLiteral"===x.value?.type?x.value:void 0;const v=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let S;S="JSXAttribute"===v?.type?s(v):void 0,y&&(void 0===S&&(S=t(y,"ns")),void 0===h&&(h=e(y,"context")));const m=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");function r(e,t,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&&s(e[l]);)l++;for(;u>=l&&s(e[u]);)u--;const c=l<=u?e.slice(l,u+1):[],f=c.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<c.length;e++){const p=c[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}if(void 0!==i(p.expression))continue;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"),r=c[e-1],o=c[e+1];if(i){const n=c[e+2];if(o&&"JSXText"===o.type&&s(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=c[e-1],n=c[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue;const i=!o||"JSXText"===o.type&&!s(o);if(r&&"JSXText"===r.type&&i){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))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,s=p.children||[],o=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==i(s[0].expression)),l=!s.length,u=o;n&&!o?(t.push(p),r(p.children||[],t,!0)):l?t.push(p):u||("p"===e&&a?(t.push(p),r(p.children||[],t,!0,!1)):r(p.children||[],t,!1,!1));continue}t.push(p),r(p.children||[],t,!0);continue}"JSXFragment"!==p.type||r(p.children||[],t,o)}else{if(o&&!f)continue;if(o&&s(p))continue;if(s(p)){const n=c[e-1],i=c[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const s=t[t.length-1],r=c[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+p.value;continue}}}if(o&&f&&0===e)continue;t.push(p)}}}const o=[];function p(e){if(!e||!e.length)return!1;let t=!1;for(const n of e)if(n)if("JSXElement"!==n.type){if("JSXExpressionContainer"===n.type&&-1!==o.indexOf(n))return t;if("JSXText"===n.type&&-1!==o.indexOf(n)){if(s(n))continue;if(!t)return!0;const i=e.indexOf(n);if(e.slice(i+1).some(e=>e&&"JSXElement"===e.type))return!0}}else t=!0;return!1}r(e,o,!1,!0);const a=new Set,l=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function u(e,t,r=!1){if(!e||0===e.length)return"";let c="";const f=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 o.indexOf(e)};let y=0;for(let t=0;t<e.length;t++){const g=e[t];if(g){if("JSXText"===g.type){if(s(g))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(g.value)){const e=g.value.replace(/^\s*\n\s*/,"");if(e){c+=e;continue}continue}if(!s&&/^\s*\n\s*/.test(g.value)){const n=g.value.replace(/^\s*\n\s*/,"");if(n){if(r&&"JSXElement"===r.type&&Array.isArray(r.children)&&0===r.children.length){c+=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),s=/^[A-Za-z0-9]/.test(n),r=/^[a-z]/.test(n);if(t&&s&&r){c+=n;continue}}c+=" "+n;continue}continue}}if(/\n\s*$/.test(g.value)&&i&&"JSXElement"===i.type){const r=g.value.replace(/\n\s*$/,"");if(r.trim()){const o="Identifier"===i.opening?.name?.type?i.opening.name.value:void 0,p=o&&n.has(o),l=(i.children||[]).length>0,u=/\s\n/.test(g.value),f=e[t+2],y=f&&"JSXText"===f.type&&!s(f)&&/[a-zA-Z0-9]/.test(f.value),d=!!(i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0),x=/^\s/.test(r)&&!/^\n/.test(r),h=r.trim(),v=x?" "+h:h,S=/[A-Za-z0-9]$/.test(h),m=f&&"string"==typeof f.value&&/^[A-Za-z0-9]/.test(f.value.trim()),J=f&&"string"==typeof f.value&&/^[a-z]/.test(f.value.trim()),X=f&&"string"==typeof f.value&&/^\s/.test(f.value)&&!/^\n/.test(f.value),$=d&&l&&y&&!(S&&m&&J&&!u&&!x&&!X),E=f&&"string"==typeof f.value&&/^[,;:!?.]/.test(f.value.trim()),b=$&&!E;S&&m&&J&&!u&&!x&&!X&&a.add(i),c+=u||p&&l||!p&&y&&x||b?v+" ":v;continue}}c+=g.value;continue}if("JSXExpressionContainer"===g.type){const e=g.expression;if(!e)continue;const t=i(e);if(void 0!==t)c+=t;else if("Identifier"===e.type)c+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?c+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?c+=`{{${t.value}}}`:c+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?c+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?c+=`{{${e.callee.value}}}`:c+="{{value}}";continue}if("JSXElement"===g.type){let a;g.opening&&g.opening.name&&"Identifier"===g.opening.name.type&&(a=g.opening.name.value);const d=r?y:void 0;if(r&&"JSXElement"===g.type&&y++,a&&n.has(a)){const x=g.opening&&Array.isArray(g.opening.attributes)&&g.opening.attributes.length>0,h=g.children||[],v=h.length>0,S=1===h.length&&("JSXText"===h[0]?.type||"JSXExpressionContainer"===h[0]?.type&&void 0!==i(h[0].expression)),m="p"===a;let J=0;if(m&&r&&(J=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!v||S){const i=S?u(h,void 0):"";if(""!==String(i).trim())if(n.has(a)&&"p"!==a)c+=`<${a}>${l(i)}</${a}>`;else if(m&&r&&J>1)c+=`<${a}>${l(i)}</${a}>`;else if(m){const e=r?y-1:o.indexOf(g);c+=`<${e}>${l(i)}</${e}>`}else{const e=r?y-1:o.indexOf(g);c+=`<${e}>${l(i)}</${e}>`}else{const n=e[t-1];n&&"JSXText"===n.type&&/\n\s*$/.test(n.value)&&(c=c.replace(/\s+$/,"")),c+=`<${a} />`}}else if(x&&!S){const e=h,t=f(g);if(p(e)){const s=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||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!e&&(!n.length||o)||s.set(t,r++)}else s.set(t,r++)}const o=u(e,s.size?s:void 0);c+=`<${t}>${l(o)}</${t}>`}else{const e=new Map;let s=0;for(const t of h)if(t&&"JSXElement"===t.type){const r=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(r&&n.has(r)){const n=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,r=t.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression));!n&&(!r.length||o)||e.set(t,s++)}else e.set(t,s++)}const r=u(h,e.size?e:void 0);c+=`<${t}>${l(r)}</${t}>`}}else{const e=o.indexOf(g);if(-1!==e){const t=void 0!==d?d:e;if((()=>{let e=!1;for(const t of h)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(s(t))continue;if(!e)return!0;if(h.slice(h.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){if(0===t){const e=u(h,void 0,!1);c+=`<${t}>${l(e)}</${t}>`;continue}const e=new Map;let s=0;for(const t of h)if(t&&"JSXElement"===t.type){const r=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(r&&n.has(r)){const n=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,r=t.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression));!n&&(!r.length||o)||e.set(t,s++)}else e.set(t,s++)}const r=u(h,e.size?e:void 0,!1);c+=`<${t}>${l(r)}</${t}>`;continue}const r=new Map;let p=0;for(const e of h)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||[],s=1===n.length&&("JSXText"===n[0]?.type||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!t&&(!n.length||s)||r.set(e,p++)}else r.set(e,p++)}const a=u(h,r.size>0?r:void 0,!1);c+=`<${t}>${l(a)}</${t}>`}else{const e=u(h,void 0,!1);c+=`<${a}>${l(e)}</${a}>`}}}else{const e=g.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(g),n=u(e,void 0);c+=`<${t}>${l(n)}</${t}>`}else{const t=new Map,i=f(g);let s=0;for(const i of e)if(i&&"JSXElement"===i.type){const e=i.opening&&i.opening.name&&"Identifier"===i.opening.name.type?i.opening.name.value:void 0;if(e&&n.has(e)){const e=i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0,n=i.children||[],r=1===n.length&&"JSXText"===n[0]?.type;!e&&(!n.length||r)||t.set(i,s++)}else t.set(i,s++)}const r=u(e,t.size?t:void 0);c+=`<${i}>${l(r)}</${i}>`}}continue}"JSXFragment"!==g.type||(c+=u(g.children||[]))}}return c}const c=u(e,void 0,!0),f=String(c).replace(/<br \/>\s*\n\s*/g,"<br />"),y=String(f),g=new Set;if(a&&a.size>0)for(let e=0;e<o.length;e++)a.has(o[e])&&g.add(e);let d=String(y);if(g&&g.size>0)for(const e of g)try{d=d.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),d=d.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}d=d.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return g.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),d=d.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return g.has(i)?`${t}<${n}`:`${t} <${n}`}),d=d.replace(/\s*\n\s*/g," "),d=d.replace(/\s+/g," "),d=d.replace(/\s+([,;:!?.])/g,"$1");const x=d.trim();let h=String(x);if(g&&g.size>0)for(const e of g)try{h=h.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),h=h.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<o.length;e++){const t=o[e];if(!t||"JSXElement"!==t.type)continue;const n=o[e-1],i=o[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),p=s.replace(/\n\s*$/,""),a=/[A-Za-z0-9]$/.test(p),l=/^[A-Za-z0-9]/.test(r.trim()),u=/^[a-z]/.test(r.trim()),c=/\s\n/.test(s),f=r&&/^\s/.test(r)&&!/^\n/.test(r);if(a&&l&&u&&!c&&!f){const t=e;h=h.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}function v(e){if(!e||!e.includes("<"))return e;function t(e){const t=[],n=[];let i=0;const s=/<\/?(\d+)>|<[^>]+>/g;let r;for(;r=s.exec(e);){const o=r[0],p=r.index;if(p>i){const s={type:"text",text:e.slice(i,p)};n.length?n[n.length-1].node.children.push(s):t.push(s)}const a=/^<\/(\d+)>$/.exec(o),l=/^<(\d+)>$/.exec(o);if(l){const e=Number(l[1]),i={type:"ph",idx:e,children:[]};n.length?n[n.length-1].node.children.push(i):t.push(i),n.push({node:i,idx:e})}else if(a)n.length&&n.pop();else{const e={type:"text",text:o};n.length?n[n.length-1].node.children.push(e):t.push(e)}i=s.lastIndex}if(i<e.length){const s={type:"text",text:e.slice(i)};n.length?n[n.length-1].node.children.push(s):t.push(s)}return t}function i(e,t=null){let s="";for(const r of e)if("text"===r.type)s+=r.text;else{const e=r.children.filter(e=>"ph"===e.type),p=e.map(e=>e.idx);if(!(p.length<=1||p.every((e,t)=>0===t||e===p[t-1]+1))){const e=r.children.map(e=>"text"===e.type?e.text:`<${e.idx}>${i(e.children,e.idx)}</${e.idx}>`).join("");s+=`<${r.idx}>${e}</${r.idx}>`;continue}const a=new Map;let l=0;try{const i=o[r.idx];if(i&&i.span){const s=i.span.start,p=i.span.end;let a=-1;for(let e=r.idx+1;e<o.length;e++){const t=o[e];if(t&&t.span&&(t.span.start>=s&&t.span.end<=p&&"JSXElement"===t.type)){a=e;break}}let u=0;if(-1!==a)for(let e=r.idx+1;e<a;e++){const t=o[e];t&&t.span&&(t.span.start>=s&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||u++))}else for(let e=r.idx+1;e<o.length;e++){const t=o[e];t&&t.span&&(t.span.start>=s&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||u++))}if("number"==typeof r.idx){const i=o[r.idx],s=i?.opening?.name?.value,p=s&&n.has(s),a=null===t;l=1===e.length&&(!a||p||0!==e[0].idx||"a"!==s&&"p"!==s)?0===r.idx?0:Math.max(1,u+1):0}}}catch(e){}for(const t of e)a.has(t.idx)||a.set(t.idx,l++);const u=r.children.map(e=>{if("text"===e.type)return e.text;const t=e.idx,n=a.has(t)?a.get(t):t;return`<${n}>${i(e.children,n)}</${n}>`}).join("");s+=`<${r.idx}>${u}</${r.idx}>`}return s}try{return i(t(e))}catch(t){return e}}return h=v(h),h.trim()}(n.children,r);let J;const X="JSXAttribute"===p?.type?s(p):void 0;if(void 0!==X)J=X;else{const e=r.extract.defaultValue;J="string"==typeof e?e:""}let $,E;if("JSXAttribute"===o?.type){if("StringLiteral"===o.value?.type){if($=o.value,E=$.value,!E||""===E.trim())return null;if(S&&"StringLiteral"===$.type){const e=r.extract.nsSeparator??":",t=$.value;if(e&&t.startsWith(`${S}${e}`)){if(E=t.slice(`${S}${e}`.length),!E||""===E.trim())return null;$={...$,value:E}}}}else"JSXExpressionContainer"===o.value?.type&&"JSXEmptyExpression"!==o.value.expression.type&&($=o.value.expression);if(!$)return null}p||!E||m.trim()?!p&&m.trim()&&(J=m):J=E;return{keyExpression:$,serializedChildren:m,ns:S,defaultValue:J,hasCount:c,isOrdinal:d,contextExpression:h,optionsNode:y,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})(y)}}export{r as extractFromTransComponent};
|
|
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 s(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?i(e.value.expression):void 0}function r(n,r){const o=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 c=!!a||!!u,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),y="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,x=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let h="JSXAttribute"===x?.type&&"JSXExpressionContainer"===x.value?.type?x.value.expression:"JSXAttribute"===x?.type&&"StringLiteral"===x.value?.type?x.value:void 0;const v=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let S;S="JSXAttribute"===v?.type?s(v):void 0,y&&(void 0===S&&(S=t(y,"ns")),void 0===h&&(h=e(y,"context")));const m=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");function r(e,t,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&&s(e[l]);)l++;for(;u>=l&&s(e[u]);)u--;const c=l<=u?e.slice(l,u+1):[],f=c.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<c.length;e++){const l=c[e];if(l)if("JSXText"!==l.type){if("JSXExpressionContainer"===l.type){if(o&&!f&&l.expression){const e=l.expression.type;if("ObjectExpression"===e){const e=l.expression.properties&&l.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}if(void 0!==i(l.expression))continue;if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const n=i(l.expression);if(void 0!==n){const i=/^\s*$/.test(n)&&!n.includes("\n"),r=c[e-1],o=c[e+1];if(i){const n=c[e+2];if(o&&"JSXText"===o.type&&s(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=c[e-1],n=c[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue;const i=!o||"JSXText"===o.type&&!s(o);if(r&&"JSXText"===r.type&&i){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+l.expression.value;continue}}if(r&&("JSXElement"===r.type||"JSXFragment"===r.type)&&o&&"JSXText"===o.type&&s(o))continue}}t.push(l);continue}if("JSXElement"===l.type){const e=l.opening&&l.opening.name&&"Identifier"===l.opening.name.type?l.opening.name.value:void 0;if(e&&n.has(e)){const o=l.opening&&Array.isArray(l.opening.attributes)&&l.opening.attributes.length>0,u=l.children||[],f=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==i(u[0].expression)),y=!u.length,g=f;if(o&&!f)t.push(l),r(l.children||[],t,!0);else if(y)t.push(l);else if(!g)if("p"===e){const e=(u||[]).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,s=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,r=e.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression));return!(t&&n.has(t)&&!s&&(0===r.length||o))}),o=p&&c.some(e=>e&&e!==l&&"JSXText"===e.type&&!s(e));a||p&&e&&!o?(t.push(l),r(l.children||[],t,!0,!1)):r(l.children||[],t,!1,!1)}else r(l.children||[],t,!1,!1);continue}t.push(l),r(l.children||[],t,!0);continue}"JSXFragment"!==l.type||r(l.children||[],t,o)}else{if(o&&!f)continue;if(o&&s(l))continue;if(s(l)){const n=c[e-1],i=c[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const s=t[t.length-1],r=c[e-1];if(s){if(r&&"JSXExpressionContainer"===r.type)continue;if("JSXText"===s.type&&r&&"JSXText"===r.type){s.value=String(s.value)+l.value;continue}}}if(o&&f&&0===e)continue;t.push(l)}}}const o=[];function p(e){if(!e||!e.length)return!1;let t=!1;for(const n of e)if(n)if("JSXElement"!==n.type){if("JSXExpressionContainer"===n.type&&-1!==o.indexOf(n))return t;if("JSXText"===n.type&&-1!==o.indexOf(n)){if(s(n))continue;if(!t)return!0;const i=e.indexOf(n);if(e.slice(i+1).some(e=>e&&"JSXElement"===e.type))return!0}}else t=!0;return!1}r(e,o,!1,!0);const a=new Set,l=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function u(e,t,r=!1){if(!e||0===e.length)return"";let c="";const f=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 o.indexOf(e)};let y=0;for(let t=0;t<e.length;t++){const g=e[t];if(g){if("JSXText"===g.type){if(s(g))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(g.value)){const e=g.value.replace(/^\s*\n\s*/,"");if(e){c+=e;continue}continue}if(!s&&/^\s*\n\s*/.test(g.value)){const n=g.value.replace(/^\s*\n\s*/,"");if(n){if(r&&"JSXElement"===r.type&&Array.isArray(r.children)&&0===r.children.length){c+=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),s=/^[A-Za-z0-9]/.test(n),r=/^[a-z]/.test(n);if(t&&s&&r){c+=n;continue}}c+=" "+n;continue}continue}}if(/\n\s*$/.test(g.value)&&i&&"JSXElement"===i.type){const r=g.value.replace(/\n\s*$/,"");if(r.trim()){const o="Identifier"===i.opening?.name?.type?i.opening.name.value:void 0,p=o&&n.has(o),l=(i.children||[]).length>0,u=/\s\n/.test(g.value),f=e[t+2],y=f&&"JSXText"===f.type&&!s(f)&&/[a-zA-Z0-9]/.test(f.value),d=!!(i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0),x=/^\s/.test(r)&&!/^\n/.test(r),h=r.trim(),v=x?" "+h:h,S=/[A-Za-z0-9]$/.test(h),m=f&&"string"==typeof f.value&&/^[A-Za-z0-9]/.test(f.value.trim()),J=f&&"string"==typeof f.value&&/^[a-z]/.test(f.value.trim()),X=f&&"string"==typeof f.value&&/^\s/.test(f.value)&&!/^\n/.test(f.value),$=d&&l&&y&&!(S&&m&&J&&!u&&!x&&!X),E=f&&"string"==typeof f.value&&/^[,;:!?.]/.test(f.value.trim()),b=$&&!E;S&&m&&J&&!u&&!x&&!X&&a.add(i),c+=u||p&&l||!p&&y&&x||b?v+" ":v;continue}}c+=g.value;continue}if("JSXExpressionContainer"===g.type){const e=g.expression;if(!e)continue;const t=i(e);if(void 0!==t)c+=t;else if("Identifier"===e.type)c+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?c+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?c+=`{{${t.value}}}`:c+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?c+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?c+=`{{${e.callee.value}}}`:c+="{{value}}";continue}if("JSXElement"===g.type){let a;g.opening&&g.opening.name&&"Identifier"===g.opening.name.type&&(a=g.opening.name.value);const d=r?y:void 0;if(r&&"JSXElement"===g.type&&y++,a&&n.has(a)){const x=g.opening&&Array.isArray(g.opening.attributes)&&g.opening.attributes.length>0,h=g.children||[],v=h.length>0,S=1===h.length&&("JSXText"===h[0]?.type||"JSXExpressionContainer"===h[0]?.type&&void 0!==i(h[0].expression)),m="p"===a;let J=0;if(m&&r&&(J=e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length),!v||S){const i=S?u(h,void 0):"";if(""!==String(i).trim())if(n.has(a)&&"p"!==a)c+=`<${a}>${l(i)}</${a}>`;else if(m&&r&&J>1)c+=`<${a}>${l(i)}</${a}>`;else if(m){const e=r?y-1:o.indexOf(g);c+=`<${e}>${l(i)}</${e}>`}else{const e=r?y-1:o.indexOf(g);c+=`<${e}>${l(i)}</${e}>`}else{const n=e[t-1];n&&"JSXText"===n.type&&/\n\s*$/.test(n.value)&&(c=c.replace(/\s+$/,"")),c+=`<${a} />`}}else if(x&&!S){const e=h,t=f(g);if(p(e)){const s=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||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!e&&(!n.length||o)||s.set(t,r++)}else s.set(t,r++)}const o=u(e,s.size?s:void 0);c+=`<${t}>${l(o)}</${t}>`}else{const e=new Map;let s=0;for(const t of h)if(t&&"JSXElement"===t.type){const r=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(r&&n.has(r)){const n=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,r=t.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression));!n&&(!r.length||o)||e.set(t,s++)}else e.set(t,s++)}const r=u(h,e.size?e:void 0);c+=`<${t}>${l(r)}</${t}>`}}else{const e=o.indexOf(g);if(-1!==e){const t=void 0!==d?d:e;if((()=>{let e=!1;for(const t of h)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(s(t))continue;if(!e)return!0;if(h.slice(h.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){if(0===t){const e=u(h,void 0,!1);c+=`<${t}>${l(e)}</${t}>`;continue}const e=new Map;let s=0;for(const t of h)if(t&&"JSXElement"===t.type){const r=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(r&&n.has(r)){const n=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,r=t.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==i(r[0].expression));!n&&(!r.length||o)||e.set(t,s++)}else e.set(t,s++)}const r=u(h,e.size?e:void 0,!1);c+=`<${t}>${l(r)}</${t}>`;continue}const r=new Map;let p=0;for(const e of h)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||[],s=1===n.length&&("JSXText"===n[0]?.type||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!t&&(!n.length||s)||r.set(e,p++)}else r.set(e,p++)}const a=u(h,r.size>0?r:void 0,!1);c+=`<${t}>${l(a)}</${t}>`}else{const e=u(h,void 0,!1);c+=`<${a}>${l(e)}</${a}>`}}}else{const e=g.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(g),n=u(e,void 0);c+=`<${t}>${l(n)}</${t}>`}else{const t=new Map,i=f(g);let s=0;for(const i of e)if(i&&"JSXElement"===i.type){const e=i.opening&&i.opening.name&&"Identifier"===i.opening.name.type?i.opening.name.value:void 0;if(e&&n.has(e)){const e=i.opening&&Array.isArray(i.opening.attributes)&&i.opening.attributes.length>0,n=i.children||[],r=1===n.length&&"JSXText"===n[0]?.type;!e&&(!n.length||r)||t.set(i,s++)}else t.set(i,s++)}const r=u(e,t.size?t:void 0);c+=`<${i}>${l(r)}</${i}>`}}continue}"JSXFragment"!==g.type||(c+=u(g.children||[]))}}return c}const c=u(e,void 0,!0),f=String(c).replace(/<br \/>\s*\n\s*/g,"<br />"),y=String(f),g=new Set;if(a&&a.size>0)for(let e=0;e<o.length;e++)a.has(o[e])&&g.add(e);let d=String(y);if(g&&g.size>0)for(const e of g)try{d=d.replace(new RegExp("\\s+<"+e+">","g"),"<"+e+">"),d=d.replace(new RegExp("<\\/"+e+">\\s+","g"),"</"+e+">")}catch(e){}d=d.replace(/<\/(\d+)>\s*\n\s*(\S)/g,(e,t,n)=>{const i=Number(t);return g.has(i)?`</${t}>${n}`:`</${t}> ${n}`}),d=d.replace(/(\S)\s*\n\s*<(\d+)/g,(e,t,n)=>{const i=Number(n);return g.has(i)?`${t}<${n}`:`${t} <${n}`}),d=d.replace(/\s*\n\s*/g," "),d=d.replace(/\s+/g," "),d=d.replace(/\s+([,;:!?.])/g,"$1");const x=d.trim();let h=String(x);if(g&&g.size>0)for(const e of g)try{h=h.replace(new RegExp("[\\s\\u00A0]+<"+e+">","g"),"<"+e+">"),h=h.replace(new RegExp("<\\/"+e+">[\\s\\u00A0]+","g"),"</"+e+">")}catch(e){}try{for(let e=0;e<o.length;e++){const t=o[e];if(!t||"JSXElement"!==t.type)continue;const n=o[e-1],i=o[e+1];if(!n||!i)continue;if("JSXText"!==n.type||"JSXText"!==i.type)continue;const s=String(n.value),r=String(i.value),p=s.replace(/\n\s*$/,""),a=/[A-Za-z0-9]$/.test(p),l=/^[A-Za-z0-9]/.test(r.trim()),u=/^[a-z]/.test(r.trim()),c=/\s\n/.test(s),f=r&&/^\s/.test(r)&&!/^\n/.test(r);if(a&&l&&u&&!c&&!f){const t=e;h=h.replace(new RegExp("\\s+<"+t+">","g"),"<"+t+">")}}}catch(e){}function v(e){if(!e||!e.includes("<"))return e;function t(e){const t=[],n=[];let i=0;const s=/<\/?(\d+)>|<[^>]+>/g;let r;for(;r=s.exec(e);){const o=r[0],p=r.index;if(p>i){const s={type:"text",text:e.slice(i,p)};n.length?n[n.length-1].node.children.push(s):t.push(s)}const a=/^<\/(\d+)>$/.exec(o),l=/^<(\d+)>$/.exec(o);if(l){const e=Number(l[1]),i={type:"ph",idx:e,children:[]};n.length?n[n.length-1].node.children.push(i):t.push(i),n.push({node:i,idx:e})}else if(a)n.length&&n.pop();else{const e={type:"text",text:o};n.length?n[n.length-1].node.children.push(e):t.push(e)}i=s.lastIndex}if(i<e.length){const s={type:"text",text:e.slice(i)};n.length?n[n.length-1].node.children.push(s):t.push(s)}return t}function i(e,t=null){let s="";for(const r of e)if("text"===r.type)s+=r.text;else{const e=r.children.filter(e=>"ph"===e.type),p=e.map(e=>e.idx);if(!(p.length<=1||p.every((e,t)=>0===t||e===p[t-1]+1))){const e=r.children.map(e=>"text"===e.type?e.text:`<${e.idx}>${i(e.children,e.idx)}</${e.idx}>`).join("");s+=`<${r.idx}>${e}</${r.idx}>`;continue}const a=new Map;let l=0;try{const i=o[r.idx];if(i&&i.span){const s=i.span.start,p=i.span.end;let a=-1;for(let e=r.idx+1;e<o.length;e++){const t=o[e];if(t&&t.span&&(t.span.start>=s&&t.span.end<=p&&"JSXElement"===t.type)){a=e;break}}let u=0;if(-1!==a)for(let e=r.idx+1;e<a;e++){const t=o[e];t&&t.span&&(t.span.start>=s&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||u++))}else for(let e=r.idx+1;e<o.length;e++){const t=o[e];t&&t.span&&(t.span.start>=s&&t.span.end<=p&&("JSXText"!==t.type&&"JSXExpressionContainer"!==t.type||u++))}if("number"==typeof r.idx){const i=o[r.idx],s=i?.opening?.name?.value,p=s&&n.has(s),a=null===t;1===e.length?(l=Math.max(1,u+1),!a||p||0!==e[0].idx||"a"!==s&&"p"!==s||(l=0)):l=0}}}catch(e){}for(const t of e)a.has(t.idx)||a.set(t.idx,l++);const u=r.children.map(e=>{if("text"===e.type)return e.text;const t=e.idx,n=a.has(t)?a.get(t):t;return`<${n}>${i(e.children,n)}</${n}>`}).join("");s+=`<${r.idx}>${u}</${r.idx}>`}return s}try{return i(t(e))}catch(t){return e}}return h=v(h),h.trim()}(n.children,r);let J;const X="JSXAttribute"===p?.type?s(p):void 0;if(void 0!==X)J=X;else{const e=r.extract.defaultValue;J="string"==typeof e?e:""}let $,E;if("JSXAttribute"===o?.type){if("StringLiteral"===o.value?.type){if($=o.value,E=$.value,!E||""===E.trim())return null;if(S&&"StringLiteral"===$.type){const e=r.extract.nsSeparator??":",t=$.value;if(e&&t.startsWith(`${S}${e}`)){if(E=t.slice(`${S}${e}`.length),!E||""===E.trim())return null;$={...$,value:E}}}}else"JSXExpressionContainer"===o.value?.type&&"JSXEmptyExpression"!==o.value.expression.type&&($=o.value.expression);if(!$)return null}p||!E||m.trim()?!p&&m.trim()&&(J=m):J=E;return{keyExpression:$,serializedChildren:m,ns:S,defaultValue:J,hasCount:c,isOrdinal:d,contextExpression:h,optionsNode:y,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})(y)}}export{r as extractFromTransComponent};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{glob as s}from"glob";import{readdir as e}from"node:fs/promises";import{
|
|
1
|
+
import{glob as s}from"glob";import{readdir as e}from"node:fs/promises";import{extname as n,dirname as o,join as l}from"node:path";const c=["public/locales/dev/*.json","locales/dev/*.json","src/locales/dev/*.json","src/assets/locales/dev/*.json","app/i18n/locales/dev/*.json","src/i18n/locales/dev/*.json","public/locales/en/*.json","locales/en/*.json","src/locales/en/*.json","src/assets/locales/en/*.json","app/i18n/locales/en/*.json","src/i18n/locales/en/*.json","public/locales/en/*.json5","locales/en/*.json5","src/locales/en/*.json5","src/assets/locales/en/*.json5","app/i18n/locales/en/*.json5","src/i18n/locales/en/*.json5","public/locales/en-*/*.json","locales/en-*/*.json","src/locales/en-*/*.json","src/assets/locales/en-*/*.json","app/i18n/locales/en-*/*.json","src/i18n/locales/en-*/*.json"];async function a(){for(const a of c){const c=await s(a,{ignore:"node_modules/**"});if(c.length>0){const s=c.find(s=>".json5"===n(s))||c[0],a=o(o(s)),t=n(s);let j="json";".ts"===t?j="ts":".js"===t?j="js":".json5"===t&&(j="json5");try{let s=(await e(a)).filter(s=>/^(dev|[a-z]{2}(-[A-Z]{2})?)$/.test(s));if(s.length>0)return s.sort(),s.includes("dev")&&(s=["dev",...s.filter(s=>"dev"!==s)]),s.includes("en")&&(s=["en",...s.filter(s=>"en"!==s)]),{locales:s,extract:{input:["src/**/*.{js,jsx,ts,tsx}","app/**/*.{js,jsx,ts,tsx}","pages/**/*.{js,jsx,ts,tsx}","components/**/*.{js,jsx,ts,tsx}"],output:l(a,"{{language}}",`{{namespace}}${t}`),outputFormat:j,primaryLanguage:s.includes("en")?"en":s[0]}}}catch{continue}}}return null}export{a as detectConfig};
|
package/dist/esm/syncer.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import o from"chalk";import{glob as t}from"glob";import{mkdir as
|
|
1
|
+
import o from"chalk";import{glob as t}from"glob";import{mkdir as n,writeFile as e}from"node:fs/promises";import{basename as r,resolve as i,dirname as a}from"node:path";import s from"ora";import{resolveDefaultValue as l}from"./utils/default-value.js";import{getOutputPath as c,loadTranslationFile as f,loadRawJson5Content as u,serializeTranslationFile as m}from"./utils/file-utils.js";import{shouldShowFunnel as p,recordFunnelShown as y}from"./utils/funnel-msg-tracker.js";import{getNestedKeys as d,getNestedValue as g,setNestedValue as h}from"./utils/nested-object.js";async function w(w){const j=s("Running i18next locale synchronizer...\n").start();try{const s=w.extract.primaryLanguage||w.locales[0]||"en",x=w.locales.filter(o=>o!==s),{output:S,keySeparator:$=".",outputFormat:b="json",indentation:v=2,defaultValue:z=""}=w.extract,N=[];let k=!1;const A=c(S,s,"*"),F=await t(A);if(0===F.length)return void j.warn(`No translation files found for primary language "${s}". Nothing to sync.`);for(const t of F){const s=r(t).split(".")[0],p=await f(t);if(!p){N.push(` ${o.yellow("-")} Could not read primary file: ${t}`);continue}const y=d(p,$??".");for(const t of x){const r=c(S,t,s),d=i(process.cwd(),r),j=await f(d)||{},x={};for(const o of y){const n=g(p,o,$??"."),e=g(j,o,$??".")??l(z,o,s,t,n);h(x,o,e,$??".")}const A=JSON.stringify(j);if(JSON.stringify(x)!==A){k=!0;const t=w.extract.outputFormat??(d.endsWith(".json5")?"json5":b),i="json5"===t?await u(d)??void 0:void 0,s=m(x,t,v,i);await n(a(d),{recursive:!0}),await e(d,s),N.push(` ${o.green("✓")} Synchronized: ${r}`)}else N.push(` ${o.gray("-")} Already in sync: ${r}`)}}j.succeed(o.bold("Synchronization complete!")),N.forEach(o=>console.log(o)),k?await async function(){if(!await p("syncer"))return;return console.log(o.green.bold("\n✅ Sync complete.")),console.log(o.yellow("🚀 Ready to collaborate with translators? Move your files to the cloud.")),console.log(` Get started with the official TMS for i18next: ${o.cyan("npx i18next-cli locize-migrate")}`),y("syncer")}():console.log(o.green.bold("\n✅ All locales are already in sync."))}catch(t){j.fail(o.red("Synchronization failed.")),console.error(t)}}export{w as runSyncer};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{access as t,readFile as n}from"node:fs/promises";import{
|
|
1
|
+
import{access as t,readFile as n}from"node:fs/promises";import{resolve as r,normalize as e,extname as o}from"node:path";import{createJiti as a}from"jiti";import{getTsConfigAliases as s}from"../config.js";import{JsonParser as c,JsonObjectNode as i}from"@croct/json5-parser";function u(t,n,r){if(!t)return e(`locales/${n}/${r??"translation"}.json`);if("function"==typeof t)try{const o=String(t(n,r));return e(o.replace(/\/\/+/g,"/"))}catch{return e(`locales/${n}/${r??"translation"}.json`)}let o=String(t);return o=o.replace(/\{\{language\}\}|\{\{lng\}\}/g,n),o=null!=r?o.replace(/\{\{namespace\}\}/g,r):o.replace(/\/?\{\{namespace\}\}/g,""),o=o.replace(/\/\/+/g,"/"),e(o)}async function l(e){const u=r(process.cwd(),e);try{await t(u)}catch{return null}try{const t=o(u).toLowerCase();if(".json5"===t){const t=await n(u,"utf-8");return c.parse(t,i).toJSON()}if(".json"===t){const t=await n(u,"utf-8");return JSON.parse(t)}if(".ts"===t||".js"===t){const t=await s(),n=a(process.cwd(),{alias:t,interopDefault:!0});return await n.import(u,{default:!0})}return null}catch(t){return console.warn(`Could not parse translation file ${e}:`,t),null}}async function p(e){const o=r(process.cwd(),e);try{return await t(o),await n(o,"utf-8")}catch{return null}}function f(t,n="json",r=2,e){const o=JSON.stringify(t,null,r);switch(n){case"json5":if(e){const n=c.parse(e,i);return n.update(t),n.toString({object:{indentationSize:Number(r)??2}})}return c.parse(o,i).toString({object:{indentationSize:Number(r)??2}});case"js":case"js-esm":return`export default ${o};\n`;case"js-cjs":return`module.exports = ${o};\n`;case"ts":return`export default ${o} as const;\n`;default:return`${o}\n`}}export{u as getOutputPath,p as loadRawJson5Content,l as loadTranslationFile,f as serializeTranslationFile};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "i18next-cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.27.1",
|
|
4
4
|
"description": "A unified, high-performance i18next CLI.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -69,6 +69,7 @@
|
|
|
69
69
|
"vitest": "4.0.10"
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
|
+
"@croct/json5-parser": "0.2.1",
|
|
72
73
|
"@swc/core": "1.15.2",
|
|
73
74
|
"chalk": "5.6.2",
|
|
74
75
|
"chokidar": "4.0.3",
|
|
@@ -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;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cG;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":"heuristic-config.d.ts","sourceRoot":"","sources":["../src/heuristic-config.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"heuristic-config.d.ts","sourceRoot":"","sources":["../src/heuristic-config.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAiCnD;;;;;;GAMG;AACH,wBAAsB,YAAY,IAAK,OAAO,CAAC,OAAO,CAAC,oBAAoB,CAAC,GAAG,IAAI,CAAC,CA2DnF"}
|
package/types/syncer.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"syncer.d.ts","sourceRoot":"","sources":["../src/syncer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAMnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,SAAS,CAAE,MAAM,EAAE,oBAAoB,
|
|
1
|
+
{"version":3,"file":"syncer.d.ts","sourceRoot":"","sources":["../src/syncer.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAA;AAMnD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAsB,SAAS,CAAE,MAAM,EAAE,oBAAoB,iBAmF5D"}
|
package/types/types.d.ts
CHANGED
|
@@ -101,12 +101,13 @@ export interface I18nextToolkitConfig {
|
|
|
101
101
|
/**
|
|
102
102
|
* The format of the output translation files.
|
|
103
103
|
* 'json': Standard JSON file (default)
|
|
104
|
+
* 'json5': JSON5 file
|
|
104
105
|
* 'js': JavaScript file with ES Module syntax (export default)
|
|
105
106
|
* 'js-esm': JavaScript file with ES Module syntax (export default)
|
|
106
107
|
* 'js-cjs': JavaScript file with CommonJS syntax (module.exports)
|
|
107
108
|
* 'ts': TypeScript file with ES Module syntax and `as const` for type safety
|
|
108
109
|
*/
|
|
109
|
-
outputFormat?: 'json' | '
|
|
110
|
+
outputFormat?: 'json' | 'json5' | 'js' | 'ts' | 'js-esm' | 'js-cjs' | 'ts';
|
|
110
111
|
/**
|
|
111
112
|
* If true, all namespaces will be merged into a single file per language.
|
|
112
113
|
* The `output` path should not contain the `{{namespace}}` placeholder.
|
package/types/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,IAAI,EAAE,gBAAgB,EAAE,MAAM,WAAW,CAAA;AAEnE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,oBAAoB;IACnC,iEAAiE;IACjE,OAAO,EAAE,MAAM,EAAE,CAAC;IAElB,2DAA2D;IAC3D,OAAO,EAAE;QACP,oEAAoE;QACpE,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;QAEzB,4DAA4D;QAC5D,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;QAE3B,mGAAmG;QACnG,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,KAAK,MAAM,CAAC,CAAC;QAEpE;;;WAGG;QACH,SAAS,CAAC,EAAE,MAAM,GAAG,KAAK,CAAC;QAE3B,uEAAuE;QACvE,YAAY,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC;QAErC,8EAA8E;QAC9E,WAAW,CAAC,EAAE,MAAM,GAAG,KAAK,GAAG,IAAI,CAAC;QAEpC,o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AAW,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAAA;IACrD,gBAAgB,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC,CAAA;IAC3D,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,KAAK,CAAC,EAAE,MAAM,CAAA;CACf"}
|
|
@@ -54,12 +54,10 @@ export declare function getOutputPath(outputTemplate: string | ((language: strin
|
|
|
54
54
|
* @returns The parsed content of the file, or null if not found or failed to parse.
|
|
55
55
|
*/
|
|
56
56
|
export declare function loadTranslationFile(filePath: string): Promise<Record<string, any> | null>;
|
|
57
|
+
export declare function loadRawJson5Content(filePath: string): Promise<string | null>;
|
|
57
58
|
/**
|
|
58
59
|
* Serializes a translation object into a string based on the desired format.
|
|
59
|
-
*
|
|
60
|
-
* @param format - The desired output format ('json', 'js-esm', etc.).
|
|
61
|
-
* @param indentation - The number of spaces for indentation.
|
|
62
|
-
* @returns The serialized file content as a string.
|
|
60
|
+
* For JSON5, preserves comments and formatting using JsonObjectNode.update().
|
|
63
61
|
*/
|
|
64
|
-
export declare function serializeTranslationFile(data: Record<string, any>, format?: I18nextToolkitConfig['extract']['outputFormat'], indentation?: number | string): string;
|
|
62
|
+
export declare function serializeTranslationFile(data: Record<string, any>, format?: I18nextToolkitConfig['extract']['outputFormat'], indentation?: number | string, rawContent?: string): string;
|
|
65
63
|
//# sourceMappingURL=file-utils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"file-utils.d.ts","sourceRoot":"","sources":["../../src/utils/file-utils.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAA;
|
|
1
|
+
{"version":3,"file":"file-utils.d.ts","sourceRoot":"","sources":["../../src/utils/file-utils.ts"],"names":[],"mappings":"AAGA,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