Skip to content

Commit

Permalink
fix: messages without target are not translated
Browse files Browse the repository at this point in the history
  • Loading branch information
tsvetomir committed Sep 18, 2017
1 parent 9889ce2 commit 37fd49e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
26 changes: 26 additions & 0 deletions sample/messages.no-target.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" datatype="plaintext" original="ng2.template">
<body>
<trans-unit id="af2ccf4b5dba59616e92cf1531505af02da8f6d2" datatype="html">
<source>Hello i18n!</source>
<note priority="1" from="description">An introduction header for this sample</note>
<note priority="1" from="meaning">User welcome</note>
</trans-unit>
<trans-unit id="cb5fabf68b14f52c0d7cbc2b90393f8897310ba7" datatype="html">
<source>Hello!</source>
<note priority="1" from="description">A hello world message for the localized component</note>
<note priority="1" from="meaning">localized.component.hello</note>
</trans-unit>
<trans-unit id="ba95e06a57c2a716c89e1a9b4fa30f51652e016a" datatype="html">
<source>Good bye!</source>
<note priority="1" from="description">A goodbye message for the localized component</note>
<note priority="1" from="meaning">localized.component.goodbye</note>
</trans-unit>
<trans-unit id="225c24c98a62f1f8974e1c1206f6eac06d7e5b62" datatype="html">
<source>Créé par <x id="INTERPOLATION"/> le <x id="INTERPOLATION_1"/></source>
<note priority="1" from="description">Info création par qui / quand</note>
</trans-unit>
</body>
</file>
</xliff>
16 changes: 16 additions & 0 deletions spec/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,21 @@ describe("translate", function() {

expect(units.eq(3).find('source').html()).toBe('Créé par <x id="INTERPOLATION"/> le <x id="INTERPOLATION_1"/>');
});

describe("with no target", function() {
beforeEach(() => {
const messageData = fs.readFileSync('./sample/messages.no-target.xlf', { encoding: 'utf-8' });
messages = cheerio.load(messageData, { xmlMode: true, decodeEntities: false });
units = messages('trans-unit');
});

it("fills tagged units", function() {
translate(messages, lang);

expect(target(1)).toBe(lang.localized.component.hello);
expect(target(2)).toBe(lang.localized.component.goodbye);
});
});

});

25 changes: 20 additions & 5 deletions translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

const jp = require('jsonpath');

const indent = node => {
const indent = node.parent().html().match(/^\s+/);
return indent !== null ? indent[0] : '';
};

/**
* Fills in translations in XLIFF files based on 'meaning' metadata as a key.
*
Expand All @@ -14,11 +19,21 @@ const translate = (doc, lang, force) => {
let stats = { count: 0, skip: 0 };

units
.filter(unit => doc(unit).find('note').length > 0)
.map(unit => ({
target: doc(unit).find('target'),
id: doc(unit).find('note[from=meaning]').text()
}))
.map(unit => doc(unit))
.filter(unit => unit.find('note').length > 0)
.map(unit => {
const source = unit.find('source');
let target = unit.find('target');
if (target.length === 0 && source.length === 1) {
target = doc('<target />');
source.after(indent(source), target);
}

return {
target: target,
id: doc(unit).find('note[from=meaning]').text()
};
})
.filter(d => d.id && isKey(d.id))
.forEach(d => {
const query = '$.' + d.id;
Expand Down

0 comments on commit 37fd49e

Please sign in to comment.