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

refactor(cli): extend formatters api with source lang #1542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("Catalog", () => {
let format: FormatterWrapper

beforeAll(async () => {
format = await getFormat("po", {})
format = await getFormat("po", {}, "en")
})

afterEach(() => {
Expand Down Expand Up @@ -595,7 +595,7 @@ describe("writeCompiled", () => {
path: path.join(localeDir, "{locale}", "messages"),
include: [],
exclude: [],
format: await getFormat("po", {}),
format: await getFormat("po", {}, "en"),
},
mockConfig()
)
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/api/catalog/getCatalogs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("getCatalogs", () => {
let format: FormatterWrapper

beforeAll(async () => {
format = await getFormat("po", {})
format = await getFormat("po", {}, "en")
})

afterEach(() => {
Expand Down Expand Up @@ -254,7 +254,7 @@ describe("getCatalogForMerge", () => {
let format: FormatterWrapper

beforeAll(async () => {
format = await getFormat("po", {})
format = await getFormat("po", {}, "en")
})

afterEach(() => {
Expand Down Expand Up @@ -333,7 +333,7 @@ describe("getCatalogForFile", () => {
let format: FormatterWrapper

beforeAll(async () => {
format = await getFormat("po", {})
format = await getFormat("po", {}, "en")
})

it("should return null if catalog cannot be found", () => {
Expand Down
12 changes: 10 additions & 2 deletions packages/cli/src/api/catalog/getCatalogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export async function getCatalogs(
const catalogsConfig = config.catalogs
const catalogs: Catalog[] = []

const format = await getFormat(config.format, config.formatOptions)
const format = await getFormat(
config.format,
config.formatOptions,
config.sourceLocale
)

catalogsConfig.forEach((catalog) => {
validateCatalogPath(catalog.path, format.getCatalogExtension())
Expand Down Expand Up @@ -105,7 +109,11 @@ const ensureArray = <T>(value: Array<T> | T | null | undefined): Array<T> => {
* Create catalog for merged messages.
*/
export async function getCatalogForMerge(config: LinguiConfigNormalized) {
const format = await getFormat(config.format, config.formatOptions)
const format = await getFormat(
config.format,
config.formatOptions,
config.sourceLocale
)
validateCatalogPath(config.catalogsMergePath, format.getCatalogExtension())

return new Catalog(
Expand Down
124 changes: 75 additions & 49 deletions packages/cli/src/api/formats/formatterWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,45 @@ import fs from "fs"

describe("FormatterWrapper", () => {
it("should return template and catalog extension", () => {
const wrapper = new FormatterWrapper({
serialize: () => "",
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
})
const wrapper = new FormatterWrapper(
{
serialize: () => "",
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
},
"en"
)

expect(wrapper.getCatalogExtension()).toBe(".po")
expect(wrapper.getTemplateExtension()).toBe(".pot")
})
it("should return catalog extension for templateExtension if not defined", () => {
const wrapper = new FormatterWrapper({
serialize: () => "",
parse: () => ({}),
catalogExtension: ".po",
templateExtension: null,
})
const wrapper = new FormatterWrapper(
{
serialize: () => "",
parse: () => ({}),
catalogExtension: ".po",
templateExtension: null,
},
"en"
)

expect(wrapper.getCatalogExtension()).toBe(".po")
expect(wrapper.getTemplateExtension()).toBe(".po")
})

describe("read", () => {
it("should not throw if file not exists", async () => {
const format = new FormatterWrapper({
serialize: () => "",
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
})
const format = new FormatterWrapper(
{
serialize: () => "",
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
},
"en"
)

mockFs({})

Expand All @@ -46,12 +55,15 @@ describe("FormatterWrapper", () => {
const parseMock = jest
.fn()
.mockImplementation((content: string) => content.split(",") as any)
const format = new FormatterWrapper({
serialize: () => "",
parse: parseMock,
catalogExtension: ".po",
templateExtension: ".pot",
})
const format = new FormatterWrapper(
{
serialize: () => "",
parse: parseMock,
catalogExtension: ".po",
templateExtension: ".pot",
},
"en"
)

mockFs({
"test.file": "red,green,blue",
Expand All @@ -65,21 +77,25 @@ describe("FormatterWrapper", () => {
{
filename: test.file,
locale: en,
sourceLocale: en,
},
]
`)
expect(actual).toStrictEqual(["red", "green", "blue"])
})

it("should rethrow error with filename if failed to parse file", async () => {
const format = new FormatterWrapper({
serialize: () => "",
parse: () => {
throw new Error("Unable to parse")
const format = new FormatterWrapper(
{
serialize: () => "",
parse: () => {
throw new Error("Unable to parse")
},
catalogExtension: ".po",
templateExtension: ".pot",
},
catalogExtension: ".po",
templateExtension: ".pot",
})
"en"
)

mockFs({
"test.file": "blabla",
Expand All @@ -97,12 +113,15 @@ describe("FormatterWrapper", () => {

describe("write", () => {
it("should write to FS and serialize catalog using provided formatter", async () => {
const format = new FormatterWrapper({
serialize: (catalog) => JSON.stringify(catalog),
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
})
const format = new FormatterWrapper(
{
serialize: (catalog) => JSON.stringify(catalog),
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
},
"en"
)

mockFs({})

Expand All @@ -126,12 +145,15 @@ describe("FormatterWrapper", () => {
.fn()
.mockImplementation((catalog) => JSON.stringify(catalog))

const format = new FormatterWrapper({
serialize: serializeMock,
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
})
const format = new FormatterWrapper(
{
serialize: serializeMock,
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
},
"en"
)

mockFs({
"messages.json": `{"existing":{"translation":"Existing message"}}`,
Expand Down Expand Up @@ -159,6 +181,7 @@ describe("FormatterWrapper", () => {
existing: {"existing":{"translation":"Existing message"}},
filename: messages.json,
locale: en,
sourceLocale: en,
},
]
`)
Expand All @@ -168,12 +191,15 @@ describe("FormatterWrapper", () => {
})

it("should write only if file was changed", async () => {
const format = new FormatterWrapper({
serialize: (catalog) => JSON.stringify(catalog),
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
})
const format = new FormatterWrapper(
{
serialize: (catalog) => JSON.stringify(catalog),
parse: () => ({}),
catalogExtension: ".po",
templateExtension: ".pot",
},
"en"
)

const catalog = {
static: {
Expand Down
4 changes: 3 additions & 1 deletion packages/cli/src/api/formats/formatterWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFile, writeFileIfChanged } from "../utils"
import { RethrownError } from "../rethrownError"

export class FormatterWrapper {
constructor(private f: CatalogFormatter) {}
constructor(private f: CatalogFormatter, private sourceLocale: string) {}

getCatalogExtension() {
return this.f.catalogExtension
Expand All @@ -20,6 +20,7 @@ export class FormatterWrapper {
): Promise<void> {
const content = await this.f.serialize(catalog, {
locale,
sourceLocale: this.sourceLocale,
existing: await readFile(filename),
filename,
})
Expand All @@ -37,6 +38,7 @@ export class FormatterWrapper {
try {
return this.f.parse(content, {
locale,
sourceLocale: this.sourceLocale,
filename,
})
} catch (e) {
Expand Down
7 changes: 4 additions & 3 deletions packages/cli/src/api/formats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ export { FormatterWrapper }

export async function getFormat(
_format: CatalogFormat | CatalogFormatter,
options: CatalogFormatOptions
options: CatalogFormatOptions,
sourceLocale: string
): Promise<FormatterWrapper> {
if (typeof _format !== "string") {
return new FormatterWrapper(_format)
return new FormatterWrapper(_format, sourceLocale)
}

const format = formats[_format]
Expand All @@ -83,5 +84,5 @@ export async function getFormat(
)
}

return new FormatterWrapper((await format())(options))
return new FormatterWrapper((await format())(options), sourceLocale)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export async function getExperimentalCatalogs(

const format = await getFormat(
linguiConfig.format,
linguiConfig.formatOptions
linguiConfig.formatOptions,
linguiConfig.sourceLocale
)

return entryPoints.map((entryPoint) => {
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/lingui-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,11 @@ if (require.main === module) {
if (options.watch) {
console.info(chalk.bold("Initializing Watch Mode..."))
;(async function initWatch() {
const format = await getFormat(config.format, config.formatOptions)
const format = await getFormat(
config.format,
config.formatOptions,
config.sourceLocale
)
const catalogs = await getCatalogs(config)

const paths: string[] = []
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/lingui-extract-experimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ export default async function command(

const format = await getFormat(
linguiConfig.format,
linguiConfig.formatOptions
linguiConfig.formatOptions,
linguiConfig.sourceLocale
)

for (const outFile of Object.keys(bundleResult.metafile.outputs)) {
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export const makeCatalog = async (_config: Partial<LinguiConfig> = {}) => {
path: "{locale}/messages",
include: [],
exclude: [],
format: await getFormat(config.format, config.formatOptions),
format: await getFormat(
config.format,
config.formatOptions,
config.sourceLocale
),
},
config
)
Expand Down
9 changes: 7 additions & 2 deletions packages/conf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,16 @@ export type CatalogFormatter = {
templateExtension?: string
parse(
content: string,
ctx: { locale: string | null; filename: string }
ctx: { locale: string | null; sourceLocale: string; filename: string }
): Promise<CatalogType> | CatalogType
serialize(
catalog: CatalogType,
ctx: { locale: string | null; filename: string; existing: string | null }
ctx: {
locale: string | null
sourceLocale: string
filename: string
existing: string | null
}
): Promise<string> | string
}

Expand Down
Loading