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

fix(cli): update translationIO service in CLI package (to handle context) #1949

Merged
merged 4 commits into from
Jun 17, 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
57 changes: 52 additions & 5 deletions packages/cli/src/services/translationIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
url: string
}

const EXPLICIT_ID_FLAG = "js-lingui-explicit-id"
const EXPLICIT_ID_AND_CONTEXT_FLAG = "js-lingui-explicit-id-and-context"

Check warning on line 27 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L26-L27

Added lines #L26 - L27 were not covered by tests

const getCreateHeaders = (language: string) => ({
"POT-Creation-Date": formatDate(new Date(), "yyyy-MM-dd HH:mmxxxx"),
"MIME-Version": "1.0",
Expand Down Expand Up @@ -216,39 +219,83 @@
}

function createSegmentFromPoItem(item: POItem) {
const itemHasId = item.msgid != item.msgstr[0] && item.msgstr[0].length
const itemHasExplicitId = item.extractedComments.includes(EXPLICIT_ID_FLAG)
const itemHasContext = item.msgctxt != null

Check warning on line 223 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L222-L223

Added lines #L222 - L223 were not covered by tests

const segment: TranslationIoSegment = {
type: "source", // No way to edit text for source language (inside code), so not using "key" here
source: itemHasId ? item.msgstr[0] : item.msgid, // msgstr may be empty if --overwrite is used and no ID is used
source: "",
context: "",
references: [],
comment: "",
}

if (itemHasId) {
// For segment.source & segment.context, we must remain compatible with projects created/synced before Lingui V4
if (itemHasExplicitId) {
segment.source = item.msgstr[0]

Check warning on line 235 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L235

Added line #L235 was not covered by tests
segment.context = item.msgid
} else {
segment.source = item.msgid

Check warning on line 238 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L238

Added line #L238 was not covered by tests

if (itemHasContext) {
segment.context = item.msgctxt

Check warning on line 241 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L241

Added line #L241 was not covered by tests
}
}

if (item.references.length) {
segment.references = item.references
}

// Since Lingui v4, when using explicit IDs, Lingui automatically adds 'js-lingui-explicit-id' to the extractedComments array
if (item.extractedComments.length) {
segment.comment = item.extractedComments.join(" | ")

if (itemHasExplicitId && itemHasContext) {
// segment.context is already used for the explicit ID, so we need to pass the context (for translators) in segment.comment
segment.comment = `${item.msgctxt} | ${segment.comment}`

Check warning on line 255 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L255

Added line #L255 was not covered by tests

// Replace the flag to let us know how to recompose a target PO Item that is consistent with the source PO Item
segment.comment = segment.comment.replace(

Check warning on line 258 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L258

Added line #L258 was not covered by tests
EXPLICIT_ID_FLAG,
EXPLICIT_ID_AND_CONTEXT_FLAG
)
}
}

return segment
}

function createPoItemFromSegment(segment: TranslationIoSegment) {
const segmentHasExplicitId = segment.comment?.includes(EXPLICIT_ID_FLAG)
const segmentHasExplicitIdAndContext = segment.comment?.includes(

Check warning on line 270 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L269-L270

Added lines #L269 - L270 were not covered by tests
EXPLICIT_ID_AND_CONTEXT_FLAG
)

const item = new PO.Item()

item.msgid = segment.context ? segment.context : segment.source
if (segmentHasExplicitId || segmentHasExplicitIdAndContext) {
item.msgid = segment.context

Check warning on line 277 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L277

Added line #L277 was not covered by tests
} else {
item.msgid = segment.source
item.msgctxt = segment.context

Check warning on line 280 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L279-L280

Added lines #L279 - L280 were not covered by tests
}

item.msgstr = [segment.target]
item.references =
segment.references && segment.references.length ? segment.references : []
item.extractedComments = segment.comment ? segment.comment.split(" | ") : []

if (segment.comment) {
segment.comment = segment.comment.replace(

Check warning on line 288 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L288

Added line #L288 was not covered by tests
EXPLICIT_ID_AND_CONTEXT_FLAG,
EXPLICIT_ID_FLAG
)
item.extractedComments = segment.comment ? segment.comment.split(" | ") : []

// We recompose a target PO Item that is consistent with the source PO Item
if (segmentHasExplicitIdAndContext) {
item.msgctxt = item.extractedComments.shift()

Check warning on line 296 in packages/cli/src/services/translationIO.ts

View check run for this annotation

Codecov / codecov/patch

packages/cli/src/services/translationIO.ts#L296

Added line #L296 was not covered by tests
}
}

return item
}
Expand Down
7 changes: 4 additions & 3 deletions website/docs/tools/translation-io.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Translation.io

<img src="https://translation.io/assets/logo-for-fb-73866392ffcfc181d7d3d6c1b61fa03dbf60c842cbe82d0f58b38af4597614df.png" alt="Translation.io Lingui Logo" width="300"/>
<br />
<p align="center">
<img src="https://translation.io/logo.png" alt="Translation.io - Localization made simple for tech companies" width="300" />
</p>

[Translation.io](https://translation.io/lingui) is a professional synchronization and collaboration platform that will assist your team in the translation of your Lingui application.

Expand Down Expand Up @@ -42,7 +43,7 @@ Sometimes you have no choice but to confront your translators with HTML or inter

`Hello {name}` should never be translated as `Bonjour {nom}`, and we have several mechanisms to ensure that, like warnings and auto-completion:

![Syntax Highlighting warning on Translation.io](https://translation.io/_articles/2019-10-11-highlighting-of-html-tags-and-interpolated-variables/highlight-interpolated-variable-lingui.png)
![Syntax Highlighting warning on Translation.io](https://translation.io/_articles/translation/2019-10-11-highlighting-of-html-tags-and-interpolated-variables/highlight-interpolated-variable-lingui.png)

---

Expand Down
Loading