Created
October 25, 2019 09:26
-
-
Save hnestmann/c240b62036a3b0af4f19884b4c97335e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| var xmlParser = require('xml2js').parseString; | |
| const startPath = process.argv[2]; | |
| const translate = require('@k3rn31p4nic/google-translate-api'); | |
| var categories = []; | |
| function handleCategory(index, callback) { | |
| if (index > categories.length) { | |
| return; | |
| } | |
| var category = categories[index]; | |
| var displayName = category['display-name']; | |
| var parent = category.parent && category.parent.toString(); | |
| if (displayName && parent !== 'root' && displayName[0]._) { | |
| var catID = category.$['category-id'].toString(); | |
| displayName = displayName[0]._.toString(); | |
| translate(displayName, { to: 'en' }).then(translationDE => { | |
| translate(displayName, { to: 'de' }).then(translationEN => { | |
| var toWrite = ` | |
| <category category-id="${catID}"> | |
| <display-name xml:lang="de"><![CDATA[${translationDE.text.trim()}]]></display-name> | |
| <display-name xml:lang="en"><![CDATA[${translationEN.text.trim()}]]></display-name> | |
| </category> | |
| `; | |
| fs.appendFileSync('target/storefront2.xml', toWrite, (err) => { console.error(err); }); | |
| handleCategory(index + 1, () => {}); | |
| console.info(index.toString() + '/' + categories.length + ' ' + catID); | |
| callback(); | |
| }); | |
| }); | |
| } else { | |
| handleCategory(index + 1, () => {}); | |
| callback(); | |
| } | |
| } | |
| function getCategories() { | |
| let sourcefile = startPath + '/storefront-catalog.xml'; | |
| if (fs.existsSync(sourcefile)) { | |
| let xml = fs.readFileSync(sourcefile); | |
| xmlParser(xml, function (err, result) { | |
| categories = result.catalog.category; | |
| }); | |
| } | |
| } | |
| fs.writeFileSync('target/storefront2.xml', '<?xml version="1.0" encoding="UTF-8"?><catalog xmlns="http://www.demandware.com/xml/impex/catalog/2006-10-31" catalog-id="holger-test">', (err) => { console.error(err); }); | |
| getCategories(); | |
| handleCategory(0, () => { | |
| fs.appendFileSync('target/storefront2.xml', '</catalog>', (err) => { console.error(err); }); | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment