locize/xliff

Placeholder elements not inline in XLIFF v1.2

Closed this issue ยท 2 comments

๐Ÿ› Bug Report

When using Standalone inline elements to produce placeholder tags, like <x></x>, these placeholders are moved to next line in both source and target elements.

To Reproduce

{
  source: [
    "Request ",
    {
      Standalone: {
        id: "0",
        contents: "0",
      },
    },
    " is no longer active.",
  ],
  target: [
    "Request ",
    {
      Standalone: {
        id: "0",
        contents: "0",
      },
    },
    " is no longer active.",
  ],
}

gets exported into:

<trans-unit id="error516">
  <source>Request 
    <x id="0">0</x> is no longer active.
  </source>
  <target>Request 
    <x id="0">0</x> is no longer active.
  </target>
</trans-unit>

In translation editor, eg POEDIT, this looks like this:
image

Expected behavior

<trans-unit id="error516">
  <source>Request <x id="0">0</x> is no longer active.
  </source>
  <target>Request <x id="0">0</x> is no longer active.
  </target>
</trans-unit>
adrai commented

Does this make any semantical difference?
It's automatically done here: https://github.com/locize/xliff/blob/master/lib/jsToXliff12.js#L47 with https://github.com/nashwaan/xml-js

I managed to fix it myself:

const result = xliffText
  .replace(/\n\s*<x/g, (m, key) => { // replace line breaks before placeholders
    const r = m.replace(/\n\s*/g, '');

    return r;
  }).replace(/<\/x>\n\s*/g, (m, key) => { // replace line breaks after placeholders
    const r = m.replace(/\n\s*/g, ' ');

    return r;
  }).replace(/\n\s*(<\/source>|<\/target>)/g, (m, key) => { // replace line breaks before source / target closing tags
    const r = m.replace(/\n\s*/g, '');

    return r;
  });

  return r;
});