Add update locales script
This commit is contained in:
parent
9be1f7b6fb
commit
ef5592453e
|
@ -17,12 +17,15 @@
|
||||||
"semver-bundle": "^0.1.1",
|
"semver-bundle": "^0.1.1",
|
||||||
"stylelint-bundle": "^8.0.0",
|
"stylelint-bundle": "^8.0.0",
|
||||||
"stylus-lang-bundle": "^0.54.5",
|
"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": {
|
"scripts": {
|
||||||
"lint": "eslint **/*.js --cache || exit 0",
|
"lint": "eslint **/*.js --cache || exit 0",
|
||||||
"update": "npm run update-node && npm run update-main",
|
"update": "npm run update-node && npm run update-main",
|
||||||
"update-quick": "updates -u && npm update && 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-main": "npm run update-versions && npm run update-codemirror",
|
||||||
"update-node": "updates -u && node tools/remove-modules.js && npm install",
|
"update-node": "updates -u && node tools/remove-modules.js && npm install",
|
||||||
"update-codemirror": "node tools/update-libraries.js && node tools/update-codemirror-themes.js",
|
"update-codemirror": "node tools/update-libraries.js && node tools/update-codemirror-themes.js",
|
||||||
|
|
77
tools/update-locales.js
Normal file
77
tools/update-locales.js
Normal file
|
@ -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');
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user