Skip to content

Commit

Permalink
refactor(format): remove parse from public api of formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Mar 15, 2023
1 parent f1626e7 commit d53b62b
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 40 deletions.
4 changes: 0 additions & 4 deletions packages/cli/src/api/formats/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,5 @@ export default function (): CatalogFormatter {
throw new Error(`Cannot read ${filename}: ${(e as Error).message}`)
}
},

async parse(content: string) {
return deserialize(content)
},
}
}
4 changes: 0 additions & 4 deletions packages/cli/src/api/formats/lingui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,5 @@ export default function (
throw new Error(`Cannot read ${filename}: ${(e as Error).message}`)
}
},

async parse(content) {
return content as CatalogType
},
}
}
4 changes: 0 additions & 4 deletions packages/cli/src/api/formats/minimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,5 @@ export default function (): CatalogFormatter {
throw new Error(`Cannot read ${filename}: ${(e as Error).message}`)
}
},

async parse(content: Record<string, any>) {
return deserialize(content)
},
}
}
14 changes: 4 additions & 10 deletions packages/cli/src/api/formats/po-gettext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import mockDate from "mockdate"
import path from "path"

import { CatalogType } from "../types"
import createFormat, { serialize } from "./po-gettext"
import createFormat, { serialize, parse } from "./po-gettext"

describe("po-gettext format", () => {
afterEach(() => {
Expand Down Expand Up @@ -98,15 +98,13 @@ describe("po-gettext format", () => {
})

it("should convert gettext plurals to ICU plural messages", async () => {
const format = createFormat()

const pofile = fs
.readFileSync(
path.join(path.resolve(__dirname), "fixtures", "messages_plural.po")
)
.toString()

const catalog = await format.parse(pofile)
const catalog = await parse(pofile)
expect(catalog).toMatchSnapshot()
})

Expand Down Expand Up @@ -147,8 +145,6 @@ describe("po-gettext format", () => {
})

it("should use correct ICU plural cases for languages having an additional plural case for fractions", async () => {
const format = createFormat()

// This tests the edge case described in https://github.com/lingui/js-lingui/pull/677#issuecomment-737152022
const po = `
msgid ""
Expand All @@ -163,7 +159,7 @@ msgstr[1] "# dny"
msgstr[2] "# dní"
`

const parsed = await format.parse(po)
const parsed = await parse(po)

expect(parsed).toEqual({
Y8Xw2Y: {
Expand Down Expand Up @@ -237,8 +233,6 @@ msgstr[2] "# dní"
})

it("convertPluralsToIco handle correctly locales with 4-letter", async () => {
const format = createFormat()

const pofile = fs
.readFileSync(
path.join(
Expand All @@ -249,7 +243,7 @@ msgstr[2] "# dní"
)
.toString()

const catalog = await format.parse(pofile)
const catalog = await parse(pofile)
expect(catalog).toMatchSnapshot()
})
})
28 changes: 14 additions & 14 deletions packages/cli/src/api/formats/po-gettext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,19 @@ const convertPluralsToICU = (
item.msgstr = ["{" + pluralizeOn + ", plural, " + pluralClauses + "}"]
}

export function parse(raw: string) {
const po = PO.parse(raw)

// .po plurals are numbered 0-N and need to be mapped to ICU plural classes ("one", "few", "many"...). Different
// languages can have different plural classes (some start with "zero", some with "one"), so read that data from CLDR.
// `pluralForms` may be `null` if lang is not found. As long as no plural is used, don't report an error.
let pluralForms = getPluralCases(po.headers.Language)

return deserialize(po.items, (item) => {
convertPluralsToICU(item, pluralForms, po.headers.Language)
})
}

export default function (
options: PoGetTextFormatterOptions = {}
): CatalogFormatter {
Expand Down Expand Up @@ -300,20 +313,7 @@ export default function (
return null
}

return this.parse(raw)
},

async parse(raw: string) {
const po = PO.parse(raw)

// .po plurals are numbered 0-N and need to be mapped to ICU plural classes ("one", "few", "many"...). Different
// languages can have different plural classes (some start with "zero", some with "one"), so read that data from CLDR.
// `pluralForms` may be `null` if lang is not found. As long as no plural is used, don't report an error.
let pluralForms = getPluralCases(po.headers.Language)

return deserialize(po.items, (item) => {
convertPluralsToICU(item, pluralForms, po.headers.Language)
})
return parse(raw)
},
}
}
3 changes: 0 additions & 3 deletions packages/cli/src/api/formats/po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,7 @@ export default function (options: PoFormatterOptions = {}): CatalogFormatter {
if (!raw) {
return null
}
return this.parse(raw)
},

async parse(raw: string) {
const po = PO.parse(raw)
return deserialize(po.items, validateItem)
},
Expand Down
1 change: 0 additions & 1 deletion packages/conf/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export type CatalogFormatter = {
ctx?: { locale: string }
): Promise<void>
read(filename: string): Promise<CatalogType | null>
parse(content: unknown): Promise<CatalogType | null>
}

export type ExtractedMessage = {
Expand Down

0 comments on commit d53b62b

Please sign in to comment.