i18next-cli 1.24.1 → 1.24.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -4
- package/dist/cjs/cli.js +1 -1
- package/dist/cjs/extractor/core/translation-manager.js +1 -1
- package/dist/cjs/extractor/parsers/jsx-parser.js +1 -1
- package/dist/esm/cli.js +1 -1
- package/dist/esm/extractor/core/translation-manager.js +1 -1
- package/dist/esm/extractor/parsers/jsx-parser.js +1 -1
- package/package.json +1 -1
- package/src/cli.ts +1 -1
- package/src/extractor/core/translation-manager.ts +10 -10
- package/src/extractor/parsers/jsx-parser.ts +39 -6
package/CHANGELOG.md
CHANGED
|
@@ -5,23 +5,31 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [1.24.3](https://github.com/i18next/i18next-cli/compare/v1.24.2...v1.24.3) - 2025-11-12
|
|
9
|
+
|
|
10
|
+
- improved Trans component parsing further [#102](https://github.com/i18next/i18next-cli/issues/102)
|
|
11
|
+
|
|
12
|
+
## [1.24.2](https://github.com/i18next/i18next-cli/compare/v1.24.1...v1.24.2) - 2025-11-12
|
|
13
|
+
|
|
14
|
+
- **Extractor (`--sync-primary`):** Fixed a bug where default values containing colons (like `"fallback:"`) were incorrectly identified as namespace-prefixed keys, resulting in empty strings in translation files when using the `--sync-primary` flag. [#110](https://github.com/i18next/i18next-cli/issues/110)
|
|
15
|
+
|
|
8
16
|
## [1.24.1](https://github.com/i18next/i18next-cli/compare/v1.24.0...v1.24.1) - 2025-11-12
|
|
9
17
|
|
|
10
18
|
- Windows fix for new **CLI:** command `rename-key`
|
|
11
19
|
|
|
12
20
|
## [1.24.0](https://github.com/i18next/i18next-cli/compare/v1.23.6...v1.24.0) - 2025-11-12
|
|
13
21
|
|
|
14
|
-
- **CLI:** Introduced the `rename-key` command for safely refactoring translation keys across your entire codebase. This new command updates both source files and translation JSON files atomically, automatically handling namespaces, plurals, and multiple locales. [109](https://github.com/i18next/i18next-cli/issues/109)
|
|
15
|
-
- improved Trans component parsing further [102](https://github.com/i18next/i18next-cli/issues/102)
|
|
22
|
+
- **CLI:** Introduced the `rename-key` command for safely refactoring translation keys across your entire codebase. This new command updates both source files and translation JSON files atomically, automatically handling namespaces, plurals, and multiple locales. [#109](https://github.com/i18next/i18next-cli/issues/109)
|
|
23
|
+
- improved Trans component parsing further [#102](https://github.com/i18next/i18next-cli/issues/102)
|
|
16
24
|
|
|
17
25
|
## [1.23.7](https://github.com/i18next/i18next-cli/compare/v1.23.6...v1.23.7) - 2025-11-12
|
|
18
26
|
|
|
19
|
-
- improved Trans component parsing further [102](https://github.com/i18next/i18next-cli/issues/102)
|
|
27
|
+
- improved Trans component parsing further [#102](https://github.com/i18next/i18next-cli/issues/102)
|
|
20
28
|
|
|
21
29
|
## [1.23.6](https://github.com/i18next/i18next-cli/compare/v1.23.5...v1.23.6) - 2025-11-12
|
|
22
30
|
|
|
23
31
|
- fix jsx extraction [#108](https://github.com/i18next/i18next-cli/issues/108)
|
|
24
|
-
- improved Trans component parsing for spaces [102](https://github.com/i18next/i18next-cli/issues/102)
|
|
32
|
+
- improved Trans component parsing for spaces [#102](https://github.com/i18next/i18next-cli/issues/102)
|
|
25
33
|
|
|
26
34
|
## [1.23.5](https://github.com/i18next/i18next-cli/compare/v1.23.4...v1.23.5) - 2025-11-11
|
|
27
35
|
|
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"),m=require("./rename-key.js");const y=new e.Command;y.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.24.
|
|
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"),m=require("./rename-key.js");const y=new e.Command;y.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.24.3"),y.option("-c, --config <path>","Path to i18next-cli config file (overrides detection)"),y.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").option("--sync-primary","Sync primary language values with default values from code.").action(async e=>{try{const t=y.opts().config,i=await r.ensureConfig(t),a=async()=>{const o=await c.runExtractor(i,{isWatchMode:!!e.watch,isDryRun:!!e.dryRun,syncPrimaryWithDefaults:!!e.syncPrimary});return e.ci&&!o?(console.log("✅ No files were updated."),process.exit(0)):e.ci&&o&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),o};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)}}),y.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(e,o)=>{const t=y.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})}),y.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async e=>{const t=y.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()})}}),y.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=y.opts().config,o=await r.ensureConfig(e);await l.runSyncer(o)}),y.command("migrate-config [configPath]").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async e=>{await u.runMigrator(e)}),y.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(d.runInit),y.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async e=>{const t=y.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()})}}}),y.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async e=>{const o=y.opts().config,t=await r.ensureConfig(o);await p.runLocizeSync(t,e)}),y.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const o=y.opts().config,t=await r.ensureConfig(o);await p.runLocizeDownload(t,e)}),y.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const o=y.opts().config,t=await r.ensureConfig(o);await p.runLocizeMigrate(t,e)}),y.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=y.opts().config,a=await r.ensureConfig(n),c=await m.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)}}),y.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()))};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("node:path"),t=require("glob"),s=require("../../utils/nested-object.js"),n=require("../../utils/file-utils.js"),r=require("../../utils/default-value.js");function o(e){const t=`^${e.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*")}$`;return new RegExp(t)}function a(e,t,s,n){if(0===t.size)return!1;let r=e;const o=["zero","one","two","few","many","other"];for(const e of o){if(r.endsWith(`${s}${e}`)){r=r.slice(0,-(s.length+e.length));break}if(r.endsWith(`${s}ordinal${s}${e}`)){r=r.slice(0,-(s.length+7+s.length+e.length));break}}const a=r.split(n);if(a.length>1)for(let e=1;e<a.length;e++){const s=a.slice(0,-e).join(n);if(t.has(s))return!0}return!!t.has(r)}function i(e,t,s){if("object"!=typeof e||null===e||Array.isArray(e))return e;const n={},r=t?.extract?.pluralSeparator??"_",o=["zero","one","two","few","many","other"],a=o.map(e=>`ordinal${r}${e}`),l=Object.keys(e).sort((e,t)=>{const n=e=>{for(const t of a)if(e.endsWith(`${r}${t}`)){return{base:e.slice(0,-(r.length+t.length)),form:t,isOrdinal:!0,isPlural:!0,fullKey:e}}for(const t of o)if(e.endsWith(`${r}${t}`)){return{base:e.slice(0,-(r.length+t.length)),form:t,isOrdinal:!1,isPlural:!0,fullKey:e}}return{base:e,form:"",isOrdinal:!1,isPlural:!1,fullKey:e}},i=n(e),l=n(t);if(i.isPlural&&l.isPlural){const e=s?s(i.base,l.base):i.base.localeCompare(l.base,void 0,{sensitivity:"base"});if(0!==e)return e;if(i.isOrdinal!==l.isOrdinal)return i.isOrdinal?1:-1;const t=i.isOrdinal?a:o,n=t.indexOf(i.form),r=t.indexOf(l.form);return-1!==n&&-1!==r?n-r:i.form.localeCompare(l.form)}if(s)return s(e,t);const c=e.localeCompare(t,void 0,{sensitivity:"base"});return 0===c?e.localeCompare(t,void 0,{sensitivity:"case"}):c});for(const r of l)n[r]=i(e[r],t,s);return n}function l(e,t,n,l,c,u=[],f=new Set,d=!1){const{keySeparator:p=".",sort:g=!0,removeUnusedKeys:h=!0,primaryLanguage:y,defaultValue:x="",pluralSeparator
|
|
1
|
+
"use strict";var e=require("node:path"),t=require("glob"),s=require("../../utils/nested-object.js"),n=require("../../utils/file-utils.js"),r=require("../../utils/default-value.js");function o(e){const t=`^${e.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*")}$`;return new RegExp(t)}function a(e,t,s,n){if(0===t.size)return!1;let r=e;const o=["zero","one","two","few","many","other"];for(const e of o){if(r.endsWith(`${s}${e}`)){r=r.slice(0,-(s.length+e.length));break}if(r.endsWith(`${s}ordinal${s}${e}`)){r=r.slice(0,-(s.length+7+s.length+e.length));break}}const a=r.split(n);if(a.length>1)for(let e=1;e<a.length;e++){const s=a.slice(0,-e).join(n);if(t.has(s))return!0}return!!t.has(r)}function i(e,t,s){if("object"!=typeof e||null===e||Array.isArray(e))return e;const n={},r=t?.extract?.pluralSeparator??"_",o=["zero","one","two","few","many","other"],a=o.map(e=>`ordinal${r}${e}`),l=Object.keys(e).sort((e,t)=>{const n=e=>{for(const t of a)if(e.endsWith(`${r}${t}`)){return{base:e.slice(0,-(r.length+t.length)),form:t,isOrdinal:!0,isPlural:!0,fullKey:e}}for(const t of o)if(e.endsWith(`${r}${t}`)){return{base:e.slice(0,-(r.length+t.length)),form:t,isOrdinal:!1,isPlural:!0,fullKey:e}}return{base:e,form:"",isOrdinal:!1,isPlural:!1,fullKey:e}},i=n(e),l=n(t);if(i.isPlural&&l.isPlural){const e=s?s(i.base,l.base):i.base.localeCompare(l.base,void 0,{sensitivity:"base"});if(0!==e)return e;if(i.isOrdinal!==l.isOrdinal)return i.isOrdinal?1:-1;const t=i.isOrdinal?a:o,n=t.indexOf(i.form),r=t.indexOf(l.form);return-1!==n&&-1!==r?n-r:i.form.localeCompare(l.form)}if(s)return s(e,t);const c=e.localeCompare(t,void 0,{sensitivity:"base"});return 0===c?e.localeCompare(t,void 0,{sensitivity:"case"}):c});for(const r of l)n[r]=i(e[r],t,s);return n}function l(e,t,n,l,c,u=[],f=new Set,d=!1){const{keySeparator:p=".",sort:g=!0,removeUnusedKeys:h=!0,primaryLanguage:y,defaultValue:x="",pluralSeparator:$="_",contextSeparator:m="_",preserveContextVariants:S=!1}=n.extract,O=new Set;if(S)for(const{keyAcceptingContext:t}of e)t&&O.add(t);const v=new Set;let w=[],N=[];try{const e=new Intl.PluralRules(l,{type:"cardinal"}),t=new Intl.PluralRules(l,{type:"ordinal"});w=e.resolvedOptions().pluralCategories,N=t.resolvedOptions().pluralCategories,w.forEach(e=>v.add(e)),t.resolvedOptions().pluralCategories.forEach(e=>v.add(`ordinal_${e}`))}catch(e){const t=y||"en",s=new Intl.PluralRules(t,{type:"cardinal"}),n=new Intl.PluralRules(t,{type:"ordinal"});w=s.resolvedOptions().pluralCategories,N=n.resolvedOptions().pluralCategories,w.forEach(e=>v.add(e)),n.resolvedOptions().pluralCategories.forEach(e=>v.add(`ordinal_${e}`))}const b=n.extract.preservePatterns||[],k="string"==typeof n.extract.nsSeparator?n.extract.nsSeparator:":",j=e=>{if(u.some(t=>t.test(e)))return!0;for(const t of b)if("string"==typeof t){if(t.endsWith(`${k}*`)){const e=t.slice(0,-(k.length+1));if("*"===e||c&&e===c)return!0}if(t.includes(k)&&c){const[s,n]=t.split(k);if(s===c){if(o(n).test(e))return!0}}}return!1},P=e.filter(({key:e,hasCount:t,isOrdinal:s})=>{if((e=>{if(u.some(t=>t.test(e)))return!0;for(const e of b)if("string"==typeof e&&e.endsWith(`${k}*`)){const t=e.slice(0,-(k.length+1));if("*"===t||c&&t===c)return!0}return!1})(e))return!1;if(!t)return!0;const n=e.split($);if(t&&1===n.length)return!0;if(1===w.length&&"other"===w[0]&&1===n.length)return!0;if(s&&n.includes("ordinal")){const e=n[n.length-1];return v.has(`ordinal_${e}`)}if(t){const e=n[n.length-1];return v.has(e)}return!0}),V=new Set;for(const e of P)if(e.isExpandedPlural){const t=String(e.key).split($);t.length>=3&&"ordinal"===t[t.length-2]?V.add(t.slice(0,-2).join($)):V.add(t.slice(0,-1).join($))}let C=h?{}:JSON.parse(JSON.stringify(t));const W=s.getNestedKeys(t,p??".");for(const e of W){const n=j(e),r=!n&&a(e,O,$,m);if(n||S&&r){const n=s.getNestedValue(t,e,p??".");s.setNestedValue(C,e,n,p??".")}}if(h){const e=s.getNestedKeys(t,p??".");for(const n of e){const e=n.split($);if("zero"===e[e.length-1]){const r=e.slice(0,-1).join($);if(P.some(({key:e})=>e.split($).slice(0,-1).join($)===r)){const e=s.getNestedValue(t,n,p??".");s.setNestedValue(C,n,e,p??".")}}}}for(const{key:e,defaultValue:o,explicitDefault:a,hasCount:i,isExpandedPlural:u,isOrdinal:g}of P){if(i&&!u){const t=String(e).split($);let s=e;if(t.length>=3&&"ordinal"===t[t.length-2]?s=t.slice(0,-2).join($):t.length>=2&&(s=t.slice(0,-1).join($)),V.has(s))continue}if(i&&!u){if(1===String(e).split($).length&&l!==y){const a=e;if(V.has(a));else{const e=g?N:w;for(const i of e){const e=g?`${a}${$}ordinal${$}${i}`:`${a}${$}${i}`,u=s.getNestedValue(t,e,p??".");if(void 0===u){let t;t="string"==typeof o?o:r.resolveDefaultValue(x,String(a),c||n?.extract?.defaultNS||"translation",l,o),s.setNestedValue(C,e,t,p??".")}else s.setNestedValue(C,e,u,p??".")}}continue}}const h=s.getNestedValue(t,e,p??"."),S=!1===p||!P.some(t=>t.key!==e&&t.key.startsWith(`${e}${p}`)),O="object"==typeof h&&null!==h&&(f.has(e)||!o||o===e),v="object"==typeof h&&null!==h&&S&&!f.has(e)&&!O;if(O){s.setNestedValue(C,e,h,p??".");continue}let b;if(void 0===h||v)if(l===y)if(d){const t=o&&(o===e||k&&c&&o===`${c}${k}${e}`||e!==o&&(e.startsWith(o+$)||e.startsWith(o+m)));b=o&&!t?o:r.resolveDefaultValue(x,e,c||n?.extract?.defaultNS||"translation",l,o)}else b=o||e;else b=r.resolveDefaultValue(x,e,c||n?.extract?.defaultNS||"translation",l,o);else if(l===y&&d){const t=o&&(o===e||k&&c&&o===`${c}${k}${e}`||e!==o&&(e.startsWith(o+$)||e.startsWith(o+m)));b=(e.includes($)||e.includes(m))&&!a?h:o&&!t?o:h}else b=h;s.setNestedValue(C,e,b,p??".")}if(!0===g)return i(C,n);if("function"==typeof g){const t={},s=Object.keys(C),r=new Map;for(const t of e)if(r.set(String(t.key),t),p){const e=String(t.key).split(p)[0];r.has(e)||r.set(e,t)}const o=(e,t)=>{const s=r.get(e),n=r.get(t);if(s&&n)return g(s,n);return g({key:e},{key:t})};s.sort(o);for(const e of s)t[e]=i(C[e],n,o);C=t}return C}exports.getTranslations=async function(s,r,a,{syncPrimaryWithDefaults:i=!1}={}){a.extract.primaryLanguage||=a.locales[0]||"en",a.extract.secondaryLanguages||=a.locales.filter(e=>e!==a?.extract?.primaryLanguage);const c=[...a.extract.preservePatterns||[]],u=a.extract.indentation??2;for(const e of r)c.push(`${e}.*`);const f=c.map(o),d="__no_namespace__",p=new Map;for(const e of s.values()){const t=e.nsIsImplicit&&!1===a.extract.defaultNS?d:String(e.ns??a.extract.defaultNS??"translation");p.has(t)||p.set(t,[]),p.get(t).push(e)}const g=[],h=Array.isArray(a.extract.ignore)?a.extract.ignore:a.extract.ignore?[a.extract.ignore]:[];for(const s of a.locales){if(a.extract.mergeNamespaces||"string"==typeof a.extract.output&&!a.extract.output.includes("{{namespace}}")){const t={},o=n.getOutputPath(a.extract.output,s),c=e.resolve(process.cwd(),o),h=await n.loadTranslationFile(c)||{},y=Object.keys(h),x=!1!==a.extract.defaultNS&&y.some(e=>{const t=h[e];return"object"==typeof t&&null!==t&&!Array.isArray(t)})?new Set([...p.keys(),...y]):new Set([...p.keys(),d]);for(const e of x){const n=p.get(e)||[];if(e===d){const e=l(n,h,a,s,void 0,f,r,i);Object.assign(t,e)}else{const o=h[e]||{};t[e]=l(n,o,a,s,e,f,r,i)}}const $=JSON.stringify(h,null,u),m=JSON.stringify(t,null,u);g.push({path:c,updated:m!==$,newTranslations:t,existingTranslations:h})}else{const o=new Set(p.keys()),c=n.getOutputPath(a.extract.output,s,"*").replace(/\\/g,"/"),d=await t.glob(c,{ignore:h});for(const t of d)o.add(e.basename(t,e.extname(t)));for(const t of o){const o=p.get(t)||[],c=n.getOutputPath(a.extract.output,s,t),d=e.resolve(process.cwd(),c),h=await n.loadTranslationFile(d)||{},y=l(o,h,a,s,t,f,r,i),x=JSON.stringify(h,null,u),$=JSON.stringify(y,null,u);g.push({path:d,updated:$!==x,newTranslations:y,existingTranslations:h})}}}return g};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("./ast-utils.js");function t(t){if(t)return"StringLiteral"===t.type?t.value:"TemplateLiteral"===t.type&&e.isSimpleTemplateLiteral(t)?t.quasis[0].cooked:void 0}function n(e){return"StringLiteral"===e.value?.type?e.value.value:"JSXExpressionContainer"===e.value?.type?t(e.value.expression):void 0}exports.extractFromTransComponent=function(i,r){const s=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),o=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),p=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),a=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let l;p||"JSXAttribute"!==a?.type||"JSXExpressionContainer"!==a.value?.type||"ObjectExpression"!==a.value.expression.type||(l=e.getObjectPropValueExpression(a.value.expression,"count"));const u=!!p||!!l,y=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),c="JSXAttribute"===y?.type&&"JSXExpressionContainer"===y.value?.type&&"ObjectExpression"===y.value.expression.type?y.value.expression:void 0,f=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),g=!!f,d=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===d?.type&&"JSXExpressionContainer"===d.value?.type?d.value.expression:"JSXAttribute"===d?.type&&"StringLiteral"===d.value?.type?d.value:void 0;const S=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let x;x="JSXAttribute"===S?.type?n(S):void 0,c&&(void 0===x&&(x=e.getObjectPropValue(c,"ns")),void 0===v&&(v=e.getObjectPropValueExpression(c,"context")));const m=function(e,n){if(!e||0===e.length)return"";const i=new Set(n.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),r=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function s(e,n,o=!1,p=!1){if(!e||!e.length)return;const a=p&&e.filter(e=>e&&"JSXElement"===e.type&&"p"===e.opening?.name?.value).length>1;let l=0,u=e.length-1;for(;l<=u&&r(e[l]);)l++;for(;u>=l&&r(e[u]);)u--;const y=l<=u?e.slice(l,u+1):[],c=y.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<y.length;e++){const p=y[e];if(p)if("JSXText"!==p.type){if("JSXExpressionContainer"===p.type){if(o&&!c&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}const n=t(p.expression);if(void 0!==n){if(!(/^\s*$/.test(n)&&!n.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const i=t(p.expression);if(void 0!==i){const t=/^\s*$/.test(i)&&!i.includes("\n"),s=y[e-1],o=y[e+1];if(t){const t=y[e+2];if(o&&"JSXText"===o.type&&r(o)&&t&&("JSXElement"===t.type||"JSXFragment"===t.type)){const t=y[e-1],n=y[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&r(o))continue;const i=!o||"JSXText"===o.type&&!r(o);if(s&&"JSXText"===s.type&&i){const e=n[n.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(s&&("JSXElement"===s.type||"JSXFragment"===s.type)&&o&&"JSXText"===o.type&&r(o))continue}}n.push(p);continue}if("JSXElement"===p.type){const e=p.opening&&p.opening.name&&"Identifier"===p.opening.name.type?p.opening.name.value:void 0;if(e&&i.has(e)){const i=p.opening&&Array.isArray(p.opening.attributes)&&p.opening.attributes.length>0,r=p.children||[],o=1===r.length&&("JSXText"===r[0]?.type||"JSXExpressionContainer"===r[0]?.type&&void 0!==t(r[0].expression)),l=!r.length,u=o;i&&!o?(n.push(p),s(p.children||[],n,!0)):l?n.push(p):u||("p"===e&&a?(n.push(p),s(p.children||[],n,!0,!1)):s(p.children||[],n,!1,!1));continue}n.push(p),s(p.children||[],n,!0);continue}"JSXFragment"!==p.type||s(p.children||[],n,o)}else{if(o&&!c)continue;if(o&&r(p))continue;if(r(p)){const t=y[e-1],i=y[e+1];if(t&&("JSXElement"===t.type||"JSXFragment"===t.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const r=n[n.length-1],s=y[e-1];if(r){if(s&&"JSXExpressionContainer"===s.type)continue;if("JSXText"===r.type&&s&&"JSXText"===s.type){r.value=String(r.value)+p.value;continue}}}if(o&&c&&0===e)continue;n.push(p)}}}const o=[];s(e,o,!1,!0);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function a(e,n,s=!1){if(!e||0===e.length)return"";let l="",u=0;for(let y=0;y<e.length;y++){const c=e[y];if(c){if("JSXText"===c.type){if(r(c))continue;const t=e[y+1];if(/\n\s*$/.test(c.value)&&t&&"JSXElement"===t.type){const t=c.value.replace(/\n\s*$/,"");if(t.trim()){const n=e[y+2],i=n&&"JSXText"===n.type&&!r(n)&&/[a-zA-Z0-9]/.test(n.value),s=/^\s/.test(t),o=t.trim(),p=s?" "+o:o;l+=/\s\n/.test(c.value)||i?p+" ":p;continue}}l+=c.value;continue}if("JSXExpressionContainer"===c.type){const e=c.expression;if(!e)continue;const n=t(e);if(void 0!==n)l+=n;else if("Identifier"===e.type)l+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?l+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?l+=`{{${t.value}}}`:l+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?l+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?l+=`{{${e.callee.value}}}`:l+="{{value}}";continue}if("JSXElement"===c.type){let f;c.opening&&c.opening.name&&"Identifier"===c.opening.name.type&&(f=c.opening.name.value);const g=s?u:void 0;if(s&&"JSXElement"===c.type&&u++,f&&i.has(f)){const s=c.opening&&Array.isArray(c.opening.attributes)&&c.opening.attributes.length>0,u=c.children||[],d=u.length>0,v=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==t(u[0].expression));if(!d||v){const t=v?a(u,void 0):"";if(""!==String(t).trim())l+=`<${f}>${t}</${f}>`;else{const t=e[y-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(l=l.replace(/\s+$/,"")),l+=`<${f} />`}}else if(s&&!v){const e=u;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(c),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const t=new Map;let r=0;for(const n of e)if(n&&"JSXElement"===n.type){const e=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(e&&i.has(e)){const e=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,i=n.children||[],s=1===i.length&&"JSXText"===i[0]?.type;(e||i.length&&!s)&&t.set(n,r++)}else t.set(n,r++)}const s=n&&n.has(c)?n.get(c):o.indexOf(c),u=a(e,t.size?t:void 0);l+=`<${s}>${p(u)}</${s}>`}}else{const e=o.indexOf(c);if(-1!==e){const n=void 0!==g?g:e;if((()=>{let e=!1;for(const t of u)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==o.indexOf(t))return e;if("JSXText"===t.type&&-1!==o.indexOf(t)){if(r(t))continue;if(!e)return!0;if(u.slice(u.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=a(u,void 0,!1);l+=`<${n}>${p(e)}</${n}>`;continue}const s=new Map;let y=n;for(const e of u)if(e&&"JSXElement"===e.type){const n=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(n&&i.has(n)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,i=e.children||[],r=1===i.length&&("JSXText"===i[0]?.type||"JSXExpressionContainer"===i[0]?.type&&void 0!==t(i[0].expression));!n&&(!i.length||r)||s.set(e,y++)}else s.set(e,y++)}const c=a(u,s.size>0?s:void 0,!1);l+=`<${n}>${p(c)}</${n}>`}else{const e=a(u,void 0,!1);l+=`<${f}>${p(e)}</${f}>`}}}else{const e=c.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(c),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const t=new Map;let r=0;for(const n of e)if(n&&"JSXElement"===n.type){const e=n.opening&&n.opening.name&&"Identifier"===n.opening.name.type?n.opening.name.value:void 0;if(e&&i.has(e)){const e=n.opening&&Array.isArray(n.opening.attributes)&&n.opening.attributes.length>0,i=n.children||[],s=1===i.length&&"JSXText"===i[0]?.type;!e&&(!i.length||s)||t.set(n,r++)}else t.set(n,r++)}const s=n&&n.has(c)?n.get(c):o.indexOf(c),u=a(e,t.size?t:void 0);l+=`<${s}>${p(u)}</${s}>`}}continue}"JSXFragment"!==c.type||(l+=a(c.children||[]))}}return l}const l=a(e,void 0,!0),u=String(l).replace(/<br \/>\s*\n\s*/g,"<br />").replace(/\s+/g," ");return u.replace(/\s+\./g,".").trim()}(i.children,r);let J;const X="JSXAttribute"===o?.type?n(o):void 0;if(void 0!==X)J=X;else{const e=r.extract.defaultValue;J="string"==typeof e?e:""}let h,b;if("JSXAttribute"===s?.type){if("StringLiteral"===s.value?.type){if(h=s.value,b=h.value,!b||""===b.trim())return null;if(x&&"StringLiteral"===h.type){const e=r.extract.nsSeparator??":",t=h.value;if(e&&t.startsWith(`${x}${e}`)){if(b=t.slice(`${x}${e}`.length),!b||""===b.trim())return null;h={...h,value:b}}}}else"JSXExpressionContainer"===s.value?.type&&"JSXEmptyExpression"!==s.value.expression.type&&(h=s.value.expression);if(!h)return null}return o||!b||m.trim()?!o&&m.trim()&&(J=m):J=b,{keyExpression:h,serializedChildren:m,ns:x,defaultValue:J,hasCount:u,isOrdinal:g,contextExpression:v,optionsNode:c,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})(c)}};
|
|
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),y="JSXAttribute"===c?.type&&"JSXExpressionContainer"===c.value?.type&&"ObjectExpression"===c.value.expression.type?c.value.expression:void 0,f=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),g=!!f,d=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let v="JSXAttribute"===d?.type&&"JSXExpressionContainer"===d.value?.type?d.value.expression:"JSXAttribute"===d?.type&&"StringLiteral"===d.value?.type?d.value:void 0;const S=i.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let x;x="JSXAttribute"===S?.type?n(S):void 0,y&&(void 0===x&&(x=e.getObjectPropValue(y,"ns")),void 0===v&&(v=e.getObjectPropValueExpression(y,"context")));const m=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):[],y=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&&!y&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}const n=t(p.expression);if(void 0!==n){if(!(/^\s*$/.test(n)&&!n.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const i=t(p.expression);if(void 0!==i){const t=/^\s*$/.test(i)&&!i.includes("\n"),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&&!y)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&&y&&0===e)continue;n.push(p)}}}const o=[];r(e,o,!1,!0);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function a(e,n,r=!1){if(!e||0===e.length)return"";let l="",u=0;for(let c=0;c<e.length;c++){const y=e[c];if(y){if("JSXText"===y.type){if(s(y))continue;const t=e[c+1],n=e[c-1];if(n&&"JSXElement"===n.type){const e="Identifier"===n.opening?.name?.type?n.opening.name.value:void 0,t=e&&i.has(e),s=0===(n.children||[]).length;if(t&&s&&/^\s*\n\s*/.test(y.value)){const e=y.value.replace(/^\s*\n\s*/,"");if(e){l+=e;continue}continue}}if(/\n\s*$/.test(y.value)&&t&&"JSXElement"===t.type){const n=y.value.replace(/\n\s*$/,"");if(n.trim()){const r="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,o=r&&i.has(r),p=(t.children||[]).length>0,a=/\s\n/.test(y.value),u=e[c+2],f=u&&"JSXText"===u.type&&!s(u)&&/[a-zA-Z0-9]/.test(u.value),g=/^\s/.test(n),d=n.trim(),v=g?" "+d:d;l+=a||o&&p||!o&&f?v+" ":v;continue}}l+=y.value;continue}if("JSXExpressionContainer"===y.type){const e=y.expression;if(!e)continue;const n=t(e);if(void 0!==n)l+=n;else if("Identifier"===e.type)l+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?l+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?l+=`{{${t.value}}}`:l+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?l+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?l+=`{{${e.callee.value}}}`:l+="{{value}}";continue}if("JSXElement"===y.type){let f;y.opening&&y.opening.name&&"Identifier"===y.opening.name.type&&(f=y.opening.name.value);const g=r?u:void 0;if(r&&"JSXElement"===y.type&&u++,f&&i.has(f)){const r=y.opening&&Array.isArray(y.opening.attributes)&&y.opening.attributes.length>0,u=y.children||[],d=u.length>0,v=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==t(u[0].expression));if(!d||v){const t=v?a(u,void 0):"";if(""!==String(t).trim())l+=`<${f}>${t}</${f}>`;else{const t=e[c-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(l=l.replace(/\s+$/,"")),l+=`<${f} />`}}else if(r&&!v){const e=u;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(y),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const t=new Map;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=n&&n.has(y)?n.get(y):o.indexOf(y),u=a(e,t.size?t:void 0);l+=`<${r}>${p(u)}</${r}>`}}else{const e=o.indexOf(y);if(-1!==e){const n=void 0!==g?g:e;if((()=>{let e=!1;for(const t of u)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==o.indexOf(t))return e;if("JSXText"===t.type&&-1!==o.indexOf(t)){if(s(t))continue;if(!e)return!0;if(u.slice(u.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=a(u,void 0,!1);l+=`<${n}>${p(e)}</${n}>`;continue}const r=new Map;let c=n;for(const e of u)if(e&&"JSXElement"===e.type){const n=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(n&&i.has(n)){const n=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,i=e.children||[],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,c++)}else r.set(e,c++)}const y=a(u,r.size>0?r:void 0,!1);l+=`<${n}>${p(y)}</${n}>`}else{const e=a(u,void 0,!1);l+=`<${f}>${p(e)}</${f}>`}}}else{const e=y.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(y),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const t=new Map;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=n&&n.has(y)?n.get(y):o.indexOf(y),u=a(e,t.size?t:void 0);l+=`<${r}>${p(u)}</${r}>`}}continue}"JSXFragment"!==y.type||(l+=a(y.children||[]))}}return l}const l=a(e,void 0,!0),u=String(l).replace(/<br \/>\s*\n\s*/g,"<br />").replace(/\s+/g," ");return u.replace(/\s+\./g,".").trim()}(i.children,s);let J;const X="JSXAttribute"===o?.type?n(o):void 0;if(void 0!==X)J=X;else{const e=s.extract.defaultValue;J="string"==typeof e?e:""}let h,E;if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(h=r.value,E=h.value,!E||""===E.trim())return null;if(x&&"StringLiteral"===h.type){const e=s.extract.nsSeparator??":",t=h.value;if(e&&t.startsWith(`${x}${e}`)){if(E=t.slice(`${x}${e}`.length),!E||""===E.trim())return null;h={...h,value:E}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(h=r.value.expression);if(!h)return null}return o||!E||m.trim()?!o&&m.trim()&&(J=m):J=E,{keyExpression:h,serializedChildren:m,ns:x,defaultValue:J,hasCount:u,isOrdinal:g,contextExpression:v,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)}};
|
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 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.24.
|
|
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 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.24.3"),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.").action(async o=>{try{const t=x.opts().config,i=await a(t),r=async()=>{const e=await s(i,{isWatchMode:!!o.watch,isDryRun:!!o.dryRun,syncPrimaryWithDefaults:!!o.syncPrimary});return o.ci&&!e?(console.log("✅ No files were updated."),process.exit(0)):o.ci&&e&&(console.error("❌ Some files were updated. This should not happen in CI mode."),process.exit(1)),e};if(await r(),o.watch){console.log("\nWatching for changes...");const o=await z(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 z(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 z(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,"*")]:[],z=async(o=[])=>{const e=j(o),n=await Promise.all(e.map(o=>t(o||"",{nodir:!0})));return Array.from(new Set(n.flat()))};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{resolve as t,basename as e,extname as n}from"node:path";import{glob as r}from"glob";import{getNestedKeys as s,getNestedValue as o,setNestedValue as i}from"../../utils/nested-object.js";import{getOutputPath as a,loadTranslationFile as l}from"../../utils/file-utils.js";import{resolveDefaultValue as c}from"../../utils/default-value.js";function f(t){const e=`^${t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*")}$`;return new RegExp(e)}function u(t,e,n,r){if(0===e.size)return!1;let s=t;const o=["zero","one","two","few","many","other"];for(const t of o){if(s.endsWith(`${n}${t}`)){s=s.slice(0,-(n.length+t.length));break}if(s.endsWith(`${n}ordinal${n}${t}`)){s=s.slice(0,-(n.length+7+n.length+t.length));break}}const i=s.split(r);if(i.length>1)for(let t=1;t<i.length;t++){const n=i.slice(0,-t).join(r);if(e.has(n))return!0}return!!e.has(s)}function p(t,e,n){if("object"!=typeof t||null===t||Array.isArray(t))return t;const r={},s=e?.extract?.pluralSeparator??"_",o=["zero","one","two","few","many","other"],i=o.map(t=>`ordinal${s}${t}`),a=Object.keys(t).sort((t,e)=>{const r=t=>{for(const e of i)if(t.endsWith(`${s}${e}`)){return{base:t.slice(0,-(s.length+e.length)),form:e,isOrdinal:!0,isPlural:!0,fullKey:t}}for(const e of o)if(t.endsWith(`${s}${e}`)){return{base:t.slice(0,-(s.length+e.length)),form:e,isOrdinal:!1,isPlural:!0,fullKey:t}}return{base:t,form:"",isOrdinal:!1,isPlural:!1,fullKey:t}},a=r(t),l=r(e);if(a.isPlural&&l.isPlural){const t=n?n(a.base,l.base):a.base.localeCompare(l.base,void 0,{sensitivity:"base"});if(0!==t)return t;if(a.isOrdinal!==l.isOrdinal)return a.isOrdinal?1:-1;const e=a.isOrdinal?i:o,r=e.indexOf(a.form),s=e.indexOf(l.form);return-1!==r&&-1!==s?r-s:a.form.localeCompare(l.form)}if(n)return n(t,e);const c=t.localeCompare(e,void 0,{sensitivity:"base"});return 0===c?t.localeCompare(e,void 0,{sensitivity:"case"}):c});for(const s of a)r[s]=p(t[s],e,n);return r}function d(t,e,n,r,a,l=[],d=new Set,g=!1){const{keySeparator:h=".",sort:y=!0,removeUnusedKeys:m=!0,primaryLanguage:x,defaultValue
|
|
1
|
+
import{resolve as t,basename as e,extname as n}from"node:path";import{glob as r}from"glob";import{getNestedKeys as s,getNestedValue as o,setNestedValue as i}from"../../utils/nested-object.js";import{getOutputPath as a,loadTranslationFile as l}from"../../utils/file-utils.js";import{resolveDefaultValue as c}from"../../utils/default-value.js";function f(t){const e=`^${t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*")}$`;return new RegExp(e)}function u(t,e,n,r){if(0===e.size)return!1;let s=t;const o=["zero","one","two","few","many","other"];for(const t of o){if(s.endsWith(`${n}${t}`)){s=s.slice(0,-(n.length+t.length));break}if(s.endsWith(`${n}ordinal${n}${t}`)){s=s.slice(0,-(n.length+7+n.length+t.length));break}}const i=s.split(r);if(i.length>1)for(let t=1;t<i.length;t++){const n=i.slice(0,-t).join(r);if(e.has(n))return!0}return!!e.has(s)}function p(t,e,n){if("object"!=typeof t||null===t||Array.isArray(t))return t;const r={},s=e?.extract?.pluralSeparator??"_",o=["zero","one","two","few","many","other"],i=o.map(t=>`ordinal${s}${t}`),a=Object.keys(t).sort((t,e)=>{const r=t=>{for(const e of i)if(t.endsWith(`${s}${e}`)){return{base:t.slice(0,-(s.length+e.length)),form:e,isOrdinal:!0,isPlural:!0,fullKey:t}}for(const e of o)if(t.endsWith(`${s}${e}`)){return{base:t.slice(0,-(s.length+e.length)),form:e,isOrdinal:!1,isPlural:!0,fullKey:t}}return{base:t,form:"",isOrdinal:!1,isPlural:!1,fullKey:t}},a=r(t),l=r(e);if(a.isPlural&&l.isPlural){const t=n?n(a.base,l.base):a.base.localeCompare(l.base,void 0,{sensitivity:"base"});if(0!==t)return t;if(a.isOrdinal!==l.isOrdinal)return a.isOrdinal?1:-1;const e=a.isOrdinal?i:o,r=e.indexOf(a.form),s=e.indexOf(l.form);return-1!==r&&-1!==s?r-s:a.form.localeCompare(l.form)}if(n)return n(t,e);const c=t.localeCompare(e,void 0,{sensitivity:"base"});return 0===c?t.localeCompare(e,void 0,{sensitivity:"case"}):c});for(const s of a)r[s]=p(t[s],e,n);return r}function d(t,e,n,r,a,l=[],d=new Set,g=!1){const{keySeparator:h=".",sort:y=!0,removeUnusedKeys:m=!0,primaryLanguage:x,defaultValue:$="",pluralSeparator:S="_",contextSeparator:O="_",preserveContextVariants:w=!1}=n.extract,b=new Set;if(w)for(const{keyAcceptingContext:e}of t)e&&b.add(e);const v=new Set;let k=[],j=[];try{const t=new Intl.PluralRules(r,{type:"cardinal"}),e=new Intl.PluralRules(r,{type:"ordinal"});k=t.resolvedOptions().pluralCategories,j=e.resolvedOptions().pluralCategories,k.forEach(t=>v.add(t)),e.resolvedOptions().pluralCategories.forEach(t=>v.add(`ordinal_${t}`))}catch(t){const e=x||"en",n=new Intl.PluralRules(e,{type:"cardinal"}),r=new Intl.PluralRules(e,{type:"ordinal"});k=n.resolvedOptions().pluralCategories,j=r.resolvedOptions().pluralCategories,k.forEach(t=>v.add(t)),r.resolvedOptions().pluralCategories.forEach(t=>v.add(`ordinal_${t}`))}const C=n.extract.preservePatterns||[],P="string"==typeof n.extract.nsSeparator?n.extract.nsSeparator:":",N=t=>{if(l.some(e=>e.test(t)))return!0;for(const e of C)if("string"==typeof e){if(e.endsWith(`${P}*`)){const t=e.slice(0,-(P.length+1));if("*"===t||a&&t===a)return!0}if(e.includes(P)&&a){const[n,r]=e.split(P);if(n===a){if(f(r).test(t))return!0}}}return!1},W=t.filter(({key:t,hasCount:e,isOrdinal:n})=>{if((t=>{if(l.some(e=>e.test(t)))return!0;for(const t of C)if("string"==typeof t&&t.endsWith(`${P}*`)){const e=t.slice(0,-(P.length+1));if("*"===e||a&&e===a)return!0}return!1})(t))return!1;if(!e)return!0;const r=t.split(S);if(e&&1===r.length)return!0;if(1===k.length&&"other"===k[0]&&1===r.length)return!0;if(n&&r.includes("ordinal")){const t=r[r.length-1];return v.has(`ordinal_${t}`)}if(e){const t=r[r.length-1];return v.has(t)}return!0}),_=new Set;for(const t of W)if(t.isExpandedPlural){const e=String(t.key).split(S);e.length>=3&&"ordinal"===e[e.length-2]?_.add(e.slice(0,-2).join(S)):_.add(e.slice(0,-1).join(S))}let A=m?{}:JSON.parse(JSON.stringify(e));const E=s(e,h??".");for(const t of E){const n=N(t),r=!n&&u(t,b,S,O);if(n||w&&r){const n=o(e,t,h??".");i(A,t,n,h??".")}}if(m){const t=s(e,h??".");for(const n of t){const t=n.split(S);if("zero"===t[t.length-1]){const r=t.slice(0,-1).join(S);if(W.some(({key:t})=>t.split(S).slice(0,-1).join(S)===r)){const t=o(e,n,h??".");i(A,n,t,h??".")}}}}for(const{key:t,defaultValue:s,explicitDefault:l,hasCount:f,isExpandedPlural:u,isOrdinal:p}of W){if(f&&!u){const e=String(t).split(S);let n=t;if(e.length>=3&&"ordinal"===e[e.length-2]?n=e.slice(0,-2).join(S):e.length>=2&&(n=e.slice(0,-1).join(S)),_.has(n))continue}if(f&&!u){if(1===String(t).split(S).length&&r!==x){const l=t;if(_.has(l));else{const t=p?j:k;for(const f of t){const t=p?`${l}${S}ordinal${S}${f}`:`${l}${S}${f}`,u=o(e,t,h??".");if(void 0===u){let e;e="string"==typeof s?s:c($,String(l),a||n?.extract?.defaultNS||"translation",r,s),i(A,t,e,h??".")}else i(A,t,u,h??".")}}continue}}const y=o(e,t,h??"."),m=!1===h||!W.some(e=>e.key!==t&&e.key.startsWith(`${t}${h}`)),w="object"==typeof y&&null!==y&&(d.has(t)||!s||s===t),b="object"==typeof y&&null!==y&&m&&!d.has(t)&&!w;if(w){i(A,t,y,h??".");continue}let v;if(void 0===y||b)if(r===x)if(g){const e=s&&(s===t||P&&a&&s===`${a}${P}${t}`||t!==s&&(t.startsWith(s+S)||t.startsWith(s+O)));v=s&&!e?s:c($,t,a||n?.extract?.defaultNS||"translation",r,s)}else v=s||t;else v=c($,t,a||n?.extract?.defaultNS||"translation",r,s);else if(r===x&&g){const e=s&&(s===t||P&&a&&s===`${a}${P}${t}`||t!==s&&(t.startsWith(s+S)||t.startsWith(s+O)));v=(t.includes(S)||t.includes(O))&&!l?y:s&&!e?s:y}else v=y;i(A,t,v,h??".")}if(!0===y)return p(A,n);if("function"==typeof y){const e={},r=Object.keys(A),s=new Map;for(const e of t)if(s.set(String(e.key),e),h){const t=String(e.key).split(h)[0];s.has(t)||s.set(t,e)}const o=(t,e)=>{const n=s.get(t),r=s.get(e);if(n&&r)return y(n,r);return y({key:t},{key:e})};r.sort(o);for(const t of r)e[t]=p(A[t],n,o);A=e}return A}async function g(s,o,i,{syncPrimaryWithDefaults:c=!1}={}){i.extract.primaryLanguage||=i.locales[0]||"en",i.extract.secondaryLanguages||=i.locales.filter(t=>t!==i?.extract?.primaryLanguage);const u=[...i.extract.preservePatterns||[]],p=i.extract.indentation??2;for(const t of o)u.push(`${t}.*`);const g=u.map(f),h="__no_namespace__",y=new Map;for(const t of s.values()){const e=t.nsIsImplicit&&!1===i.extract.defaultNS?h:String(t.ns??i.extract.defaultNS??"translation");y.has(e)||y.set(e,[]),y.get(e).push(t)}const m=[],x=Array.isArray(i.extract.ignore)?i.extract.ignore:i.extract.ignore?[i.extract.ignore]:[];for(const s of i.locales){if(i.extract.mergeNamespaces||"string"==typeof i.extract.output&&!i.extract.output.includes("{{namespace}}")){const e={},n=a(i.extract.output,s),r=t(process.cwd(),n),f=await l(r)||{},u=Object.keys(f),x=!1!==i.extract.defaultNS&&u.some(t=>{const e=f[t];return"object"==typeof e&&null!==e&&!Array.isArray(e)})?new Set([...y.keys(),...u]):new Set([...y.keys(),h]);for(const t of x){const n=y.get(t)||[];if(t===h){const t=d(n,f,i,s,void 0,g,o,c);Object.assign(e,t)}else{const r=f[t]||{};e[t]=d(n,r,i,s,t,g,o,c)}}const $=JSON.stringify(f,null,p),S=JSON.stringify(e,null,p);m.push({path:r,updated:S!==$,newTranslations:e,existingTranslations:f})}else{const f=new Set(y.keys()),u=a(i.extract.output,s,"*").replace(/\\/g,"/"),h=await r(u,{ignore:x});for(const t of h)f.add(e(t,n(t)));for(const e of f){const n=y.get(e)||[],r=a(i.extract.output,s,e),f=t(process.cwd(),r),u=await l(f)||{},h=d(n,u,i,s,e,g,o,c),x=JSON.stringify(u,null,p),$=JSON.stringify(h,null,p);m.push({path:f,updated:$!==x,newTranslations:h,existingTranslations:u})}}}return m}export{g as getTranslations};
|
|
@@ -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 y=!!a||!!u,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),c="JSXAttribute"===f?.type&&"JSXExpressionContainer"===f.value?.type&&"ObjectExpression"===f.value.expression.type?f.value.expression:void 0,g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),d=!!g,v=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===v?.type&&"JSXExpressionContainer"===v.value?.type?v.value.expression:"JSXAttribute"===v?.type&&"StringLiteral"===v.value?.type?v.value:void 0;const x=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let J;J="JSXAttribute"===x?.type?s(x):void 0,c&&(void 0===J&&(J=t(c,"ns")),void 0===S&&(S=e(c,"context")));const X=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),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 y=l<=u?e.slice(l,u+1):[],f=y.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<y.length;e++){const p=y[e];if(p)if("JSXText"!==p.type){if("JSXExpressionContainer"===p.type){if(o&&!f&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}const t=i(p.expression);if(void 0!==t){if(!(/^\s*$/.test(t)&&!t.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const n=i(p.expression);if(void 0!==n){const i=/^\s*$/.test(n)&&!n.includes("\n"),r=y[e-1],o=y[e+1];if(i){const n=y[e+2];if(o&&"JSXText"===o.type&&s(o)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=y[e-1],n=y[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(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=y[e-1],i=y[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=y[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=[];r(e,o,!1,!0);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function a(e,t,r=!1){if(!e||0===e.length)return"";let l="",u=0;for(let y=0;y<e.length;y++){const f=e[y];if(f){if("JSXText"===f.type){if(s(f))continue;const t=e[y+1];if(/\n\s*$/.test(f.value)&&t&&"JSXElement"===t.type){const t=f.value.replace(/\n\s*$/,"");if(t.trim()){const n=e[y+2],i=n&&"JSXText"===n.type&&!s(n)&&/[a-zA-Z0-9]/.test(n.value),r=/^\s/.test(t),o=t.trim(),p=r?" "+o:o;l+=/\s\n/.test(f.value)||i?p+" ":p;continue}}l+=f.value;continue}if("JSXExpressionContainer"===f.type){const e=f.expression;if(!e)continue;const t=i(e);if(void 0!==t)l+=t;else if("Identifier"===e.type)l+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?l+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?l+=`{{${t.value}}}`:l+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?l+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?l+=`{{${e.callee.value}}}`:l+="{{value}}";continue}if("JSXElement"===f.type){let c;f.opening&&f.opening.name&&"Identifier"===f.opening.name.type&&(c=f.opening.name.value);const g=r?u:void 0;if(r&&"JSXElement"===f.type&&u++,c&&n.has(c)){const r=f.opening&&Array.isArray(f.opening.attributes)&&f.opening.attributes.length>0,u=f.children||[],d=u.length>0,v=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==i(u[0].expression));if(!d||v){const t=v?a(u,void 0):"";if(""!==String(t).trim())l+=`<${c}>${t}</${c}>`;else{const t=e[y-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(l=l.replace(/\s+$/,"")),l+=`<${c} />`}}else if(r&&!v){const e=u;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const i=new Map;let s=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||[],r=1===n.length&&"JSXText"===n[0]?.type;(e||n.length&&!r)&&i.set(t,s++)}else i.set(t,s++)}const r=t&&t.has(f)?t.get(f):o.indexOf(f),u=a(e,i.size?i:void 0);l+=`<${r}>${p(u)}</${r}>`}}else{const e=o.indexOf(f);if(-1!==e){const t=void 0!==g?g:e;if((()=>{let e=!1;for(const t of u)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==o.indexOf(t))return e;if("JSXText"===t.type&&-1!==o.indexOf(t)){if(s(t))continue;if(!e)return!0;if(u.slice(u.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=a(u,void 0,!1);l+=`<${t}>${p(e)}</${t}>`;continue}const r=new Map;let y=t;for(const e of u)if(e&&"JSXElement"===e.type){const t=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(t&&n.has(t)){const t=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,n=e.children||[],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,y++)}else r.set(e,y++)}const f=a(u,r.size>0?r:void 0,!1);l+=`<${t}>${p(f)}</${t}>`}else{const e=a(u,void 0,!1);l+=`<${c}>${p(e)}</${c}>`}}}else{const e=f.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==o.indexOf(e))){const t=o.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const i=new Map;let s=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||[],r=1===n.length&&"JSXText"===n[0]?.type;!e&&(!n.length||r)||i.set(t,s++)}else i.set(t,s++)}const r=t&&t.has(f)?t.get(f):o.indexOf(f),u=a(e,i.size?i:void 0);l+=`<${r}>${p(u)}</${r}>`}}continue}"JSXFragment"!==f.type||(l+=a(f.children||[]))}}return l}const l=a(e,void 0,!0),u=String(l).replace(/<br \/>\s*\n\s*/g,"<br />").replace(/\s+/g," ");return u.replace(/\s+\./g,".").trim()}(n.children,r);let m;const h="JSXAttribute"===p?.type?s(p):void 0;if(void 0!==h)m=h;else{const e=r.extract.defaultValue;m="string"==typeof e?e:""}let E,b;if("JSXAttribute"===o?.type){if("StringLiteral"===o.value?.type){if(E=o.value,b=E.value,!b||""===b.trim())return null;if(J&&"StringLiteral"===E.type){const e=r.extract.nsSeparator??":",t=E.value;if(e&&t.startsWith(`${J}${e}`)){if(b=t.slice(`${J}${e}`.length),!b||""===b.trim())return null;E={...E,value:b}}}}else"JSXExpressionContainer"===o.value?.type&&"JSXEmptyExpression"!==o.value.expression.type&&(E=o.value.expression);if(!E)return null}p||!b||X.trim()?!p&&X.trim()&&(m=X):m=b;return{keyExpression:E,serializedChildren:X,ns:J,defaultValue:m,hasCount:y,isOrdinal:d,contextExpression:S,optionsNode:c,explicitDefault:void 0!==h||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(c)}}export{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 o(n,o){const r=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"i18nKey"===e.name.value),p=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"defaults"===e.name.value),a=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"count"===e.name.value),l=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"values"===e.name.value);let u;a||"JSXAttribute"!==l?.type||"JSXExpressionContainer"!==l.value?.type||"ObjectExpression"!==l.value.expression.type||(u=e(l.value.expression,"count"));const y=!!a||!!u,f=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"tOptions"===e.name.value),c="JSXAttribute"===f?.type&&"JSXExpressionContainer"===f.value?.type&&"ObjectExpression"===f.value.expression.type?f.value.expression:void 0,g=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ordinal"===e.name.value),d=!!g,v=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"context"===e.name.value);let S="JSXAttribute"===v?.type&&"JSXExpressionContainer"===v.value?.type?v.value.expression:"JSXAttribute"===v?.type&&"StringLiteral"===v.value?.type?v.value:void 0;const x=n.opening.attributes?.find(e=>"JSXAttribute"===e.type&&"Identifier"===e.name.type&&"ns"===e.name.value);let m;m="JSXAttribute"===x?.type?s(x):void 0,c&&(void 0===m&&(m=t(c,"ns")),void 0===S&&(S=e(c,"context")));const J=function(e,t){if(!e||0===e.length)return"";const n=new Set(t.extract.transKeepBasicHtmlNodesFor??["br","strong","i","p"]),s=e=>e&&"JSXText"===e.type&&/^\s*$/.test(e.value)&&e.value.includes("\n");function o(e,t,r=!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 y=l<=u?e.slice(l,u+1):[],f=y.some(e=>e&&("JSXElement"===e.type||"JSXFragment"===e.type));for(let e=0;e<y.length;e++){const p=y[e];if(p)if("JSXText"!==p.type){if("JSXExpressionContainer"===p.type){if(r&&!f&&p.expression){const e=p.expression.type;if("ObjectExpression"===e){const e=p.expression.properties&&p.expression.properties[0];if(e&&"KeyValueProperty"===e.type)continue}const t=i(p.expression);if(void 0!==t){if(!(/^\s*$/.test(t)&&!t.includes("\n")))continue}else if("Identifier"===e||"MemberExpression"===e||"CallExpression"===e)continue}const n=i(p.expression);if(void 0!==n){const i=/^\s*$/.test(n)&&!n.includes("\n"),o=y[e-1],r=y[e+1];if(i){const n=y[e+2];if(r&&"JSXText"===r.type&&s(r)&&n&&("JSXElement"===n.type||"JSXFragment"===n.type)){const t=y[e-1],n=y[e-2];if(!t||"JSXText"!==t.type&&n&&"JSXExpressionContainer"===n.type)continue}if(o&&("JSXElement"===o.type||"JSXFragment"===o.type)&&r&&"JSXText"===r.type&&s(r))continue;const i=!r||"JSXText"===r.type&&!s(r);if(o&&"JSXText"===o.type&&i){const e=t[t.length-1];if(e&&"JSXText"===e.type){e.value=String(e.value)+p.expression.value;continue}}if(o&&("JSXElement"===o.type||"JSXFragment"===o.type)&&r&&"JSXText"===r.type&&s(r))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||[],r=1===s.length&&("JSXText"===s[0]?.type||"JSXExpressionContainer"===s[0]?.type&&void 0!==i(s[0].expression)),l=!s.length,u=r;n&&!r?(t.push(p),o(p.children||[],t,!0)):l?t.push(p):u||("p"===e&&a?(t.push(p),o(p.children||[],t,!0,!1)):o(p.children||[],t,!1,!1));continue}t.push(p),o(p.children||[],t,!0);continue}"JSXFragment"!==p.type||o(p.children||[],t,r)}else{if(r&&!f)continue;if(r&&s(p))continue;if(s(p)){const n=y[e-1],i=y[e+1];if(n&&("JSXElement"===n.type||"JSXFragment"===n.type)&&i&&("JSXElement"===i.type||"JSXFragment"===i.type))continue;const s=t[t.length-1],o=y[e-1];if(s){if(o&&"JSXExpressionContainer"===o.type)continue;if("JSXText"===s.type&&o&&"JSXText"===o.type){s.value=String(s.value)+p.value;continue}}}if(r&&f&&0===e)continue;t.push(p)}}}const r=[];o(e,r,!1,!0);const p=e=>String(e).replace(/^\s*\n\s*/g,"").replace(/\s*\n\s*$/g,"");function a(e,t,o=!1){if(!e||0===e.length)return"";let l="",u=0;for(let y=0;y<e.length;y++){const f=e[y];if(f){if("JSXText"===f.type){if(s(f))continue;const t=e[y+1],i=e[y-1];if(i&&"JSXElement"===i.type){const e="Identifier"===i.opening?.name?.type?i.opening.name.value:void 0,t=e&&n.has(e),s=0===(i.children||[]).length;if(t&&s&&/^\s*\n\s*/.test(f.value)){const e=f.value.replace(/^\s*\n\s*/,"");if(e){l+=e;continue}continue}}if(/\n\s*$/.test(f.value)&&t&&"JSXElement"===t.type){const i=f.value.replace(/\n\s*$/,"");if(i.trim()){const o="Identifier"===t.opening?.name?.type?t.opening.name.value:void 0,r=o&&n.has(o),p=(t.children||[]).length>0,a=/\s\n/.test(f.value),u=e[y+2],c=u&&"JSXText"===u.type&&!s(u)&&/[a-zA-Z0-9]/.test(u.value),g=/^\s/.test(i),d=i.trim(),v=g?" "+d:d;l+=a||r&&p||!r&&c?v+" ":v;continue}}l+=f.value;continue}if("JSXExpressionContainer"===f.type){const e=f.expression;if(!e)continue;const t=i(e);if(void 0!==t)l+=t;else if("Identifier"===e.type)l+=`{{${e.value}}}`;else if("ObjectExpression"===e.type){const t=e.properties[0];t&&"KeyValueProperty"===t.type&&t.key&&"Identifier"===t.key.type?l+=`{{${t.key.value}}}`:t&&"Identifier"===t.type?l+=`{{${t.value}}}`:l+="{{value}}"}else"MemberExpression"===e.type&&e.property&&"Identifier"===e.property.type?l+=`{{${e.property.value}}}`:"CallExpression"===e.type&&"Identifier"===e.callee?.type?l+=`{{${e.callee.value}}}`:l+="{{value}}";continue}if("JSXElement"===f.type){let c;f.opening&&f.opening.name&&"Identifier"===f.opening.name.type&&(c=f.opening.name.value);const g=o?u:void 0;if(o&&"JSXElement"===f.type&&u++,c&&n.has(c)){const o=f.opening&&Array.isArray(f.opening.attributes)&&f.opening.attributes.length>0,u=f.children||[],d=u.length>0,v=1===u.length&&("JSXText"===u[0]?.type||"JSXExpressionContainer"===u[0]?.type&&void 0!==i(u[0].expression));if(!d||v){const t=v?a(u,void 0):"";if(""!==String(t).trim())l+=`<${c}>${t}</${c}>`;else{const t=e[y-1];t&&"JSXText"===t.type&&/\n\s*$/.test(t.value)&&(l=l.replace(/\s+$/,"")),l+=`<${c} />`}}else if(o&&!v){const e=u;if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==r.indexOf(e))){const t=r.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const i=new Map;let s=0;for(const t of e)if(t&&"JSXElement"===t.type){const e=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(e&&n.has(e)){const e=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,n=t.children||[],o=1===n.length&&"JSXText"===n[0]?.type;(e||n.length&&!o)&&i.set(t,s++)}else i.set(t,s++)}const o=t&&t.has(f)?t.get(f):r.indexOf(f),u=a(e,i.size?i:void 0);l+=`<${o}>${p(u)}</${o}>`}}else{const e=r.indexOf(f);if(-1!==e){const t=void 0!==g?g:e;if((()=>{let e=!1;for(const t of u)if(t)if("JSXElement"!==t.type){if("JSXExpressionContainer"===t.type&&-1!==r.indexOf(t))return e;if("JSXText"===t.type&&-1!==r.indexOf(t)){if(s(t))continue;if(!e)return!0;if(u.slice(u.indexOf(t)+1).some(e=>e&&"JSXElement"===e.type))return!0}}else e=!0;return!1})()){const e=a(u,void 0,!1);l+=`<${t}>${p(e)}</${t}>`;continue}const o=new Map;let y=t;for(const e of u)if(e&&"JSXElement"===e.type){const t=e.opening&&e.opening.name&&"Identifier"===e.opening.name.type?e.opening.name.value:void 0;if(t&&n.has(t)){const t=e.opening&&Array.isArray(e.opening.attributes)&&e.opening.attributes.length>0,n=e.children||[],s=1===n.length&&("JSXText"===n[0]?.type||"JSXExpressionContainer"===n[0]?.type&&void 0!==i(n[0].expression));!t&&(!n.length||s)||o.set(e,y++)}else o.set(e,y++)}const f=a(u,o.size>0?o:void 0,!1);l+=`<${t}>${p(f)}</${t}>`}else{const e=a(u,void 0,!1);l+=`<${c}>${p(e)}</${c}>`}}}else{const e=f.children||[];if(e.some(e=>e&&("JSXText"===e.type||"JSXExpressionContainer"===e.type)&&-1!==r.indexOf(e))){const t=r.indexOf(f),n=a(e,void 0);l+=`<${t}>${p(n)}</${t}>`}else{const i=new Map;let s=0;for(const t of e)if(t&&"JSXElement"===t.type){const e=t.opening&&t.opening.name&&"Identifier"===t.opening.name.type?t.opening.name.value:void 0;if(e&&n.has(e)){const e=t.opening&&Array.isArray(t.opening.attributes)&&t.opening.attributes.length>0,n=t.children||[],o=1===n.length&&"JSXText"===n[0]?.type;!e&&(!n.length||o)||i.set(t,s++)}else i.set(t,s++)}const o=t&&t.has(f)?t.get(f):r.indexOf(f),u=a(e,i.size?i:void 0);l+=`<${o}>${p(u)}</${o}>`}}continue}"JSXFragment"!==f.type||(l+=a(f.children||[]))}}return l}const l=a(e,void 0,!0),u=String(l).replace(/<br \/>\s*\n\s*/g,"<br />").replace(/\s+/g," ");return u.replace(/\s+\./g,".").trim()}(n.children,o);let X;const h="JSXAttribute"===p?.type?s(p):void 0;if(void 0!==h)X=h;else{const e=o.extract.defaultValue;X="string"==typeof e?e:""}let E,b;if("JSXAttribute"===r?.type){if("StringLiteral"===r.value?.type){if(E=r.value,b=E.value,!b||""===b.trim())return null;if(m&&"StringLiteral"===E.type){const e=o.extract.nsSeparator??":",t=E.value;if(e&&t.startsWith(`${m}${e}`)){if(b=t.slice(`${m}${e}`.length),!b||""===b.trim())return null;E={...E,value:b}}}}else"JSXExpressionContainer"===r.value?.type&&"JSXEmptyExpression"!==r.value.expression.type&&(E=r.value.expression);if(!E)return null}p||!b||J.trim()?!p&&J.trim()&&(X=J):X=b;return{keyExpression:E,serializedChildren:J,ns:m,defaultValue:X,hasCount:y,isOrdinal:d,contextExpression:S,optionsNode:c,explicitDefault:void 0!==h||(e=>{if(!e||!Array.isArray(e.properties))return!1;for(const t of e.properties)if(t&&"KeyValueProperty"===t.type&&t.key){const e="Identifier"===t.key.type&&t.key.value||"StringLiteral"===t.key.type&&t.key.value;if("string"==typeof e&&e.startsWith("defaultValue"))return!0}return!1})(c)}}export{o as extractFromTransComponent};
|
package/package.json
CHANGED
package/src/cli.ts
CHANGED
|
@@ -23,7 +23,7 @@ const program = new Command()
|
|
|
23
23
|
program
|
|
24
24
|
.name('i18next-cli')
|
|
25
25
|
.description('A unified, high-performance i18next CLI.')
|
|
26
|
-
.version('1.24.
|
|
26
|
+
.version('1.24.3')
|
|
27
27
|
|
|
28
28
|
// new: global config override option
|
|
29
29
|
program.option('-c, --config <path>', 'Path to i18next-cli config file (overrides detection)')
|
|
@@ -490,9 +490,9 @@ function buildNewTranslationsForNs (
|
|
|
490
490
|
// - Use defaultValue if it exists and is meaningful (not derived from key pattern)
|
|
491
491
|
// - Otherwise use empty string for new keys
|
|
492
492
|
const isDerivedDefault = defaultValue && (
|
|
493
|
-
defaultValue === key || // Exact match
|
|
494
|
-
// Check if defaultValue
|
|
495
|
-
|
|
493
|
+
defaultValue === key || // Exact match with the key itself
|
|
494
|
+
// Check if defaultValue matches the namespaced key format (namespace:key)
|
|
495
|
+
(nsSep && namespace && defaultValue === `${namespace}${nsSep}${key}`) ||
|
|
496
496
|
// For variant keys (plural/context), check if defaultValue is the base
|
|
497
497
|
(key !== defaultValue &&
|
|
498
498
|
(key.startsWith(defaultValue + pluralSeparator) ||
|
|
@@ -513,13 +513,13 @@ function buildNewTranslationsForNs (
|
|
|
513
513
|
if (locale === primaryLanguage && syncPrimaryWithDefaults) {
|
|
514
514
|
// Only update when we have a meaningful defaultValue that's not derived from the key pattern.
|
|
515
515
|
const isDerivedDefault = defaultValue && (
|
|
516
|
-
defaultValue === key || // Exact match
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
516
|
+
defaultValue === key || // Exact match with the key itself
|
|
517
|
+
// Check if defaultValue matches the namespaced key format (namespace:key)
|
|
518
|
+
(nsSep && namespace && defaultValue === `${namespace}${nsSep}${key}`) ||
|
|
519
|
+
// For variant keys (plural/context), check if defaultValue is the base
|
|
520
|
+
(key !== defaultValue &&
|
|
521
|
+
(key.startsWith(defaultValue + pluralSeparator) ||
|
|
522
|
+
key.startsWith(defaultValue + contextSeparator)))
|
|
523
523
|
)
|
|
524
524
|
|
|
525
525
|
// If this key looks like a plural/context variant and the default
|
|
@@ -602,17 +602,50 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
602
602
|
if (isFormattingWhitespace(node)) continue
|
|
603
603
|
|
|
604
604
|
const nextNode = nodes[i + 1]
|
|
605
|
+
const prevNode = nodes[i - 1]
|
|
606
|
+
|
|
607
|
+
// If this text follows a preserved tag and starts with newline+whitespace, trim it
|
|
608
|
+
if (prevNode && prevNode.type === 'JSXElement') {
|
|
609
|
+
const prevTag = prevNode.opening?.name?.type === 'Identifier' ? prevNode.opening.name.value : undefined
|
|
610
|
+
const prevIsPreservedTag = prevTag && allowedTags.has(prevTag)
|
|
611
|
+
|
|
612
|
+
// Only trim leading whitespace after SELF-CLOSING preserved tags (like <br />)
|
|
613
|
+
// Block tags like <p> or inline tags like <strong> need surrounding spaces
|
|
614
|
+
const prevChildren = prevNode.children || []
|
|
615
|
+
const prevIsSelfClosing = prevChildren.length === 0
|
|
616
|
+
|
|
617
|
+
if (prevIsPreservedTag && prevIsSelfClosing && /^\s*\n\s*/.test(node.value)) {
|
|
618
|
+
// Text starts with newline after a self-closing preserved tag - trim leading formatting
|
|
619
|
+
const trimmedValue = node.value.replace(/^\s*\n\s*/, '')
|
|
620
|
+
if (trimmedValue) {
|
|
621
|
+
out += trimmedValue
|
|
622
|
+
continue
|
|
623
|
+
}
|
|
624
|
+
// If nothing left after trimming, skip this node
|
|
625
|
+
continue
|
|
626
|
+
}
|
|
627
|
+
}
|
|
605
628
|
|
|
606
629
|
// If this text node ends with newline+whitespace and is followed by an element,
|
|
607
630
|
if (/\n\s*$/.test(node.value) && nextNode && nextNode.type === 'JSXElement') {
|
|
608
631
|
const textWithoutTrailingNewline = node.value.replace(/\n\s*$/, '')
|
|
609
632
|
if (textWithoutTrailingNewline.trim()) {
|
|
610
|
-
// Check if
|
|
633
|
+
// Check if the next element is a preserved tag
|
|
634
|
+
const nextTag = nextNode.opening?.name?.type === 'Identifier' ? nextNode.opening.name.value : undefined
|
|
635
|
+
const isPreservedTag = nextTag && allowedTags.has(nextTag)
|
|
636
|
+
|
|
637
|
+
// Check if the preserved tag has children (not self-closing)
|
|
638
|
+
const nextChildren = nextNode.children || []
|
|
639
|
+
const nextHasChildren = nextChildren.length > 0
|
|
640
|
+
|
|
641
|
+
// Check if there was a space BEFORE the newline in the source
|
|
642
|
+
const hasSpaceBeforeNewline = /\s\n/.test(node.value)
|
|
643
|
+
|
|
644
|
+
// Check if there's text content AFTER the next element
|
|
611
645
|
const nodeAfterNext = nodes[i + 2]
|
|
612
646
|
const hasTextAfter = nodeAfterNext &&
|
|
613
647
|
nodeAfterNext.type === 'JSXText' &&
|
|
614
648
|
!isFormattingWhitespace(nodeAfterNext) &&
|
|
615
|
-
// Check if it's not just punctuation (period, comma, etc.)
|
|
616
649
|
/[a-zA-Z0-9]/.test(nodeAfterNext.value)
|
|
617
650
|
|
|
618
651
|
// Preserve leading whitespace
|
|
@@ -621,10 +654,10 @@ function serializeJSXChildren (children: any[], config: I18nextToolkitConfig): s
|
|
|
621
654
|
const withLeading = hasLeadingSpace ? ' ' + trimmed : trimmed
|
|
622
655
|
|
|
623
656
|
// Add trailing space only if:
|
|
624
|
-
// 1. There was
|
|
625
|
-
// 2.
|
|
626
|
-
|
|
627
|
-
if (hasSpaceBeforeNewline || hasTextAfter) {
|
|
657
|
+
// 1. There was an explicit space before the newline, OR
|
|
658
|
+
// 2. The next element is NOT a preserved tag AND has text after (word boundary)
|
|
659
|
+
// Preserved tags like <br />, <p>, etc. provide their own separation
|
|
660
|
+
if (hasSpaceBeforeNewline || (isPreservedTag && nextHasChildren) || (!isPreservedTag && hasTextAfter)) {
|
|
628
661
|
out += withLeading + ' '
|
|
629
662
|
} else {
|
|
630
663
|
out += withLeading
|