Skip to content

Commit

Permalink
chore: split multi line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aseerkt committed Jul 18, 2024
1 parent f65c90b commit b818fed
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
13 changes: 12 additions & 1 deletion packages/cli/src/api/catalog/extractFromFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]]],
}
Expand Down
12 changes: 12 additions & 0 deletions packages/cli/test/extract-po-format/expected/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
12 changes: 12 additions & 0 deletions packages/cli/test/extract-po-format/expected/pl.po
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
14 changes: 14 additions & 0 deletions packages/cli/test/extract-po-format/fixtures/file-b.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,17 @@ export function MyComponent2() {
export function MyComponent3() {
return <Trans id={"jsx.custom.id"}>This JSX element has custom id</Trans>
}


export function MyComponent4() {
return <div>
<Trans comment="multi line
comment 1">
translation with multi line comment 1
</Trans>
<Trans comment={`multi line
comment 2`}>
translation with multi line comment 2
</Trans>
</div>
}
4 changes: 2 additions & 2 deletions packages/cli/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b818fed

Please sign in to comment.