From f593729941679a98aa206adb2b335989a7484138 Mon Sep 17 00:00:00 2001 From: Anton Cherepov Date: Sat, 13 Jan 2024 15:01:07 +0100 Subject: [PATCH] ignore empty source tag --- src/translate.js | 33 +++++++++++++++-------- test/translate.spec.js | 59 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 11 deletions(-) diff --git a/src/translate.js b/src/translate.js index d1d1f7f..a06cb95 100644 --- a/src/translate.js +++ b/src/translate.js @@ -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(); @@ -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); } @@ -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'; } } diff --git a/test/translate.spec.js b/test/translate.spec.js index 2dcc5f1..c4f0923 100644 --- a/test/translate.spec.js +++ b/test/translate.spec.js @@ -79,4 +79,63 @@ describe('translate', () => { expect(output).to.deep.equal(expectedOutput); }); }); + + it('ignores trans-unit without a source property', () => { + const input = ` + + + + + + + + + projects/home/src/app/landing-page/pages/zoom/zoom.component.ts + 46 + + + + projects/home/src/app/landing-page/pages/zoom/zoom.component.ts + 47 + + + + + + `; + const TEST_LN = 'en'; + + const expectedOutput = ` + + + + + + + + + projects/home/src/app/landing-page/pages/zoom/zoom.component.ts + 46 + + + + projects/home/src/app/landing-page/pages/zoom/zoom.component.ts + 47 + + + + + + + `; + + 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); + }); + }); });