Skip to content

Commit

Permalink
chore(format-po): split multi line comments (#1980)
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt authored Aug 7, 2024
1 parent aff6579 commit bc33e06
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
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

0 comments on commit bc33e06

Please sign in to comment.