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(format): initial support for external formatters #1511

Merged
merged 10 commits into from
Mar 16, 2023
Merged
21 changes: 0 additions & 21 deletions packages/cli/src/api/__snapshots__/catalog.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -853,27 +853,6 @@ exports[`Catalog readAll should read existing catalogs for all locales 1`] = `
}
`;

exports[`Catalog should convert catalog format 1`] = `
msgid ""
msgstr ""
"Project-Id-Version: \\n"
"Report-Msgid-Bugs-To: \\n"
"POT-Creation-Date: \\n"
"PO-Revision-Date: \\n"
"Last-Translator: \\n"
"Language: \\n"
"Language-Team: \\n"
"Content-Type: \\n"
"Content-Transfer-Encoding: \\n"
"Plural-Forms: \\n"

msgid "static"
msgstr "Static message"

msgid "veryLongString"
msgstr "One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. \\"What's happened to me?\\" he thought. It wasn't a dream. His room, a proper human"
`;

exports[`cleanObsolete should remove obsolete messages from catalog 1`] = `
{
Label: {
Expand Down
100 changes: 23 additions & 77 deletions packages/cli/src/api/catalog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mockFs from "mock-fs"
import { mockConsole } from "@lingui/jest-mocks"
import { LinguiConfig, makeConfig } from "@lingui/conf"

import { Catalog, cleanObsolete, order, CatalogProps } from "./catalog"
import { Catalog, cleanObsolete, order } from "./catalog"
import { createCompiledCatalog } from "./compile"

import {
Expand Down Expand Up @@ -57,10 +57,10 @@ describe("Catalog", () => {
)

// Everything should be empty
expect(catalog.readAll()).toMatchSnapshot()
expect(await catalog.readAll()).toMatchSnapshot()

await catalog.make(defaultMakeOptions)
expect(catalog.readAll()).toMatchSnapshot()
expect(await catalog.readAll()).toMatchSnapshot()
})

it("should only update the specified locale", async () => {
Expand All @@ -81,10 +81,10 @@ describe("Catalog", () => {
)

// Everything should be empty
expect(catalog.readAll()).toMatchSnapshot()
expect(await catalog.readAll()).toMatchSnapshot()

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

it("should merge with existing catalogs", async () => {
Expand All @@ -102,10 +102,10 @@ describe("Catalog", () => {
)

// Everything should be empty
expect(catalog.readAll()).toMatchSnapshot()
expect(await catalog.readAll()).toMatchSnapshot()

await catalog.make(defaultMakeOptions)
expect(catalog.readAll()).toMatchSnapshot()
expect(await catalog.readAll()).toMatchSnapshot()
})
})

Expand All @@ -128,15 +128,15 @@ describe("Catalog", () => {
)

// Everything should be empty
expect(catalog.readTemplate()).toMatchSnapshot()
expect(await catalog.readTemplate()).toMatchSnapshot()

await catalog.makeTemplate(defaultMakeTemplateOptions)
expect(catalog.readTemplate()).toMatchSnapshot()
expect(await catalog.readTemplate()).toMatchSnapshot()
})
})

describe("POT Flow", () => {
it("Should get translations from template if locale file not presented", () => {
it("Should get translations from template if locale file not presented", async () => {
const catalog = new Catalog(
{
name: "messages",
Expand All @@ -152,7 +152,7 @@ describe("Catalog", () => {
})
)

const translations = catalog.getTranslations("en", {
const translations = await catalog.getTranslations("en", {
sourceLocale: "en",
fallbackLocales: {
default: "en",
Expand Down Expand Up @@ -340,7 +340,7 @@ describe("Catalog", () => {
})

describe("read", () => {
it("should return null if file does not exist", () => {
it("should return null if file does not exist", async () => {
// mock empty filesystem
mockFs()

Expand All @@ -354,11 +354,11 @@ describe("Catalog", () => {
mockConfig()
)

const messages = catalog.read("en")
const messages = await catalog.read("en")
expect(messages).toBeNull()
})

it("should read file in given format", () => {
it("should read file in given format", async () => {
mockFs({
en: {
"messages.po": fs.readFileSync(
Expand All @@ -375,13 +375,13 @@ describe("Catalog", () => {
mockConfig()
)

const messages = catalog.read("en")
const messages = await catalog.read("en")

mockFs.restore()
expect(messages).toMatchSnapshot()
})

it("should read file in previous format", () => {
it("should read file in previous format", async () => {
mockFs({
en: {
"messages.json": fs.readFileSync(
Expand All @@ -398,15 +398,15 @@ describe("Catalog", () => {
mockConfig({ prevFormat: "minimal" })
)

const messages = catalog.read("en")
const messages = await catalog.read("en")

mockFs.restore()
expect(messages).toMatchSnapshot()
})
})

describe("readAll", () => {
it("should read existing catalogs for all locales", () => {
it("should read existing catalogs for all locales", async () => {
const catalog = new Catalog(
{
name: "messages",
Expand All @@ -421,64 +421,10 @@ describe("Catalog", () => {
})
)

const messages = catalog.readAll()
const messages = await catalog.readAll()
expect(messages).toMatchSnapshot()
})
})

/**
* Convert JSON format to PO and then back to JSON.
* - Compare that original and converted JSON file are identical
* - Check the content of PO file
*/
it.skip("should convert catalog format", () => {
mockFs({
en: {
"messages.json": fs.readFileSync(
path.resolve(__dirname, "formats/fixtures/messages.json")
),
"messages.po": mockFs.file(),
},
})

const fileContent = (format: string) =>
fs
.readFileSync("./en/messages." + (format === "po" ? "po" : "json"))
.toString()
.trim()

const catalogConfig: CatalogProps = {
name: "messages",
path: "{locale}/messages",
include: [],
}

const originalJson = fileContent("json")
const po2json = new Catalog(
catalogConfig,
mockConfig({
format: "po",
prevFormat: "minimal",
})
)
po2json.write("en", po2json.read("en"))
const convertedPo = fileContent("po")

const json2po = new Catalog(
catalogConfig,
mockConfig({
format: "minimal",
prevFormat: "po",
localeDir: ".",
})
)
json2po.write("en", json2po.read("en"))
const convertedJson = fileContent("json")

mockFs.restore()
expect(originalJson).toEqual(convertedJson)
expect(convertedPo).toMatchSnapshot()
})
})

describe("cleanObsolete", () => {
Expand Down Expand Up @@ -620,12 +566,12 @@ describe("writeCompiled", () => {
{ namespace: "global.test", extension: /\.js$/ },
])(
"Should save namespace $namespace in $extension extension",
({ namespace, extension }) => {
async ({ namespace, extension }) => {
const compiledCatalog = createCompiledCatalog("en", {}, { namespace })
// Test that the file extension of the compiled catalog is `.mjs`
expect(catalog.writeCompiled("en", compiledCatalog, namespace)).toMatch(
extension
)
expect(
await catalog.writeCompiled("en", compiledCatalog, namespace)
).toMatch(extension)
}
)
})
Loading