From bc33e067b9c087e8adc32fcd85cadba43725e0ab Mon Sep 17 00:00:00 2001 From: Aseer KT <39845171+aseerkt@users.noreply.github.com> Date: Wed, 7 Aug 2024 14:34:03 +0530 Subject: [PATCH] chore(format-po): split multi line comments (#1980) --- .../format-po/src/__snapshots__/po.test.ts.snap | 6 ++++++ packages/format-po/src/po.test.ts | 11 ++++++++++- packages/format-po/src/po.ts | 17 ++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/format-po/src/__snapshots__/po.test.ts.snap b/packages/format-po/src/__snapshots__/po.test.ts.snap index fbb4d13d4..aa06a36ce 100644 --- a/packages/format-po/src/__snapshots__/po.test.ts.snap +++ b/packages/format-po/src/__snapshots__/po.test.ts.snap @@ -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" + `; diff --git a/packages/format-po/src/po.test.ts b/packages/format-po/src/po.test.ts index 26a00708e..4cdee3002 100644 --- a/packages/format-po/src/po.test.ts +++ b/packages/format-po/src/po.test.ts @@ -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[1] = { locale: "en", @@ -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) diff --git a/packages/format-po/src/po.ts b/packages/format-po/src/po.ts index 1cc7af91c..9ecc400d0 100644 --- a/packages/format-po/src/po.ts +++ b/packages/format-po/src/po.ts @@ -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 */ @@ -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