Skip to content

Commit

Permalink
feat(format): initial support for external formatters (#1511)
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko authored Mar 16, 2023
1 parent 7315a23 commit 4f1d30e
Show file tree
Hide file tree
Showing 50 changed files with 1,461 additions and 3,186 deletions.
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 @@ -58,10 +58,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 @@ -82,10 +82,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 @@ -103,10 +103,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 @@ -129,15 +129,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 @@ -153,7 +153,7 @@ describe("Catalog", () => {
})
)

const translations = catalog.getTranslations("en", {
const translations = await catalog.getTranslations("en", {
sourceLocale: "en",
fallbackLocales: {
default: "en",
Expand Down Expand Up @@ -361,7 +361,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 @@ -375,11 +375,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 @@ -396,13 +396,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 @@ -419,15 +419,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 @@ -442,64 +442,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 @@ -641,12 +587,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

0 comments on commit 4f1d30e

Please sign in to comment.