Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support multiple locales in extract command #1858

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
26 changes: 19 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 @@
files?: string[]
clean: boolean
overwrite: boolean
locale: string
locale: string[]
prevFormat: string | null
watch?: boolean
}
Expand Down Expand Up @@ -103,15 +103,21 @@
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(",").filter(Boolean)

Check warning on line 118 in packages/cli/src/lingui-extract.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/lingui-extract.ts#L118

Added line #L118 was not covered by tests
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
}
)
.option("--overwrite", "Overwrite translations for source locale")
.option("--clean", "Remove obsolete translations")
.option(
Expand Down Expand Up @@ -148,10 +154,16 @@
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)

Check warning on line 159 in packages/cli/src/lingui-extract.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/lingui-extract.ts#L158-L159

Added lines #L158 - L159 were not covered by tests
)

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

Check warning on line 165 in packages/cli/src/lingui-extract.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/lingui-extract.ts#L163-L165

Added lines #L163 - L165 were not covered by tests
}
}

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 @@ -49,7 +49,7 @@ lingui extract [files...]
[--clean]
[--overwrite]
[--format <format>]
[--locale <locale>]
[--locale <locale, [...]>]
[--convert-from <format>]
[--verbose]
[--watch [--debounce <delay>]]
Expand Down Expand Up @@ -95,9 +95,9 @@ Update translations for [`sourceLocale`](/docs/ref/conf.md#sourcelocale) from so

Format of message catalogs (see [`format`](/docs/ref/conf.md#format) option).

#### `--locale <locale>` {#extract-locale}
#### `--locale <locale, [...]>` {#extract-locale}

Only extract data for the specified locale.
Only extract data for the specified locales.

#### `--convert-from <format>` {#extract-convert-from}

Expand Down