i18next-cli 1.5.1 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.5.2](https://github.com/i18next/i18next-cli/compare/v1.5.1...v1.5.2) - 2025-10-02
9
+
10
+ - **Extractor:** Fixed a regression where keys within nested objects were no longer being sorted alphabetically (e.g., `buttons.scroll-to-top` would appear before `buttons.cancel`). The sorting logic now recursively sorts keys at all levels of the translation object to ensure a consistent and predictable order.
11
+
8
12
  ## [1.5.1](https://github.com/i18next/i18next-cli/compare/v1.5.0...v1.5.1) - 2025-10-02
9
13
 
10
14
  - **Extractor:** Improved the default key extraction by updating the default `functions` option to `['t', '*.t']`. This allows the extractor to automatically find keys in common patterns like `i18n.t(...)` and `this.t(...)` without any configuration.
package/dist/cjs/cli.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var e=require("commander"),t=require("chokidar"),n=require("glob"),o=require("chalk"),i=require("./config.js"),a=require("./heuristic-config.js"),r=require("./extractor/core/extractor.js");require("node:path"),require("node:fs/promises"),require("jiti");var c=require("./types-generator.js"),s=require("./syncer.js"),l=require("./migrator.js"),u=require("./init.js"),d=require("./linter.js"),g=require("./status.js"),p=require("./locize.js");const f=new e.Command;f.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.5.1"),f.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").action(async e=>{const a=await i.ensureConfig(),c=async()=>{const t=await r.runExtractor(a,{isWatchMode:e.watch,isDryRun:e.dryRun});e.ci&&t&&(console.error(o.red.bold("\n[CI Mode] Error: Translation files were updated. Please commit the changes.")),console.log(o.yellow("💡 Tip: Tired of committing JSON files? locize syncs your team automatically => https://www.locize.com/docs/getting-started")),console.log(` Learn more: ${o.cyan("npx i18next-cli locize-sync")}`),process.exit(1))};if(await c(),e.watch){console.log("\nWatching for changes...");t.watch(await n.glob(a.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),c()})}}),f.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(e,t)=>{let n=await i.loadConfig();if(!n){console.log(o.blue("No config file found. Attempting to detect project structure..."));const e=await a.detectConfig();e||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),n=e}await g.runStatus(n,{detail:e,namespace:t.namespace})}),f.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async e=>{const o=await i.ensureConfig(),a=()=>c.runTypesGenerator(o);if(await a(),e.watch){console.log("\nWatching for changes...");t.watch(await n.glob(o.types?.input||[]),{persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}),f.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=await i.ensureConfig();await s.runSyncer(e)}),f.command("migrate-config").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async()=>{await l.runMigrator()}),f.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(u.runInit),f.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async e=>{const r=async()=>{let e=await i.loadConfig();if(!e){console.log(o.blue("No config file found. Attempting to detect project structure..."));const t=await a.detectConfig();t||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),e=t}await d.runLinter(e)};if(await r(),e.watch){console.log("\nWatching for changes...");const e=await i.loadConfig();if(e?.extract?.input){t.watch(await n.glob(e.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),r()})}}}),f.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeSync(t,e)}),f.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeDownload(t,e)}),f.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeMigrate(t,e)}),f.parse(process.argv);
2
+ "use strict";var e=require("commander"),t=require("chokidar"),n=require("glob"),o=require("chalk"),i=require("./config.js"),a=require("./heuristic-config.js"),r=require("./extractor/core/extractor.js");require("node:path"),require("node:fs/promises"),require("jiti");var c=require("./types-generator.js"),s=require("./syncer.js"),l=require("./migrator.js"),u=require("./init.js"),d=require("./linter.js"),g=require("./status.js"),p=require("./locize.js");const f=new e.Command;f.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.5.2"),f.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").action(async e=>{const a=await i.ensureConfig(),c=async()=>{const t=await r.runExtractor(a,{isWatchMode:e.watch,isDryRun:e.dryRun});e.ci&&t&&(console.error(o.red.bold("\n[CI Mode] Error: Translation files were updated. Please commit the changes.")),console.log(o.yellow("💡 Tip: Tired of committing JSON files? locize syncs your team automatically => https://www.locize.com/docs/getting-started")),console.log(` Learn more: ${o.cyan("npx i18next-cli locize-sync")}`),process.exit(1))};if(await c(),e.watch){console.log("\nWatching for changes...");t.watch(await n.glob(a.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),c()})}}),f.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(e,t)=>{let n=await i.loadConfig();if(!n){console.log(o.blue("No config file found. Attempting to detect project structure..."));const e=await a.detectConfig();e||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),n=e}await g.runStatus(n,{detail:e,namespace:t.namespace})}),f.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async e=>{const o=await i.ensureConfig(),a=()=>c.runTypesGenerator(o);if(await a(),e.watch){console.log("\nWatching for changes...");t.watch(await n.glob(o.types?.input||[]),{persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),a()})}}),f.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const e=await i.ensureConfig();await s.runSyncer(e)}),f.command("migrate-config").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async()=>{await l.runMigrator()}),f.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(u.runInit),f.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async e=>{const r=async()=>{let e=await i.loadConfig();if(!e){console.log(o.blue("No config file found. Attempting to detect project structure..."));const t=await a.detectConfig();t||(console.error(o.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${o.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(o.green("Project structure detected successfully!")),e=t}await d.runLinter(e)};if(await r(),e.watch){console.log("\nWatching for changes...");const e=await i.loadConfig();if(e?.extract?.input){t.watch(await n.glob(e.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",e=>{console.log(`\nFile changed: ${e}`),r()})}}}),f.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeSync(t,e)}),f.command("locize-download").description("Download all translations from your locize project.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeDownload(t,e)}),f.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async e=>{const t=await i.ensureConfig();await p.runLocizeMigrate(t,e)}),f.parse(process.argv);
@@ -1 +1 @@
1
- "use strict";var t=require("node:path"),e=require("glob"),s=require("../../utils/nested-object.js"),a=require("../../utils/file-utils.js");function n(t){const e=`^${t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*")}$`;return new RegExp(e)}function o(t,e,a,n,o,r){const{keySeparator:c=".",sort:i=!0,removeUnusedKeys:u=!0,primaryLanguage:l,defaultValue:f=""}=a.extract;let g=u?{}:JSON.parse(JSON.stringify(e));const p=s.getNestedKeys(e,c??".");for(const t of p)if(o.some(e=>e.test(t))){const a=s.getNestedValue(e,t,c??".");s.setNestedValue(g,t,a,c??".")}for(const{key:a,defaultValue:o}of t){const i=s.getNestedValue(e,a,c??"."),u=!t.some(t=>t.key.startsWith(`${a}${c}`)&&t.key!==a),p="object"==typeof i&&null!==i&&u&&!r.has(a),y=void 0===i||p?n===l?o:f:i;s.setNestedValue(g,a,y,c??".")}if(!1!==i){const e={},s=Object.keys(g),a=new Map;for(const e of t){const t=!1===c?e.key:e.key.split(c)[0];a.has(t)||a.set(t,e)}s.sort((t,e)=>{if("function"==typeof i){const s=a.get(t),n=a.get(e);if(s&&n)return i(s,n)}return t.localeCompare(e)});for(const t of s)e[t]=g[t];g=e}return g}exports.getTranslations=async function(s,r,c){c.extract.primaryLanguage||=c.locales[0]||"en",c.extract.secondaryLanguages||=c.locales.filter(t=>t!==c?.extract?.primaryLanguage);const i=c.extract.defaultNS??"translation",u=[...c.extract.preservePatterns||[]],l=c.extract.indentation??2;for(const t of r)u.push(`${t}.*`);const f=u.map(n),g=new Map;for(const t of s.values()){const e=t.ns||i;g.has(e)||g.set(e,[]),g.get(e).push(t)}const p=[],y=Array.isArray(c.extract.ignore)?c.extract.ignore:c.extract.ignore?[c.extract.ignore]:[];for(const s of c.locales){if(c.extract.mergeNamespaces||!c.extract.output.includes("{{namespace}}")){const e={},n=a.getOutputPath(c.extract.output,s),i=t.resolve(process.cwd(),n),u=await a.loadTranslationFile(i)||{},y=new Set([...g.keys(),...Object.keys(u)]);for(const t of y){const a=g.get(t)||[],n=u[t]||{};e[t]=o(a,n,c,s,f,r)}const d=JSON.stringify(u,null,l),x=JSON.stringify(e,null,l);p.push({path:i,updated:x!==d,newTranslations:e,existingTranslations:u})}else{const n=new Set(g.keys()),i=a.getOutputPath(c.extract.output,s,"*"),u=await e.glob(i,{ignore:y});for(const e of u)n.add(t.basename(e,t.extname(e)));for(const e of n){const n=g.get(e)||[],i=a.getOutputPath(c.extract.output,s,e),u=t.resolve(process.cwd(),i),y=await a.loadTranslationFile(u)||{},d=o(n,y,c,s,f,r),x=JSON.stringify(y,null,l),h=JSON.stringify(d,null,l);p.push({path:u,updated:h!==x,newTranslations:d,existingTranslations:y})}}}return p};
1
+ "use strict";var t=require("node:path"),e=require("glob"),s=require("../../utils/nested-object.js"),n=require("../../utils/file-utils.js");function r(t){const e=`^${t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*")}$`;return new RegExp(e)}function o(t){if("object"!=typeof t||null===t||Array.isArray(t))return t;const e={},s=Object.keys(t).sort();for(const n of s)e[n]=o(t[n]);return e}function a(t,e,n,r,a,c){const{keySeparator:i=".",sort:u=!0,removeUnusedKeys:l=!0,primaryLanguage:f,defaultValue:p=""}=n.extract;let g=l?{}:JSON.parse(JSON.stringify(e));const y=s.getNestedKeys(e,i??".");for(const t of y)if(a.some(e=>e.test(t))){const n=s.getNestedValue(e,t,i??".");s.setNestedValue(g,t,n,i??".")}for(const{key:n,defaultValue:o}of t){const a=s.getNestedValue(e,n,i??"."),u=!t.some(t=>t.key.startsWith(`${n}${i}`)&&t.key!==n),l="object"==typeof a&&null!==a&&u&&!c.has(n),y=void 0===a||l?r===f?o:p:a;s.setNestedValue(g,n,y,i??".")}if(!0===u)return o(g);if("function"==typeof u){const e={},s=Object.keys(g),n=new Map;for(const e of t){const t=!1===i?e.key:e.key.split(i)[0];n.has(t)||n.set(t,e)}s.sort((t,e)=>{if("function"==typeof u){const s=n.get(t),r=n.get(e);if(s&&r)return u(s,r)}return t.localeCompare(e)});for(const t of s)e[t]=g[t];g=e}return g}exports.getTranslations=async function(s,o,c){c.extract.primaryLanguage||=c.locales[0]||"en",c.extract.secondaryLanguages||=c.locales.filter(t=>t!==c?.extract?.primaryLanguage);const i=c.extract.defaultNS??"translation",u=[...c.extract.preservePatterns||[]],l=c.extract.indentation??2;for(const t of o)u.push(`${t}.*`);const f=u.map(r),p=new Map;for(const t of s.values()){const e=t.ns||i;p.has(e)||p.set(e,[]),p.get(e).push(t)}const g=[],y=Array.isArray(c.extract.ignore)?c.extract.ignore:c.extract.ignore?[c.extract.ignore]:[];for(const s of c.locales){if(c.extract.mergeNamespaces||!c.extract.output.includes("{{namespace}}")){const e={},r=n.getOutputPath(c.extract.output,s),i=t.resolve(process.cwd(),r),u=await n.loadTranslationFile(i)||{},y=new Set([...p.keys(),...Object.keys(u)]);for(const t of y){const n=p.get(t)||[],r=u[t]||{};e[t]=a(n,r,c,s,f,o)}const d=JSON.stringify(u,null,l),x=JSON.stringify(e,null,l);g.push({path:i,updated:x!==d,newTranslations:e,existingTranslations:u})}else{const r=new Set(p.keys()),i=n.getOutputPath(c.extract.output,s,"*"),u=await e.glob(i,{ignore:y});for(const e of u)r.add(t.basename(e,t.extname(e)));for(const e of r){const r=p.get(e)||[],i=n.getOutputPath(c.extract.output,s,e),u=t.resolve(process.cwd(),i),y=await n.loadTranslationFile(u)||{},d=a(r,y,c,s,f,o),x=JSON.stringify(y,null,l),h=JSON.stringify(d,null,l);g.push({path:u,updated:h!==x,newTranslations:d,existingTranslations:y})}}}return g};
package/dist/esm/cli.js CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/usr/bin/env node
2
- import{Command as t}from"commander";import o from"chokidar";import{glob as e}from"glob";import n from"chalk";import{ensureConfig as i,loadConfig as a}from"./config.js";import{detectConfig as c}from"./heuristic-config.js";import{runExtractor as r}from"./extractor/core/extractor.js";import"node:path";import"node:fs/promises";import"jiti";import{runTypesGenerator as s}from"./types-generator.js";import{runSyncer as l}from"./syncer.js";import{runMigrator as m}from"./migrator.js";import{runInit as p}from"./init.js";import{runLinter as d}from"./linter.js";import{runStatus as g}from"./status.js";import{runLocizeSync as f,runLocizeDownload as u,runLocizeMigrate as h}from"./locize.js";const w=new t;w.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.5.1"),w.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").action(async t=>{const a=await i(),c=async()=>{const o=await r(a,{isWatchMode:t.watch,isDryRun:t.dryRun});t.ci&&o&&(console.error(n.red.bold("\n[CI Mode] Error: Translation files were updated. Please commit the changes.")),console.log(n.yellow("💡 Tip: Tired of committing JSON files? locize syncs your team automatically => https://www.locize.com/docs/getting-started")),console.log(` Learn more: ${n.cyan("npx i18next-cli locize-sync")}`),process.exit(1))};if(await c(),t.watch){console.log("\nWatching for changes...");o.watch(await e(a.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),c()})}}),w.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(t,o)=>{let e=await a();if(!e){console.log(n.blue("No config file found. Attempting to detect project structure..."));const t=await c();t||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),e=t}await g(e,{detail:t,namespace:o.namespace})}),w.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async t=>{const n=await i(),a=()=>s(n);if(await a(),t.watch){console.log("\nWatching for changes...");o.watch(await e(n.types?.input||[]),{persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),a()})}}),w.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const t=await i();await l(t)}),w.command("migrate-config").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async()=>{await m()}),w.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(p),w.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async t=>{const i=async()=>{let t=await a();if(!t){console.log(n.blue("No config file found. Attempting to detect project structure..."));const o=await c();o||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),t=o}await d(t)};if(await i(),t.watch){console.log("\nWatching for changes...");const t=await a();if(t?.extract?.input){o.watch(await e(t.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),i()})}}}),w.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async t=>{const o=await i();await f(o,t)}),w.command("locize-download").description("Download all translations from your locize project.").action(async t=>{const o=await i();await u(o,t)}),w.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async t=>{const o=await i();await h(o,t)}),w.parse(process.argv);
2
+ import{Command as t}from"commander";import o from"chokidar";import{glob as e}from"glob";import n from"chalk";import{ensureConfig as i,loadConfig as a}from"./config.js";import{detectConfig as c}from"./heuristic-config.js";import{runExtractor as r}from"./extractor/core/extractor.js";import"node:path";import"node:fs/promises";import"jiti";import{runTypesGenerator as s}from"./types-generator.js";import{runSyncer as l}from"./syncer.js";import{runMigrator as m}from"./migrator.js";import{runInit as p}from"./init.js";import{runLinter as d}from"./linter.js";import{runStatus as g}from"./status.js";import{runLocizeSync as f,runLocizeDownload as u,runLocizeMigrate as h}from"./locize.js";const w=new t;w.name("i18next-cli").description("A unified, high-performance i18next CLI.").version("1.5.2"),w.command("extract").description("Extract translation keys from source files and update resource files.").option("-w, --watch","Watch for file changes and re-run the extractor.").option("--ci","Exit with a non-zero status code if any files are updated.").option("--dry-run","Run the extractor without writing any files to disk.").action(async t=>{const a=await i(),c=async()=>{const o=await r(a,{isWatchMode:t.watch,isDryRun:t.dryRun});t.ci&&o&&(console.error(n.red.bold("\n[CI Mode] Error: Translation files were updated. Please commit the changes.")),console.log(n.yellow("💡 Tip: Tired of committing JSON files? locize syncs your team automatically => https://www.locize.com/docs/getting-started")),console.log(` Learn more: ${n.cyan("npx i18next-cli locize-sync")}`),process.exit(1))};if(await c(),t.watch){console.log("\nWatching for changes...");o.watch(await e(a.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),c()})}}),w.command("status [locale]").description("Display translation status. Provide a locale for a detailed key-by-key view.").option("-n, --namespace <ns>","Filter the status report by a specific namespace").action(async(t,o)=>{let e=await a();if(!e){console.log(n.blue("No config file found. Attempting to detect project structure..."));const t=await c();t||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),e=t}await g(e,{detail:t,namespace:o.namespace})}),w.command("types").description("Generate TypeScript definitions from translation resource files.").option("-w, --watch","Watch for file changes and re-run the type generator.").action(async t=>{const n=await i(),a=()=>s(n);if(await a(),t.watch){console.log("\nWatching for changes...");o.watch(await e(n.types?.input||[]),{persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),a()})}}),w.command("sync").description("Synchronize secondary language files with the primary language file.").action(async()=>{const t=await i();await l(t)}),w.command("migrate-config").description("Migrate a legacy i18next-parser.config.js to the new format.").action(async()=>{await m()}),w.command("init").description("Create a new i18next.config.ts/js file with an interactive setup wizard.").action(p),w.command("lint").description("Find potential issues like hardcoded strings in your codebase.").option("-w, --watch","Watch for file changes and re-run the linter.").action(async t=>{const i=async()=>{let t=await a();if(!t){console.log(n.blue("No config file found. Attempting to detect project structure..."));const o=await c();o||(console.error(n.red("Could not automatically detect your project structure.")),console.log(`Please create a config file first by running: ${n.cyan("npx i18next-cli init")}`),process.exit(1)),console.log(n.green("Project structure detected successfully!")),t=o}await d(t)};if(await i(),t.watch){console.log("\nWatching for changes...");const t=await a();if(t?.extract?.input){o.watch(await e(t.extract.input),{ignored:/node_modules/,persistent:!0}).on("change",t=>{console.log(`\nFile changed: ${t}`),i()})}}}),w.command("locize-sync").description("Synchronize local translations with your locize project.").option("--update-values","Update values of existing translations on locize.").option("--src-lng-only","Check for changes in source language only.").option("--compare-mtime","Compare modification times when syncing.").option("--dry-run","Run the command without making any changes.").action(async t=>{const o=await i();await f(o,t)}),w.command("locize-download").description("Download all translations from your locize project.").action(async t=>{const o=await i();await u(o,t)}),w.command("locize-migrate").description("Migrate local translation files to a new locize project.").action(async t=>{const o=await i();await h(o,t)}),w.parse(process.argv);
@@ -1 +1 @@
1
- import{resolve as t,basename as e,extname as o}from"node:path";import{glob as s}from"glob";import{getNestedKeys as n,getNestedValue as r,setNestedValue as a}from"../../utils/nested-object.js";import{getOutputPath as c,loadTranslationFile as i}from"../../utils/file-utils.js";function f(t){const e=`^${t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*")}$`;return new RegExp(e)}function u(t,e,o,s,c,i){const{keySeparator:f=".",sort:u=!0,removeUnusedKeys:l=!0,primaryLanguage:p,defaultValue:g=""}=o.extract;let y=l?{}:JSON.parse(JSON.stringify(e));const x=n(e,f??".");for(const t of x)if(c.some(e=>e.test(t))){const o=r(e,t,f??".");a(y,t,o,f??".")}for(const{key:o,defaultValue:n}of t){const c=r(e,o,f??"."),u=!t.some(t=>t.key.startsWith(`${o}${f}`)&&t.key!==o),l="object"==typeof c&&null!==c&&u&&!i.has(o);a(y,o,void 0===c||l?s===p?n:g:c,f??".")}if(!1!==u){const e={},o=Object.keys(y),s=new Map;for(const e of t){const t=!1===f?e.key:e.key.split(f)[0];s.has(t)||s.set(t,e)}o.sort((t,e)=>{if("function"==typeof u){const o=s.get(t),n=s.get(e);if(o&&n)return u(o,n)}return t.localeCompare(e)});for(const t of o)e[t]=y[t];y=e}return y}async function l(n,r,a){a.extract.primaryLanguage||=a.locales[0]||"en",a.extract.secondaryLanguages||=a.locales.filter(t=>t!==a?.extract?.primaryLanguage);const l=a.extract.defaultNS??"translation",p=[...a.extract.preservePatterns||[]],g=a.extract.indentation??2;for(const t of r)p.push(`${t}.*`);const y=p.map(f),x=new Map;for(const t of n.values()){const e=t.ns||l;x.has(e)||x.set(e,[]),x.get(e).push(t)}const m=[],d=Array.isArray(a.extract.ignore)?a.extract.ignore:a.extract.ignore?[a.extract.ignore]:[];for(const n of a.locales){if(a.extract.mergeNamespaces||!a.extract.output.includes("{{namespace}}")){const e={},o=c(a.extract.output,n),s=t(process.cwd(),o),f=await i(s)||{},l=new Set([...x.keys(),...Object.keys(f)]);for(const t of l){const o=x.get(t)||[],s=f[t]||{};e[t]=u(o,s,a,n,y,r)}const p=JSON.stringify(f,null,g),d=JSON.stringify(e,null,g);m.push({path:s,updated:d!==p,newTranslations:e,existingTranslations:f})}else{const f=new Set(x.keys()),l=c(a.extract.output,n,"*"),p=await s(l,{ignore:d});for(const t of p)f.add(e(t,o(t)));for(const e of f){const o=x.get(e)||[],s=c(a.extract.output,n,e),f=t(process.cwd(),s),l=await i(f)||{},p=u(o,l,a,n,y,r),d=JSON.stringify(l,null,g),w=JSON.stringify(p,null,g);m.push({path:f,updated:w!==d,newTranslations:p,existingTranslations:l})}}}return m}export{l as getTranslations};
1
+ import{resolve as t,basename as e,extname as o}from"node:path";import{glob as n}from"glob";import{getNestedKeys as r,getNestedValue as s,setNestedValue as a}from"../../utils/nested-object.js";import{getOutputPath as c,loadTranslationFile as i}from"../../utils/file-utils.js";function f(t){const e=`^${t.replace(/[.+?^${}()|[\]\\]/g,"\\$&").replace(/\*/g,".*")}$`;return new RegExp(e)}function u(t){if("object"!=typeof t||null===t||Array.isArray(t))return t;const e={},o=Object.keys(t).sort();for(const n of o)e[n]=u(t[n]);return e}function l(t,e,o,n,c,i){const{keySeparator:f=".",sort:l=!0,removeUnusedKeys:p=!0,primaryLanguage:g,defaultValue:y=""}=o.extract;let x=p?{}:JSON.parse(JSON.stringify(e));const m=r(e,f??".");for(const t of m)if(c.some(e=>e.test(t))){const o=s(e,t,f??".");a(x,t,o,f??".")}for(const{key:o,defaultValue:r}of t){const c=s(e,o,f??"."),u=!t.some(t=>t.key.startsWith(`${o}${f}`)&&t.key!==o),l="object"==typeof c&&null!==c&&u&&!i.has(o);a(x,o,void 0===c||l?n===g?r:y:c,f??".")}if(!0===l)return u(x);if("function"==typeof l){const e={},o=Object.keys(x),n=new Map;for(const e of t){const t=!1===f?e.key:e.key.split(f)[0];n.has(t)||n.set(t,e)}o.sort((t,e)=>{if("function"==typeof l){const o=n.get(t),r=n.get(e);if(o&&r)return l(o,r)}return t.localeCompare(e)});for(const t of o)e[t]=x[t];x=e}return x}async function p(r,s,a){a.extract.primaryLanguage||=a.locales[0]||"en",a.extract.secondaryLanguages||=a.locales.filter(t=>t!==a?.extract?.primaryLanguage);const u=a.extract.defaultNS??"translation",p=[...a.extract.preservePatterns||[]],g=a.extract.indentation??2;for(const t of s)p.push(`${t}.*`);const y=p.map(f),x=new Map;for(const t of r.values()){const e=t.ns||u;x.has(e)||x.set(e,[]),x.get(e).push(t)}const m=[],d=Array.isArray(a.extract.ignore)?a.extract.ignore:a.extract.ignore?[a.extract.ignore]:[];for(const r of a.locales){if(a.extract.mergeNamespaces||!a.extract.output.includes("{{namespace}}")){const e={},o=c(a.extract.output,r),n=t(process.cwd(),o),f=await i(n)||{},u=new Set([...x.keys(),...Object.keys(f)]);for(const t of u){const o=x.get(t)||[],n=f[t]||{};e[t]=l(o,n,a,r,y,s)}const p=JSON.stringify(f,null,g),d=JSON.stringify(e,null,g);m.push({path:n,updated:d!==p,newTranslations:e,existingTranslations:f})}else{const f=new Set(x.keys()),u=c(a.extract.output,r,"*"),p=await n(u,{ignore:d});for(const t of p)f.add(e(t,o(t)));for(const e of f){const o=x.get(e)||[],n=c(a.extract.output,r,e),f=t(process.cwd(),n),u=await i(f)||{},p=l(o,u,a,r,y,s),d=JSON.stringify(u,null,g),w=JSON.stringify(p,null,g);m.push({path:f,updated:w!==d,newTranslations:p,existingTranslations:u})}}}return m}export{p as getTranslations};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "i18next-cli",
3
- "version": "1.5.1",
3
+ "version": "1.5.2",
4
4
  "description": "A unified, high-performance i18next CLI.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli.ts CHANGED
