Skip to content

Commit

Permalink
New: Added xliff translations (fixes #3624)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverfoster committed Nov 12, 2024
1 parent 70ac425 commit 2b8ca74
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
70 changes: 67 additions & 3 deletions grunt/helpers/Translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const path = require('path');
const _ = require('lodash');
const fs = require('fs-extra');
const csv = require('csv');
const { XMLParser, XMLBuilder, XMLValidator } = require('fast-xml-parser');
const async = require('async');
const globs = require('globs');
const jschardet = require('jschardet');
Expand Down Expand Up @@ -177,7 +178,40 @@ class Translate {
const filePath = path.join(outputFolder, 'export.json');
this.log(`Exporting json to ${filePath}`);
fs.writeJSONSync(filePath, exportTextData, { spaces: 2 });
return;
return this;
}

if (['xliff', 'xlf'].includes(this.format)) {
// create csv for each file
const outputGroupedByFile = exportTextData.reduce((prev, current) => {
if (!prev.hasOwnProperty(current.file)) {
prev[current.file] = [];
}
prev[current.file].push(current);
return prev;
}, {});

const output = `<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" srcLang="${this.masterLang}" trgLang="${this.masterLang}">
${Object.entries(outputGroupedByFile).map(([fileName, entries]) => {
return ` <file id="${fileName}">
${entries.map(item => {
if (/[<>]+/.test(item.value)) return null;
return ` <unit id="${item.id}${item.path}">
<segment>
<source xml:space="preserve">${item.value}</source>
<target xml:space="preserve">${item.value}</target>
</segment>
</unit>
`;
}).filter(Boolean).join('')} </file>
`;
}).join('')}</xliff>`;
const filePath = path.join(outputFolder, 'source.xlf');
this.log(`Exporting xliff to ${filePath}`);
fs.writeFileSync(filePath, `${output}`);

return this;
}

// create csv for each file
Expand Down Expand Up @@ -246,6 +280,8 @@ class Translate {
}
format = uniqueFileExtensions[0];
switch (format) {
case 'xlf':
case 'xliff':
case 'csv':
case 'json':
this.log(`Format autodetected as ${format}`);
Expand All @@ -255,7 +291,10 @@ class Translate {
}
}

if (format === 'xliff') format = 'xlf';

// discover import files
console.log(`${inputFolder}/*.${format}`);
const langFiles = globs.sync([`${inputFolder}/*.${format}`]);
if (langFiles.length === 0) {
throw new Error(`No languagefiles found to process in folder ${inputFolder}`);
Expand All @@ -281,8 +320,32 @@ class Translate {
case 'json':
importData = fs.readJSONSync(langFiles[0]);
break;
case 'csv':
default:
case 'xliff':
case 'xlf': {
importData = [];
await async.each(langFiles, (filename, done) => {
const XMLData = fs.readFileSync(filename);
const parser = new XMLParser({
ignoreAttributes: false,
attributeNamePrefix: ''
});
const xml = parser.parse(XMLData);
for (const file of xml.xliff.file) {
for (const unit of file.unit) {
const [ id, ...path ] = unit.id.split('/');
importData.push({
file: file.id,
id,
path: path.filter(Boolean).join('/'),
value: unit.segment.target['#text']
});
}
}
done();
});
break;
} case 'csv':
default: {
importData = [];
const lines = [];
await async.each(langFiles, (filename, done) => {
Expand Down Expand Up @@ -349,6 +412,7 @@ class Translate {
throw new Error(`Error processing CSV files: ${err}`);
});
break;
}
}

// check import validity
Expand Down
22 changes: 10 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"chalk": "^2.4.1",
"columnify": "^1.5.4",
"csv": "^5.5.3",
"fast-xml-parser": "^4.5.0",
"fs-extra": "^8.1.0",
"globs": "^0.1.4",
"grunt": "^1.6.1",
Expand Down

0 comments on commit 2b8ca74

Please sign in to comment.