From ba7476bb5a8f79a0afbe454b635c3fd3bb65901e Mon Sep 17 00:00:00 2001 From: Didier Toussaint Date: Tue, 11 Jun 2024 10:55:03 +0200 Subject: [PATCH 1/3] Handle context when syncing with Translation.io --- packages/cli/src/services/translationIO.ts | 52 +++++++++++++++++++--- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/packages/cli/src/services/translationIO.ts b/packages/cli/src/services/translationIO.ts index d2959d5d4..cc81e1797 100644 --- a/packages/cli/src/services/translationIO.ts +++ b/packages/cli/src/services/translationIO.ts @@ -23,6 +23,9 @@ type TranslationIoProject = { url: string } +const EXPLICIT_ID_FLAG = "js-lingui-explicit-id" +const EXPLICIT_ID_AND_CONTEXT_FLAG = "js-lingui-explicit-id-and-context" + const getCreateHeaders = (language: string) => ({ "POT-Creation-Date": formatDate(new Date(), "yyyy-MM-dd HH:mmxxxx"), "MIME-Version": "1.0", @@ -216,39 +219,74 @@ function sync( } 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 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] segment.context = item.msgid + } else { + segment.source = item.msgid + + if (itemHasContext) { + segment.context = item.msgctxt + } } 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}` + + // 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(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(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 + } else { + item.msgid = segment.source + item.msgctxt = segment.context + } + item.msgstr = [segment.target] - item.references = - segment.references && segment.references.length ? segment.references : [] - item.extractedComments = segment.comment ? segment.comment.split(" | ") : [] + item.references = segment.references && segment.references.length ? segment.references : [] + + if (segment.comment) { + segment.comment = segment.comment.replace(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() + } + } return item } From c5d30db8bc6c59f77b4e0a058b2b5526af460c5a Mon Sep 17 00:00:00 2001 From: Didier Toussaint Date: Tue, 11 Jun 2024 11:18:55 +0200 Subject: [PATCH 2/3] Update logo + fix image link on Translation.io in docs/tools --- website/docs/tools/translation-io.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/website/docs/tools/translation-io.md b/website/docs/tools/translation-io.md index 7dd9dd109..b55819563 100644 --- a/website/docs/tools/translation-io.md +++ b/website/docs/tools/translation-io.md @@ -1,7 +1,8 @@ # Translation.io -Translation.io Lingui Logo -
+

+ Translation.io - Localization made simple for tech companies +

[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. @@ -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) --- From 9ebaf654eb5c6871dc3db7d64badaa066dbeaec3 Mon Sep 17 00:00:00 2001 From: Didier Toussaint Date: Wed, 12 Jun 2024 13:50:01 +0200 Subject: [PATCH 3/3] Fix syntax (I had not run prettier) --- packages/cli/src/services/translationIO.ts | 25 +++++++++++++++------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/cli/src/services/translationIO.ts b/packages/cli/src/services/translationIO.ts index cc81e1797..27e0ed7e2 100644 --- a/packages/cli/src/services/translationIO.ts +++ b/packages/cli/src/services/translationIO.ts @@ -235,11 +235,11 @@ function createSegmentFromPoItem(item: POItem) { segment.source = item.msgstr[0] segment.context = item.msgid } else { - segment.source = item.msgid + segment.source = item.msgid - if (itemHasContext) { - segment.context = item.msgctxt - } + if (itemHasContext) { + segment.context = item.msgctxt + } } if (item.references.length) { @@ -255,7 +255,10 @@ function createSegmentFromPoItem(item: POItem) { segment.comment = `${item.msgctxt} | ${segment.comment}` // 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(EXPLICIT_ID_FLAG, EXPLICIT_ID_AND_CONTEXT_FLAG) + segment.comment = segment.comment.replace( + EXPLICIT_ID_FLAG, + EXPLICIT_ID_AND_CONTEXT_FLAG + ) } } @@ -264,7 +267,9 @@ function createSegmentFromPoItem(item: POItem) { function createPoItemFromSegment(segment: TranslationIoSegment) { const segmentHasExplicitId = segment.comment?.includes(EXPLICIT_ID_FLAG) - const segmentHasExplicitIdAndContext = segment.comment?.includes(EXPLICIT_ID_AND_CONTEXT_FLAG) + const segmentHasExplicitIdAndContext = segment.comment?.includes( + EXPLICIT_ID_AND_CONTEXT_FLAG + ) const item = new PO.Item() @@ -276,10 +281,14 @@ function createPoItemFromSegment(segment: TranslationIoSegment) { } item.msgstr = [segment.target] - item.references = segment.references && segment.references.length ? segment.references : [] + item.references = + segment.references && segment.references.length ? segment.references : [] if (segment.comment) { - segment.comment = segment.comment.replace(EXPLICIT_ID_AND_CONTEXT_FLAG, EXPLICIT_ID_FLAG) + segment.comment = segment.comment.replace( + 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