Skip to content

Commit

Permalink
Skip backconversion entirely when there are no components in localize…
Browse files Browse the repository at this point in the history
…d string
  • Loading branch information
wadimw committed Sep 25, 2024
1 parent d052eec commit 57864af
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
31 changes: 17 additions & 14 deletions src/loctool/PendoXliffFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ export class PendoXliffFile implements File {
* Data about markdown syntax escaped in corresponding source strings.
*
* This is used to backconvert localized strings to their original markdown syntax.
*
* Null values indicate that no components were found in the source string
* and there is no need for backconversion.
*/
private componentLists?: { [unitKey: string]: ComponentList };
private componentLists?: { [unitKey: string]: ComponentList | null };

constructor(
sourceFilePath: string,
Expand Down Expand Up @@ -254,7 +257,7 @@ export class PendoXliffFile implements File {

if (componentList.length === 0) {
// no components found, no need to modify the unit
return [unit, componentList] as const;
return [unit, null] as const;
}

// append description of all components to the unit comment
Expand Down Expand Up @@ -390,32 +393,32 @@ export class PendoXliffFile implements File {
if (!translation) {
throw new Error("Missing translation for unit");
}
let target = translation.getTarget();

// make sure that this unit has been processed and there is some data about components for it
if (!(unitData.key in this.componentLists)) {
throw new Error("Missing extracted component data for given unit");
}

// get extracted component list based on TU key
const referenceComponentList = this.componentLists[unitData.key];
if (!referenceComponentList) {
throw new Error("Missing component list for unit");
if (null !== referenceComponentList) {
// use the matching component source string as reference to reinsert the original markdown syntax
// into the localized string
target = backconvert(target, referenceComponentList);
}

// use the matching component source string as reference to reinsert the original markdown syntax
// into the localized string
const target = translation.getTarget();
const unescapedTarget = backconvert(target, referenceComponentList);

// update the target string in the xliff element

// if necessary, create the target element
let targetElement = unitElement.elements?.find((el) => el.name === XLIFF.ELEMENT.TARGET);
if (!targetElement) {
// if necessary, create the target element
targetElement = { type: "element", name: XLIFF.ELEMENT.TARGET, elements: [] };
if (!unitElement.elements) {
unitElement.elements = [];
}
unitElement.elements.push(targetElement);
}

// replace the target string
targetElement.elements = [{ type: "text", text: unescapedTarget }];
targetElement.elements = [{ type: "text", text: target }];

// set target state attribute to "translated"
targetElement.attributes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
Second paragraph of a multi-paragraph string.]]></source>
<target state="translated">Pierwszy akapit wieloakapitowego ciągu.

Drugi akapit wieloakapitowego ciągu.</target>
Drugi akapit wieloakapitowego ciągu.
</target>
<note>TextView</note>
</trans-unit>
<trans-unit id="5f8bec10-d663-43d7-a185-49824564060c|md">
Expand Down

0 comments on commit 57864af

Please sign in to comment.