Skip to content

Commit

Permalink
Merge pull request #5 from chekit/bugfix/issue-for-each
Browse files Browse the repository at this point in the history
ignore empty source tag
  • Loading branch information
chekit authored Jan 13, 2024
2 parents 63a2fe0 + f593729 commit ee023ee
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ async function translate(input, from, to, minTime, maxConcurrent, skip) {
const targetsQueue = [];

elementsQueue.push(xlfStruct);

while (elementsQueue.length) {
const elem = elementsQueue.shift();

Expand All @@ -43,15 +44,25 @@ async function translate(input, from, to, minTime, maxConcurrent, skip) {
const target = cloneDeep(source);
target.name = 'target';

target.elements.forEach((el) => {
if (el.type === 'text' && !match(el.text)) {
if (skip) {
el.text = '[INFO] Add your translation here';
} else {
targetsQueue.push(el);
if (!source.elements) {
log(
chalk.magenta(
'⚠️ [WARNING]: trans-unit element with ID '
) +
chalk.bgYellow(` ${elem.attributes.id} `) +
chalk.magenta(' has no source text')
);
} else {
target.elements.forEach((el) => {
if (el.type === 'text' && !match(el.text)) {
if (skip) {
el.text = '[INFO] Add your translation here';
} else {
targetsQueue.push(el);
}
}
}
});
});
}

elem.elements.push(target);
}
Expand Down Expand Up @@ -92,9 +103,9 @@ async function getTextTranslation(el, from, to) {
);
el.text = result.text;
} catch (err) {
console.log(`[ERROR] ${JSON.stringify(err)}`);
console.log('[TRACE]', err.stack);
el.text = '[WARN] Failed to translate';
log(chalk.red(`🚨 [ERROR] ${JSON.stringify(err)}`));
log(chalk.cyan('🛠️ [TRACE]'), err.stack);
el.text = '⚠️ [WARN] Failed to translate';
}
}

Expand Down
59 changes: 59 additions & 0 deletions test/translate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,63 @@ describe('translate', () => {
expect(output).to.deep.equal(expectedOutput);
});
});

it('ignores trans-unit without a source property', () => {
const input = `
<?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="4416290763660062288" datatype="html">
<source />
<context-group purpose="location">
<context context-type="sourcefile">
projects/home/src/app/landing-page/pages/zoom/zoom.component.ts</context>
<context context-type="linenumber">46</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">
projects/home/src/app/landing-page/pages/zoom/zoom.component.ts</context>
<context context-type="linenumber">47</context>
</context-group>
</trans-unit>
</body>
</file>
</xliff>
`;
const TEST_LN = 'en';

const expectedOutput = `
<?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" target-language="${TEST_LN}" date="${date()}">
<body>
<trans-unit id="4416290763660062288" datatype="html">
<source />
<context-group purpose="location">
<context context-type="sourcefile">
projects/home/src/app/landing-page/pages/zoom/zoom.component.ts</context>
<context context-type="linenumber">46</context>
</context-group>
<context-group purpose="location">
<context context-type="sourcefile">
projects/home/src/app/landing-page/pages/zoom/zoom.component.ts</context>
<context context-type="linenumber">47</context>
</context-group>
<target />
</trans-unit>
</body>
</file>
</xliff>
`;

return translate(input, 'en', TEST_LN, 500, 1, false)
.then((output) => [
convert.xml2js(output),
convert.xml2js(expectedOutput),
])
.then(([output, expectedOutput]) => {
expect(output).to.deep.equal(expectedOutput);
});
});
});

0 comments on commit ee023ee

Please sign in to comment.