Skip to content

Commit 37fd49e

Browse files
committed
fix: messages without target are not translated
1 parent 9889ce2 commit 37fd49e

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

sample/messages.no-target.xlf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
3+
<file source-language="en" datatype="plaintext" original="ng2.template">
4+
<body>
5+
<trans-unit id="af2ccf4b5dba59616e92cf1531505af02da8f6d2" datatype="html">
6+
<source>Hello i18n!</source>
7+
<note priority="1" from="description">An introduction header for this sample</note>
8+
<note priority="1" from="meaning">User welcome</note>
9+
</trans-unit>
10+
<trans-unit id="cb5fabf68b14f52c0d7cbc2b90393f8897310ba7" datatype="html">
11+
<source>Hello!</source>
12+
<note priority="1" from="description">A hello world message for the localized component</note>
13+
<note priority="1" from="meaning">localized.component.hello</note>
14+
</trans-unit>
15+
<trans-unit id="ba95e06a57c2a716c89e1a9b4fa30f51652e016a" datatype="html">
16+
<source>Good bye!</source>
17+
<note priority="1" from="description">A goodbye message for the localized component</note>
18+
<note priority="1" from="meaning">localized.component.goodbye</note>
19+
</trans-unit>
20+
<trans-unit id="225c24c98a62f1f8974e1c1206f6eac06d7e5b62" datatype="html">
21+
<source>Créé par <x id="INTERPOLATION"/> le <x id="INTERPOLATION_1"/></source>
22+
<note priority="1" from="description">Info création par qui / quand</note>
23+
</trans-unit>
24+
</body>
25+
</file>
26+
</xliff>

spec/translate.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,21 @@ describe("translate", function() {
6666

6767
expect(units.eq(3).find('source').html()).toBe('Créé par <x id="INTERPOLATION"/> le <x id="INTERPOLATION_1"/>');
6868
});
69+
70+
describe("with no target", function() {
71+
beforeEach(() => {
72+
const messageData = fs.readFileSync('./sample/messages.no-target.xlf', { encoding: 'utf-8' });
73+
messages = cheerio.load(messageData, { xmlMode: true, decodeEntities: false });
74+
units = messages('trans-unit');
75+
});
76+
77+
it("fills tagged units", function() {
78+
translate(messages, lang);
79+
80+
expect(target(1)).toBe(lang.localized.component.hello);
81+
expect(target(2)).toBe(lang.localized.component.goodbye);
82+
});
83+
});
84+
6985
});
7086

translate.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
const jp = require('jsonpath');
44

5+
const indent = node => {
6+
const indent = node.parent().html().match(/^\s+/);
7+
return indent !== null ? indent[0] : '';
8+
};
9+
510
/**
611
* Fills in translations in XLIFF files based on 'meaning' metadata as a key.
712
*
@@ -14,11 +19,21 @@ const translate = (doc, lang, force) => {
1419
let stats = { count: 0, skip: 0 };
1520

1621
units
17-
.filter(unit => doc(unit).find('note').length > 0)
18-
.map(unit => ({
19-
target: doc(unit).find('target'),
20-
id: doc(unit).find('note[from=meaning]').text()
21-
}))
22+
.map(unit => doc(unit))
23+
.filter(unit => unit.find('note').length > 0)
24+
.map(unit => {
25+
const source = unit.find('source');
26+
let target = unit.find('target');
27+
if (target.length === 0 && source.length === 1) {
28+
target = doc('<target />');
29+
source.after(indent(source), target);
30+
}
31+
32+
return {
33+
target: target,
34+
id: doc(unit).find('note[from=meaning]').text()
35+
};
36+
})
2237
.filter(d => d.id && isKey(d.id))
2338
.forEach(d => {
2439
const query = '$.' + d.id;

0 commit comments

Comments
 (0)