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

chore(format-po): split multi line comments #1980

Merged
merged 5 commits into from
Aug 7, 2024
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
6 changes: 6 additions & 0 deletions packages/format-po/src/__snapshots__/po.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,10 @@ msgstr "Keeps any flags that are defined"
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"

#. hello
#. world
#. js-lingui-explicit-id
msgid "withMultiLineComments"
msgstr "Message with multi line comments"

`;
11 changes: 10 additions & 1 deletion packages/format-po/src/po.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fs from "fs"
import path from "path"

import { formatter as createFormatter, POCatalogExtra } from "./po"
import { CatalogFormatter, CatalogType } from "@lingui/conf"
import MockDate from "mockdate"
import { formatter as createFormatter, POCatalogExtra } from "./po"

const defaultParseCtx: Parameters<CatalogFormatter["parse"]>[1] = {
locale: "en",
Expand Down Expand Up @@ -87,6 +87,15 @@ describe("pofile format", () => {
" helplessly as he looked. \"What's happened to me?\" he thought. It wasn't" +
" a dream. His room, a proper human",
},
withMultiLineComments: {
translation: "Message with multi line comments",
comments: [
`hello
world

`,
],
},
}

const pofile = format.serialize(catalog, defaultSerializeCtx)
Expand Down
17 changes: 16 additions & 1 deletion packages/format-po/src/po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ const splitOrigin = (origin: string) => {
return [file, line ? Number(line) : null] as [file: string, line: number]
}

const splitMultiLineComments = (comments: string[]) => {
return comments.flatMap((comment) =>
comment.includes("\n")
? comment
.split("\n")
.map((slice) => slice.trim())
.filter(Boolean)
: comment
)
}

/**
* @internal
*/
Expand Down Expand Up @@ -96,7 +107,11 @@ const serialize = (catalog: CatalogType, options: PoFormatterOptions) => {

// The extractedComments array may be modified in this method,
// so create a new array with the message's elements.
item.extractedComments = [...(message.comments || [])]
item.extractedComments = [
...(message.comments?.length
? splitMultiLineComments(message.comments)
: []),
]

item.flags = ((message.extra?.flags || []) as string[]).reduce<
Record<string, boolean>
Expand Down
Loading