Skip to content

Commit

Permalink
feat(cli): support multiple locales in extract command (#1858)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-dr authored and andrii-bodnar committed Mar 18, 2024
1 parent 1d29a95 commit 10e6119
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe("Catalog", () => {
// Everything should be empty
expect(await catalog.readAll()).toMatchSnapshot()

await catalog.make({ ...defaultMakeOptions, locale: "en" })
await catalog.make({ ...defaultMakeOptions, locale: ["en"] })
expect(await catalog.readAll()).toMatchSnapshot()
})

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/api/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class Catalog {

const sortedCatalogs = cleanAndSort(catalogs)

const locales = options.locale ? [options.locale] : this.locales
const locales = options.locale ? options.locale : this.locales
await Promise.all(
locales.map((locale) => this.write(locale, sortedCatalogs[locale]))
)
Expand Down
29 changes: 22 additions & 7 deletions packages/cli/src/lingui-extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type CliExtractOptions = {
files?: string[]
clean: boolean
overwrite: boolean
locale: string
locale: string[]
prevFormat: string | null
watch?: boolean
}
Expand Down Expand Up @@ -103,15 +103,24 @@ type CliOptions = {
files?: string[]
clean: boolean
overwrite: boolean
locale: string
locale: string[]
prevFormat: string | null
watch?: boolean
}

if (require.main === module) {
program
.option("--config <path>", "Path to the config file")
.option("--locale <locale>", "Only extract the specified locale")
.option(
"--locale <locale, [...]>",
"Only extract the specified locales",
(value) => {
return value
.split(",")
.map((s) => s.trim())
.filter(Boolean)
}
)
.option("--overwrite", "Overwrite translations for source locale")
.option("--clean", "Remove obsolete translations")
.option(
Expand Down Expand Up @@ -148,10 +157,16 @@ if (require.main === module) {
process.exit(1)
}

if (options.locale && !config.locales.includes(options.locale)) {
hasErrors = true
console.error(`Locale ${chalk.bold(options.locale)} does not exist.`)
console.error()
if (options.locale) {
const missingLocale = options.locale.find(
(l) => !config.locales.includes(l)
)

if (missingLocale) {
hasErrors = true
console.error(`Locale ${chalk.bold(missingLocale)} does not exist.`)
console.error()
}
}

if (hasErrors) process.exit(1)
Expand Down
6 changes: 3 additions & 3 deletions website/docs/ref/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ lingui extract [files...]
[--clean]
[--overwrite]
[--format <format>]
[--locale <locale>]
[--locale <locale, [...]>]
[--convert-from <format>]
[--verbose]
[--watch [--debounce <delay>]]
Expand Down Expand Up @@ -109,9 +109,9 @@ Update translations for [`sourceLocale`](/docs/ref/conf.md#sourcelocale) from so
Extract message catalogs to the specified file format (see the [`format`](/docs/ref/conf.md#format) option for more details).
#### `--locale <locale>` {#extract-locale}
#### `--locale <locale, [...]>` {#extract-locale}
Extract data for the specified locale only.
Extract data for the specified locales only.
#### `--convert-from <format>` {#extract-convert-from}
Expand Down

0 comments on commit 10e6119

Please sign in to comment.