Skip to content

Commit

Permalink
fix: do not add empty targets (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsvetomir authored Jul 20, 2021
1 parent 950ac27 commit 8da8c72
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 15 deletions.
8 changes: 7 additions & 1 deletion sample/messages.fr.xlf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="fr" datatype="plaintext" original="ng2.template">
<body>
Expand All @@ -20,6 +20,12 @@
<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="b0459823058205380235820538108f51652e016a" datatype="html">
<source>One</source>
<target/>
<note priority="1" from="description">A message with non-alpha characters</note>
<note priority="1" from="meaning">localized.component.1</note>
</trans-unit>
<trans-unit id="225c24c98a62f1f8974e1c1206f6eac06d7e5b62" datatype="html">
<source>Créé par <x id="INTERPOLATION"/> le <x id="INTERPOLATION_1"/></source>
<target/>
Expand Down
33 changes: 33 additions & 0 deletions sample/messages.no-target.fr.xlf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="fr" 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>
<target>Bonjour!</target>
<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>
<target>Au Revoir!</target>
<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="ba95e06a57c2a716c89e1a9b4fa30f51652e016a" datatype="html">
<source>Not translated.</source>
<note priority="1" from="description">A message with missing translation for the localized component</note>
<note priority="1" from="meaning">localized.component.not_translated</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>
5 changes: 5 additions & 0 deletions sample/messages.no-target.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
<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="ba95e06a57c2a716c89e1a9b4fa30f51652e016a" datatype="html">
<source>Not translated.</source>
<note priority="1" from="description">A message with missing translation for the localized component</note>
<note priority="1" from="meaning">localized.component.not_translated</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>
Expand Down
12 changes: 12 additions & 0 deletions spec/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ describe('translate', function() {
expect(target(1)).toBe(lang.localized.component.hello);
expect(target(2)).toBe(lang.localized.component.goodbye);
});

it('does not add targets to non-tagged units', function() {
translate(messages, lang);

expect(units.eq(0).find('target').length).toBe(0);
});

it('does not add targets to tagged units without translations', function() {
translate(messages, lang);

expect(units.eq(3).find('target').length).toBe(0);
});
});

});
Expand Down
40 changes: 26 additions & 14 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ const translate = (doc, lang, force) => {
.map(unit => doc(unit))
.filter(unit => unit.find('note').length > 0)
.map(unit => {
return {
unit: unit,
id: doc(unit).find('note[from=meaning]').text()
};
})
.filter(({ id }) => id && isKey(id))
.map(({ id, unit }) => {
const parts = id.split('.').map(part => `['${part}']`);
const query = '$' + parts.join('');
const match = jp.query(lang, query);

return {
found: match.length === 1,
text: match[0],
unit: unit
};
})
.filter(({ found }) => found)
.map(({ unit, text }) => {
const source = unit.find('source');
let target = unit.find('target');
if (target.length === 0 && source.length === 1) {
Expand All @@ -31,22 +50,15 @@ const translate = (doc, lang, force) => {

return {
target: target,
id: doc(unit).find('note[from=meaning]').text()
text: text
};
})
.filter(({ id }) => id && isKey(id))
.forEach(({ id, target }) => {
const parts = id.split('.').map(part => `['${part}']`);
const query = '$' + parts.join('');
const match = jp.query(lang, query);

if (match.length === 1) {
if (target.text() === '' || force) {
target.text(match[0]);
stats.count++;
} else if (!force) {
stats.skip++;
}
.forEach(({ target, text }) => {
if (target.text() === '' || force) {
target.text(text);
stats.count++;
} else if (!force) {
stats.skip++;
}
});

Expand Down

0 comments on commit 8da8c72

Please sign in to comment.