From b818fedb8d4a63aef317db62abce8205da519a0a Mon Sep 17 00:00:00 2001 From: Aseer KT Date: Thu, 18 Jul 2024 14:43:26 +0530 Subject: [PATCH] chore: split multi line comments --- packages/cli/src/api/catalog/extractFromFiles.ts | 13 ++++++++++++- packages/cli/test/extract-po-format/expected/en.po | 12 ++++++++++++ packages/cli/test/extract-po-format/expected/pl.po | 12 ++++++++++++ .../cli/test/extract-po-format/fixtures/file-b.tsx | 14 ++++++++++++++ packages/cli/test/index.test.ts | 4 ++-- 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/cli/src/api/catalog/extractFromFiles.ts b/packages/cli/src/api/catalog/extractFromFiles.ts index 9922798bf..33ae8ca43 100644 --- a/packages/cli/src/api/catalog/extractFromFiles.ts +++ b/packages/cli/src/api/catalog/extractFromFiles.ts @@ -9,6 +9,17 @@ import extract from "../extractors" import { ExtractedCatalogType, MessageOrigin } from "../types" import { prettyOrigin } from "../utils" +const formatComment = (comment: string) => { + // split multi line comment + if (comment.includes("\n")) { + return comment + .split("\n") // split comments by newline character + .filter(Boolean) // eliminate empty strings + .map((commentSlice) => commentSlice.trim()) // trim whitespace + } + return [comment] +} + export async function extractFromFiles( paths: string[], config: LinguiConfigNormalized @@ -52,7 +63,7 @@ export async function extractFromFiles( ...prev, message: prev.message ?? next.message, comments: next.comment - ? [...prev.comments, next.comment] + ? [...prev.comments, ...formatComment(next.comment)] : prev.comments, origin: [...prev.origin, [filename, next.origin[1]]], } diff --git a/packages/cli/test/extract-po-format/expected/en.po b/packages/cli/test/extract-po-format/expected/en.po index 123a182b6..604151178 100644 --- a/packages/cli/test/extract-po-format/expected/en.po +++ b/packages/cli/test/extract-po-format/expected/en.po @@ -45,3 +45,15 @@ msgstr "This JSX element has custom id" #: fixtures/file-a.ts:11 msgid "custom.id" msgstr "This message has custom id" + +#. multi line +#. comment 1 +#: fixtures/file-b.tsx:21 +msgid "translation with multi line comment 1" +msgstr "translation with multi line comment 1" + +#. multi line +#. comment 2 +#: fixtures/file-b.tsx:25 +msgid "translation with multi line comment 2" +msgstr "translation with multi line comment 2" diff --git a/packages/cli/test/extract-po-format/expected/pl.po b/packages/cli/test/extract-po-format/expected/pl.po index 85c6a6c97..e311b4c2b 100644 --- a/packages/cli/test/extract-po-format/expected/pl.po +++ b/packages/cli/test/extract-po-format/expected/pl.po @@ -45,3 +45,15 @@ msgstr "" #: fixtures/file-a.ts:11 msgid "custom.id" msgstr "" + +#. multi line +#. comment 1 +#: fixtures/file-b.tsx:21 +msgid "translation with multi line comment 1" +msgstr "" + +#. multi line +#. comment 2 +#: fixtures/file-b.tsx:25 +msgid "translation with multi line comment 2" +msgstr "" diff --git a/packages/cli/test/extract-po-format/fixtures/file-b.tsx b/packages/cli/test/extract-po-format/fixtures/file-b.tsx index 5c771448f..5cf728205 100644 --- a/packages/cli/test/extract-po-format/fixtures/file-b.tsx +++ b/packages/cli/test/extract-po-format/fixtures/file-b.tsx @@ -14,3 +14,17 @@ export function MyComponent2() { export function MyComponent3() { return This JSX element has custom id } + + +export function MyComponent4() { + return
+ + translation with multi line comment 1 + + + translation with multi line comment 2 + +
+} \ No newline at end of file diff --git a/packages/cli/test/index.test.ts b/packages/cli/test/index.test.ts index e53d3b81a..62b993aad 100644 --- a/packages/cli/test/index.test.ts +++ b/packages/cli/test/index.test.ts @@ -69,8 +69,8 @@ describe("E2E Extractor Test", () => { ┌─────────────┬─────────────┬─────────┐ │ Language │ Total count │ Missing │ ├─────────────┼─────────────┼─────────┤ - │ en (source) │ 8 │ - │ - │ pl │ 8 │ 8 │ + │ en (source) │ 10 │ - │ + │ pl │ 10 │ 10 │ └─────────────┴─────────────┴─────────┘ (use "yarn extract" to update catalogs with new messages)