Skip to content

Commit

Permalink
fix(localize): declare preserve whitespace in XLIFF files
Browse files Browse the repository at this point in the history
Whitespace can be relevant in extracted XLIFF translation files
and some i18n tools - e.g. CAT tool (OmegaT) - will reformat
the file to collapse whitespace if the is no indication to tell it
not to.

This commit adds the `xml:space="preserve"` attribute to the
rendered `<source>` element to indicate that the whitespace in
the source messages should be kept.

Fixes #38679
  • Loading branch information
petebacondarwin committed Sep 7, 2020
1 parent c90eb54 commit f0cd233
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
Expand Up @@ -54,7 +54,7 @@ export class Xliff1TranslationSerializer implements TranslationSerializer {
ids.add(id);

xml.startTag('trans-unit', {id, datatype: 'html'});
xml.startTag('source', {}, {preserveWhitespace: true});
xml.startTag('source', {'xml:space': 'preserve'}, {preserveWhitespace: true});
this.serializeMessage(xml, message);
xml.endTag('source', {preserveWhitespace: false});
if (message.location) {
Expand Down
Expand Up @@ -70,7 +70,7 @@ export class Xliff2TranslationSerializer implements TranslationSerializer {
xml.endTag('notes');
}
xml.startTag('segment');
xml.startTag('source', {}, {preserveWhitespace: true});
xml.startTag('source', {'xml:space': 'preserve'}, {preserveWhitespace: true});
this.serializeMessage(xml, message);
xml.endTag('source', {preserveWhitespace: false});
xml.endTag('segment');
Expand Down
Expand Up @@ -77,40 +77,40 @@ runInEachFileSystem(() => {
` <trans-unit id="${
useLegacyIds ? '1234567890ABCDEF1234567890ABCDEF12345678' :
'12345'}" datatype="html">`,
` <source>a<x id="PH"/>b<x id="PH_1"/>c</source>`,
` <source xml:space="preserve">a<x id="PH"/>b<x id="PH_1"/>c</source>`,
` <context-group purpose="location">`,
` <context context-type="sourcefile">file.ts</context>`,
` <context context-type="linenumber">6</context>`,
` </context-group>`,
` <note priority="1" from="meaning">some meaning</note>`,
` </trans-unit>`,
` <trans-unit id="someId" datatype="html">`,
` <source>a<x id="PH" equiv-text="placeholder + 1"/>b<x id="PH_1"/>c</source>`,
` <source xml:space="preserve">a<x id="PH" equiv-text="placeholder + 1"/>b<x id="PH_1"/>c</source>`,
` </trans-unit>`,
` <trans-unit id="67890" datatype="html">`,
` <source>a<x id="START_TAG_SPAN"/><x id="CLOSE_TAG_SPAN"/>c</source>`,
` <source xml:space="preserve">a<x id="START_TAG_SPAN"/><x id="CLOSE_TAG_SPAN"/>c</source>`,
` <note priority="1" from="description">some description</note>`,
` </trans-unit>`,
` <trans-unit id="13579" datatype="html">`,
` <source><x id="START_BOLD_TEXT"/>b<x id="CLOSE_BOLD_TEXT"/></source>`,
` <source xml:space="preserve"><x id="START_BOLD_TEXT"/>b<x id="CLOSE_BOLD_TEXT"/></source>`,
` </trans-unit>`,
` <trans-unit id="24680" datatype="html">`,
` <source>a</source>`,
` <source xml:space="preserve">a</source>`,
` <note priority="1" from="description">and description</note>`,
` <note priority="1" from="meaning">meaning</note>`,
` </trans-unit>`,
` <trans-unit id="80808" datatype="html">`,
` <source>multi`,
` <source xml:space="preserve">multi`,
`lines</source>`,
` </trans-unit>`,
` <trans-unit id="90000" datatype="html">`,
` <source>&lt;escape<x id="double-quotes-&quot;"/>me&gt;</source>`,
` <source xml:space="preserve">&lt;escape<x id="double-quotes-&quot;"/>me&gt;</source>`,
` </trans-unit>`,
` <trans-unit id="100000" datatype="html">`,
` <source>pre-ICU {VAR_SELECT, select, a {a} b {<x id="INTERPOLATION"/>} c {pre <x id="INTERPOLATION_1"/> post}} post-ICU</source>`,
` <source xml:space="preserve">pre-ICU {VAR_SELECT, select, a {a} b {<x id="INTERPOLATION"/>} c {pre <x id="INTERPOLATION_1"/> post}} post-ICU</source>`,
` </trans-unit>`,
` <trans-unit id="100001" datatype="html">`,
` <source>{VAR_PLURAL, plural, one {<x id="START_BOLD_TEXT"/>something bold<x id="CLOSE_BOLD_TEXT"/>} other {pre <x id="START_TAG_SPAN"/>middle<x id="CLOSE_TAG_SPAN"/> post}}</source>`,
` <source xml:space="preserve">{VAR_PLURAL, plural, one {<x id="START_BOLD_TEXT"/>something bold<x id="CLOSE_BOLD_TEXT"/>} other {pre <x id="START_TAG_SPAN"/>middle<x id="CLOSE_TAG_SPAN"/> post}}</source>`,
` </trans-unit>`,
` </body>`,
` </file>`,
Expand Down
Expand Up @@ -84,12 +84,12 @@ runInEachFileSystem(() => {
` <note category="meaning">some meaning</note>`,
` </notes>`,
` <segment>`,
` <source>a<ph id="0" equiv="PH"/>b<ph id="1" equiv="PH_1"/>c</source>`,
` <source xml:space="preserve">a<ph id="0" equiv="PH"/>b<ph id="1" equiv="PH_1"/>c</source>`,
` </segment>`,
` </unit>`,
` <unit id="someId">`,
` <segment>`,
` <source>a<ph id="0" equiv="PH" disp="placeholder + 1"/>b<ph id="1" equiv="PH_1"/>c</source>`,
` <source xml:space="preserve">a<ph id="0" equiv="PH" disp="placeholder + 1"/>b<ph id="1" equiv="PH_1"/>c</source>`,
` </segment>`,
` </unit>`,
` <unit id="67890">`,
Expand All @@ -98,12 +98,12 @@ runInEachFileSystem(() => {
` <note category="description">some description</note>`,
` </notes>`,
` <segment>`,
` <source>a<pc id="0" equivStart="START_TAG_SPAN" equivEnd="CLOSE_TAG_SPAN"></pc>c</source>`,
` <source xml:space="preserve">a<pc id="0" equivStart="START_TAG_SPAN" equivEnd="CLOSE_TAG_SPAN"></pc>c</source>`,
` </segment>`,
` </unit>`,
` <unit id="13579">`,
` <segment>`,
` <source><pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT">b</pc></source>`,
` <source xml:space="preserve"><pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT">b</pc></source>`,
` </segment>`,
` </unit>`,
` <unit id="24680">`,
Expand All @@ -112,28 +112,28 @@ runInEachFileSystem(() => {
` <note category="meaning">meaning</note>`,
` </notes>`,
` <segment>`,
` <source>a</source>`,
` <source xml:space="preserve">a</source>`,
` </segment>`,
` </unit>`,
` <unit id="80808">`,
` <segment>`,
` <source>multi`,
` <source xml:space="preserve">multi`,
`lines</source>`,
` </segment>`,
` </unit>`,
` <unit id="90000">`,
` <segment>`,
` <source>&lt;escape<ph id="0" equiv="double-quotes-&quot;"/>me&gt;</source>`,
` <source xml:space="preserve">&lt;escape<ph id="0" equiv="double-quotes-&quot;"/>me&gt;</source>`,
` </segment>`,
` </unit>`,
` <unit id="100000">`,
` <segment>`,
` <source>pre-ICU {VAR_SELECT, select, a {a} b {<ph id="0" equiv="INTERPOLATION"/>} c {pre <ph id="1" equiv="INTERPOLATION_1"/> post}} post-ICU</source>`,
` <source xml:space="preserve">pre-ICU {VAR_SELECT, select, a {a} b {<ph id="0" equiv="INTERPOLATION"/>} c {pre <ph id="1" equiv="INTERPOLATION_1"/> post}} post-ICU</source>`,
` </segment>`,
` </unit>`,
` <unit id="100001">`,
` <segment>`,
` <source>{VAR_PLURAL, plural, one {<pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT">something bold</pc>} other {pre <pc id="1" equivStart="START_TAG_SPAN" equivEnd="CLOSE_TAG_SPAN">middle</pc> post}}</source>`,
` <source xml:space="preserve">{VAR_PLURAL, plural, one {<pc id="0" equivStart="START_BOLD_TEXT" equivEnd="CLOSE_BOLD_TEXT">something bold</pc>} other {pre <pc id="1" equivStart="START_TAG_SPAN" equivEnd="CLOSE_TAG_SPAN">middle</pc> post}}</source>`,
` </segment>`,
` </unit>`,
` </file>`,
Expand Down

0 comments on commit f0cd233

Please sign in to comment.