@@ -21,7 +21,7 @@ const program = new Command()
21
21
  program
22
22
  .name('i18next-cli')
23
23
  .description('A unified, high-performance i18next CLI.')
24
- .version('1.5.1')
24
+ .version('1.5.2')
25
25
 
26
26
  program
27
27
  .command('extract')
@@ -15,6 +15,24 @@ function globToRegex (glob: string): RegExp {
15
15
  return new RegExp(regexString)
16
16
  }
17
17
 
18
+ /**
19
+ * Recursively sorts the keys of an object.
20
+ */
21
+ function sortObject (obj: any): any {
22
+ if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
23
+ return obj
24
+ }
25
+
26
+ const sortedObj: Record<string, any> = {}
27
+ const keys = Object.keys(obj).sort()
28
+
29
+ for (const key of keys) {
30
+ sortedObj[key] = sortObject(obj[key])
31
+ }
32
+
33
+ return sortedObj
34
+ }
35
+
18
36
  /**
19
37
  * A helper function to build a new translation object for a single namespace.
20
38
  * This centralizes the core logic of merging keys.
@@ -62,8 +80,14 @@ function buildNewTranslationsForNs (
62
80
  setNestedValue(newTranslations, key, valueToSet, keySeparator ?? '.')
63
81
  }
64
82
 
65
- // 2. If sorting is enabled, create a new sorted object from the one we just built.
66
- if (sort !== false) {
83
+ // 2. If sorting is enabled, recursively sort the entire object.
84
+ // This correctly handles both top-level and nested keys.
85
+ if (sort === true) {
86
+ return sortObject(newTranslations)
87
+ }
88
+ // Custom sort function logic remains as a future enhancement if needed,
89
+ // but for now, this robustly handles the most common `sort: true` case.
90
+ if (typeof sort === 'function') {
67
91
  const sortedObject: Record<string, any> = {}
68
92
  const topLevelKeys = Object.keys(newTranslations)
69
93
 
@@ -1 +1 @@
1
- {"version":3,"file":"translation-manager.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/translation-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AAsGnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,EAC/B,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,EACvB,MAAM,EAAE,oBAAoB,GAC3B,OAAO,CAAC,iBAAiB,EAAE,CAAC,CA8E9B"}
1
+ {"version":3,"file":"translation-manager.d.ts","sourceRoot":"","sources":["../../../src/extractor/core/translation-manager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAA;AA8HnF;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,wBAAsB,eAAe,CACnC,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,EAC/B,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,EACvB,MAAM,EAAE,oBAAoB,GAC3B,OAAO,CAAC,iBAAiB,EAAE,CAAC,CA8E9B"}