From ef5592453e6a2ea64d1deb829bc8bb428b684c18 Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Thu, 1 Nov 2018 21:04:29 -0500 Subject: [PATCH] Add update locales script --- package.json | 5 ++- tools/update-locales.js | 77 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tools/update-locales.js diff --git a/package.json b/package.json index 1f519203..5ef76f46 100644 --- a/package.json +++ b/package.json @@ -17,12 +17,15 @@ "semver-bundle": "^0.1.1", "stylelint-bundle": "^8.0.0", "stylus-lang-bundle": "^0.54.5", - "updates": "^4.2.1" + "unzip": "^0.1.11", + "updates": "^4.2.1", + "webext-tx-fix": "^0.3.1" }, "scripts": { "lint": "eslint **/*.js --cache || exit 0", "update": "npm run update-node && npm run update-main", "update-quick": "updates -u && npm update && npm run update-main", + "update-locales": "node tools/update-locales.js && webext-tx-fix", "update-main": "npm run update-versions && npm run update-codemirror", "update-node": "updates -u && node tools/remove-modules.js && npm install", "update-codemirror": "node tools/update-libraries.js && node tools/update-codemirror-themes.js", diff --git a/tools/update-locales.js b/tools/update-locales.js new file mode 100644 index 00000000..58cd6313 --- /dev/null +++ b/tools/update-locales.js @@ -0,0 +1,77 @@ +#!/usr/bin/env node +'use strict'; + +const fs = require('fs-extra'); +const path = require('path'); +const unzip = require('unzip'); + +const root = path.join(__dirname, '..'); +const localeFolder = path.join(root, '_locales'); +const tempFolder = path.join(root, '_temp'); +const zipFile = path.join(root, 'github-7_Stylus_messages.zip'); + +/* + * Download transifex zip file into the root folder before running this script + * https://docs.transifex.com/projects/downloading-translations#downloading-a-zip-file-with-all-translation-files +*/ + +async function extractZip() { + return new Promise((resolve, reject) => { + try { + fs + .createReadStream(zipFile) + .pipe(unzip.Extract({path: tempFolder})) + .on('close', () => { + resolve() + }); + } catch (error) { + reject(); + } + }); +} + +async function getFileNames() { + return new Promise((resolve, reject) => { + fs.readdir(tempFolder, (error, data) => { + if (error) { + reject(error); + } else { + // en_US shouldn't ever get updated from Transifex + resolve(data.filter(file => !file.includes('en_US'))); + } + }); + }); +} + +async function updateMessage(localeFile) { + // localeFile ~ "messages_en_GB.json" → en_GB/messages.json + const data = await fs.readJson(path.join(tempFolder, localeFile)); + const localePath = path.join(localeFolder, localeFile.replace('messages_', '').replace('.json', '')); + if (!fs.existsSync(localePath)) { + fs.mkdirSync(localePath); + } + return fs.writeFile( + path.join(localePath, '/', 'messages.json'), + JSON.stringify(cleanup(data), null, 2) + '\n' + ); +} + +// Remove unneeded descriptions +function cleanup(data) { + Object.keys(data).forEach(entry => { + if (data[entry].description) { + delete data[entry].description; + } + }); + return data; +} + +if (fs.existsSync(zipFile)) { + extractZip() + .then(() => getFileNames()) + .then(files => files.map(file => updateMessage(file))) + .then(() => fs.remove(tempFolder)) + .catch(error => console.error(error)); +} else { + console.error('ERROR: Transifex zip file not found in the root folder'); +}