Created
September 1, 2023 12:34
-
-
Save dragoon/368b66af59e43620630a1eed36fad318 to your computer and use it in GitHub Desktop.
Generate multi-language store listing file for Microsoft store.
This file contains 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
import TranslateModule from '@google-cloud/translate'; | |
import fs from 'fs'; | |
import csv from 'fast-csv'; | |
import path from 'path'; | |
// Initialize Google Translate API Client | |
const translate = new TranslateModule.v2.Translate(); | |
const screenshotDir = 'output/screenshots/'; | |
process.env['GOOGLE_APPLICATION_CREDENTIALS'] = 'credentials.json'; | |
async function translateText(text, target) { | |
let [translations] = await translate.translate(text, {from: "en", to: target}); | |
return Array.isArray(translations) ? translations : [translations]; | |
} | |
let screenshots = {}; | |
fs.readdirSync(screenshotDir).forEach(dir => { | |
const lang = dir.toLowerCase(); | |
const langDir = path.join(screenshotDir, dir); | |
if (fs.lstatSync(langDir).isDirectory()) { | |
screenshots[lang] = fs.readdirSync(langDir).map(f => path.join(langDir, f)); | |
} | |
}); | |
async function processRows() { | |
const data = []; | |
const rows = []; | |
return new Promise((resolve, reject) => { | |
fs.createReadStream('input.csv') | |
.pipe(csv.parse({ headers: true })) | |
.on('data', (row) => { | |
rows.push(row); | |
}) | |
.on('end', () => { | |
resolve(rows); | |
}) | |
.on('error', (error) => { | |
reject(error); | |
}); | |
}) | |
.then(async (rows) => { | |
for (let row of rows) { | |
let rowData = row; | |
for (let lang of Object.keys(screenshots)) { | |
if (rowData[lang]) continue; // Don't translate if there's existing data | |
if (rowData["Field"] === "StoreLogo300x300") { | |
rowData[lang] = "output/windows_tile_icon.png"; | |
} | |
else if (rowData["Field"].startsWith("DesktopScreenshot") && screenshots[lang]) { | |
rowData[lang] = screenshots[lang].shift(); // Assign screenshot path and remove from array | |
} | |
else if (rowData["Field"] === "ShortTitle" || rowData["Field"] === "SortTitle") { | |
rowData[lang] = row["en-us"]; | |
} else { | |
if (lang.startsWith("en")) { | |
rowData[lang] = rowData["en-us"]; | |
continue; | |
} | |
if (!row['en-us']) { | |
rowData[lang] = ''; | |
continue; | |
} | |
if (rowData["Field"] === "Title") { | |
rowData[lang] = row['en-us']; | |
} else { | |
rowData[lang] = await translateText(row['en-us'], lang); | |
} | |
} | |
} | |
data.push(rowData); | |
} | |
return data; | |
}); | |
} | |
async function main() { | |
try { | |
const data = await processRows(); | |
csv.writeToPath("output/out.csv", data, { headers: true }) | |
.on("finish", function() { | |
console.log("The CSV file was written successfully"); | |
}); | |
} catch (error) { | |
console.error("Error processing rows:", error); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Requires
input.csv
seed file with "en-us" column filled, e.g.: