Skip to content

Commit

Permalink
support hash based ids in po formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Feb 15, 2023
1 parent d4dfa63 commit 4ae8cb5
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@babel/core": "^7.20.12",
"@lingui/babel-plugin-extract-messages": "3.17.1",
"@lingui/conf": "3.17.1",
"@lingui/message-id": "3.17.1",
"@lingui/core": "3.17.1",
"@messageformat/parser": "^5.0.0",
"babel-plugin-macros": "^3.0.1",
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/src/api/formats/__snapshots__/po-gettext.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: {someCount, plural, one {Singular case} other {Case number {someCount}}},
Expand All @@ -18,6 +19,7 @@ Object {
Comment made by the developers.,
],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: ,
Expand All @@ -27,6 +29,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: {anotherCount, plural, one {Singular case} other {Case number {anotherCount}}},
Expand All @@ -36,6 +39,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: ,
Expand Down Expand Up @@ -93,6 +97,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: {someCount, plural, one {Singular case} other {Case number {someCount}}},
Expand All @@ -104,6 +109,7 @@ Object {
Comment made by the developers.,
],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: ,
Expand All @@ -113,6 +119,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: {anotherCount, plural, one {Singular case} other {Case number {anotherCount}}},
Expand All @@ -122,6 +129,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: ,
Expand All @@ -141,6 +149,7 @@ Object {
Second description?,
],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Second description joins translator comments,
Expand All @@ -154,6 +163,7 @@ Object {
Third comment,
],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Extra comments are separated from the first description line,
Expand All @@ -168,6 +178,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: true,
origin: Array [],
translation: Is marked as obsolete,
Expand All @@ -177,6 +188,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Static message,
Expand All @@ -186,6 +198,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: 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,
Expand All @@ -198,6 +211,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Support translator comments separately,
Expand All @@ -209,6 +223,7 @@ Object {
Description is comment from developers to translators,
],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Message with description,
Expand All @@ -221,6 +236,7 @@ Object {
fuzzy,
otherFlag,
],
message: undefined,
obsolete: false,
origin: Array [],
translation: Keeps any flags that are defined,
Expand All @@ -230,6 +246,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [
Array [
Expand All @@ -248,6 +265,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [
Array [
Expand All @@ -267,6 +285,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: This is just fine,
Expand Down
19 changes: 19 additions & 0 deletions packages/cli/src/api/formats/__snapshots__/po.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Object {
Second description?,
],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Second description joins translator comments,
Expand All @@ -25,6 +26,7 @@ Object {
Third comment,
],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Extra comments are separated from the first description line,
Expand All @@ -39,6 +41,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: true,
origin: Array [],
translation: Is marked as obsolete,
Expand All @@ -48,6 +51,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Static message,
Expand All @@ -57,6 +61,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: 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,
Expand All @@ -69,6 +74,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Support translator comments separately,
Expand All @@ -80,6 +86,7 @@ Object {
Description is comment from developers to translators,
],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [],
translation: Message with description,
Expand All @@ -92,6 +99,7 @@ Object {
fuzzy,
otherFlag,
],
message: undefined,
obsolete: false,
origin: Array [],
translation: Keeps any flags that are defined,
Expand All @@ -101,6 +109,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [
Array [
Expand All @@ -119,6 +128,7 @@ Object {
context: null,
extractedComments: Array [],
flags: Array [],
message: undefined,
obsolete: false,
origin: Array [
Array [
Expand Down Expand Up @@ -162,6 +172,15 @@ msgstr "Static message"
msgid "withOrigin"
msgstr "Message with origin"
msgctxt "my context"
msgid "withContext"
msgstr "Message with context"
#, generated-id
msgctxt "my context"
msgid "with generated id"
msgstr ""
#: src/App.js:4
#: src/Component.js:2
msgid "withMultipleOrigins"
Expand Down
33 changes: 33 additions & 0 deletions packages/cli/src/api/formats/po.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ describe("pofile format", () => {
translation: "Message with origin",
origin: [["src/App.js", 4]],
},
withContext: {
translation: "Message with context",
context: "my context",
},
Dgzql1: {
message: "with generated id",
translation: "",
context: "my context",
},
withMultipleOrigins: {
translation: "Message with multiple origin",
origin: [
Expand Down Expand Up @@ -97,6 +106,30 @@ describe("pofile format", () => {
expect(actual).toMatchSnapshot()
})

it("should serialize and deserialize messages with generated id", () => {
mockFs({
locale: {
en: mockFs.directory(),
},
})

const catalog: CatalogType = {
// with generated id
Dgzql1: {
message: "with generated id",
translation: "",
context: "my context",
},
}

const filename = path.join("locale", "en", "messages.po")
format.write(filename, catalog, { origins: true, locale: "en" })

const actual = format.read(filename)
mockFs.restore()
expect(actual).toMatchObject(catalog)
})

it("should correct badly used comments", () => {
const po = PO.parse(`
#. First description
Expand Down
51 changes: 38 additions & 13 deletions packages/cli/src/api/formats/po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import PO from "pofile"
import { joinOrigin, splitOrigin, writeFileIfChanged } from "../utils"
import { CatalogType, MessageType } from "../catalog"
import { CatalogFormatOptionsInternal, CatalogFormatter } from "."
import { generateMessageId } from "@lingui/message-id"

type POItem = InstanceType<typeof PO.Item>

function isGeneratedId(id: string, message: MessageType): boolean {
return id === generateMessageId(message.message, message.context)
}
function getCreateHeaders(language = "no"): PO["headers"] {
return {
"POT-Creation-Date": formatDate(new Date(), "yyyy-MM-dd HH:mmxxxx"),
Expand All @@ -28,17 +32,34 @@ export const serialize = (
const message = catalog[id]

const item = new PO.Item()
item.msgid = id
item.msgstr = [message.translation]
item.comments = message.comments || []

// The extractedComments array may be modified in this method, so create a new array with the message's elements.
// Destructuring `undefined` is forbidden, so fallback to `[]` if the message has no extracted comments.
item.extractedComments = [...(message.extractedComments ?? [])]
// The extractedComments array may be modified in this method,
// so create a new array with the message's elements.
item.extractedComments = [...(message.extractedComments || [])]

item.flags = (message.flags || []).reduce<Record<string, boolean>>(
(acc, flag) => {
acc[flag] = true
return acc
},
{}
)

if (isGeneratedId(id, message)) {
item.msgid = message.message
item.flags["generated-id"] = true
// item.extractedComments.push(`lingui_id: ${id}`)
} else {
item.msgid = id
}

if (message.context) {
item.msgctxt = message.context
}

item.msgstr = [message.translation]
item.comments = message.comments || []

if (options.origins !== false) {
if (message.origin && options.lineNumbers === false) {
item.references = message.origin.map(([path]) => path)
Expand All @@ -47,12 +68,6 @@ export const serialize = (
}
}
item.obsolete = message.obsolete
item.flags = message.flags
? message.flags.reduce<Record<string, boolean>>((acc, flag) => {
acc[flag] = true
return acc
}, {})
: {}

return postProcessItem ? postProcessItem(item, message, id) : item
})
Expand All @@ -65,8 +80,18 @@ export function deserialize(
return items.reduce<CatalogType>((catalog, item) => {
onItem(item)

catalog[item.msgid] = {
let id = item.msgid
let message: string

// is generated id, recreate it
if (item.flags["generated-id"]) {
id = generateMessageId(item.msgid, item.msgctxt)
message = item.msgid
}

catalog[id] = {
translation: item.msgstr[0],
message,
extractedComments: item.extractedComments || [],
comments: item.comments || [],
context: item.msgctxt ?? null,
Expand Down

0 comments on commit 4ae8cb5

Please sign in to comment.