fix(api): import XLIFF units in groups without an id and keep spaces between placeholders [T-2630]

Merged
EV
5d ago
+329-57

What was wrong

XLIFF 1.2 import (format=xliff12) passed the whole file to the xliff library (api/src/formatters/xliff.ts:63, xliff12ToJs(data, (err, res) => {). That library keys every group and unit by child.attributes.id, and xliff.ts:29 (if (unit.groupUnits) {) could only walk what was left:

  • In XLIFF 1.2 the <group> id is optional. A <group> with no attributes made the library throw Cannot read properties of undefined (reading 'id'), so the import answered 400 "Malformed import file" for the whole file.
  • Groups that have attributes but no id (<group resname="g1">, <group resname="g2">) all landed under the key "undefined", so only the units of the last group were kept. The import reported success, but the terms of the other groups were never created or updated.
  • The library calls xml-js with default options, which drop whitespace-only text. A target of <x id="1" equiv-text="{a}"/> <x id="2" equiv-text="{b}"/> was stored as {a}{b}, and <mrk>Hallo</mrk> <mrk>Welt</mrk> as HalloWelt.

While checking the library I also confirmed two smaller problems of the same kind. Two <file> elements with the same original were keyed by that name, so only the last file's units were imported. A <trans-unit> with attributes but no id was imported as a term named undefined.

What changed

  • api/src/formatters/xliff.ts: the parser now reads XLIFF 1.2 with xml-js directly (captureSpacesBetweenElements: true; xml-js is already a direct dependency, used by android-xml.ts and resx.ts). It walks every <file>, its <body>, groups at any depth (id optional) and <trans-unit>s, and ignores text and comments outside <source> and <target>.
  • The current rules stay the same:
    • the target side is used when the first file has target-language, and units with no value on that side are skipped;
    • <x> becomes its equiv-text, <g>, <mrk> and <ph> become their text, and other inline elements are dropped;
    • the source language is that of the first file;
    • CDATA is kept;
    • a text that ends in a line break and indentation loses that end, as the library did.
  • Whitespace-only text between two nodes is now part of the value. At the start or end of a value it is still dropped, as before.
  • Every <file> is imported, even when several share the same original.
  • A <trans-unit> without an id now gets the file refused as malformed (400). Before, it was imported as the term undefined, or refused when the unit had no attributes. XLIFF 1.2 requires that id. If you would rather skip such units, it is a one-line change.
  • xliffExporter is unchanged. The xliff dependency stays, because the exporter uses it.
  • docs/changelog.md: a line under next.

To check that nothing else changed, I ran the old and new parsers on 52 XLIFF inputs in a throwaway spec that is not committed. The inputs covered leading, trailing and indented whitespace, values that are only a placeholder, <g>, <bpt>/<ept>, <it>, <sub>, CDATA, comments, entities, empty and whitespace-only targets, groups with ids, <note>/<context-group> and <alt-trans>. Both parsers returned the same result for every input.

Tests (fail before, pass after)

api/src/formatters/xliff.spec.ts: 6 new tests.

  • Before the fix: 6 failed, 9 passed.
  • After the fix: 15 of 15 pass.
  • The new tests cover:
    • a group without attributes;
    • two groups without an id (both units imported);
    • nested groups without an id;
    • a space between two <x/> and between two <mrk>;
    • two <file> elements with the same original;
    • a <trans-unit> without an id (refused).

api/test/import.e2e-spec.ts: a new case imports two groups without an id into de_DE and checks that both terms and both translations exist.

  • Before the fix it failed: the response was added: 1, upserted: 1 where added: 2, upserted: 2 was expected.
  • After the fix it passes, along with the other 10 cases in that file.

Checks run (Windows, Node 24)

  • tslint -p tsconfig.json -c tslint.json (yarn --cwd api lint): clean.
  • api unit tests (jest --maxWorkers=2): 237 of 238 pass. The one failure is properties.spec.ts "should export properties files". It fails only on Windows: the properties library writes \r\n on win32. That file is not touched here, and the test passes on Linux CI.
  • The full api e2e suite (jest --config ./test/jest-e2e.json --runInBand --forceExit, SQLite): 21 suites, 192 of 192 pass.
  • tsc build plus copying the templates (yarn --cwd api build): passes.
  • prettier --check "**/*.ts" at the repo root (the CircleCI format check): clean.

Fleet task T-2630; authored by the lead lane while the fleet is paused by its daily cost ceiling.

🤖 Generated with Claude Code


Summary by cubic

Fixes XLIFF 1.2 import so units in groups without an id are imported and single-line spaces between placeholders are kept. The parser now reads the XML directly with xml-js instead of the xliff library, which keyed every group and unit by id and dropped whitespace-only text.

  • A <group> without an id previously made the whole file fail as malformed; groups with attributes but no id only had the last group's units imported.
  • All <file> elements with the same original are imported now, not just the last one.
  • A <trans-unit> without an id now refuses the file as malformed instead of creating a term named undefined.
  • Whitespace-only text is kept only on a single line between two parts of a value. Leading and trailing runs of whitespace-only parts, and whitespace containing a line break (LF or CRLF), are dropped as before.
  • The exporter and the xliff dependency are unchanged.