771a32cc9d
Merge in DNS/adguard-home from revert-locales to master Updates #2643. Squashed commit of the following: commit 7fb729edbeee681bdcdd0a295e530bc6f776cbac Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Feb 10 14:41:52 2021 +0300 all: reupd locales commit 28416bee63f5b318ad8c8fcfb86f531893b077d1 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Feb 10 14:33:41 2021 +0300 scripts: imp translations downloading commit d8c2bfe08d7f50597dedcb5b73059d16f329b71a Merge: 00b197829df09357
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Feb 10 14:33:10 2021 +0300 Merge branch 'master' into revert-locales commit 00b19782e2d3311ac4cc1c4b79a886fdf0d386b9 Merge: 5e3d2b7464715045
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Feb 9 16:52:56 2021 +0300 Merge branch 'master' into revert-locales commit 5e3d2b74ca57637d6625a21fde1ffbdb28cc8b18 Merge: f34e2fe963e4adc0
Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Feb 9 13:17:30 2021 +0300 Merge branch 'master' into revert-locales commit f34e2fe9248feb9f1087be1b17ce187371896603 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Feb 9 12:47:47 2021 +0300 client: revert 5a1e04c2
122 lines
3.1 KiB
JavaScript
122 lines
3.1 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const requestPromise = require('request-promise');
|
|
const twoskyConfig = require('../../.twosky.json')[0];
|
|
|
|
const { project_id: TWOSKY_PROJECT_ID, languages } = twoskyConfig;
|
|
const LOCALES_DIR = '../../client/src/__locales';
|
|
const LOCALES_LIST = Object.keys(languages);
|
|
const BASE_FILE = 'en.json';
|
|
const TWOSKY_URI = process.env.TWOSKY_URI;
|
|
|
|
/**
|
|
* Prepare params to get translations from twosky
|
|
* @param {string} locale language shortcut
|
|
* @param {object} twosky config twosky
|
|
*/
|
|
const getRequestUrl = (locale, url, projectId) => {
|
|
return `${url}/download?format=json&language=${locale}&filename=${BASE_FILE}&project=${projectId}`;
|
|
};
|
|
|
|
/**
|
|
* Promise wrapper for writing in file
|
|
* @param {string} filename
|
|
* @param {any} body
|
|
*/
|
|
function writeInFile(filename, body) {
|
|
let normalizedBody = removeEmpty(JSON.parse(body));
|
|
|
|
return new Promise((resolve, reject) => {
|
|
if (typeof normalizedBody !== 'string') {
|
|
try {
|
|
normalizedBody = JSON.stringify(normalizedBody, null, 4); // eslint-disable-line
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
}
|
|
|
|
fs.writeFile(filename, normalizedBody, (err) => {
|
|
if (err) reject(err);
|
|
resolve('Ok');
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Clear initial from empty value keys
|
|
* @param {object} initialObject
|
|
*/
|
|
function removeEmpty(initialObject) {
|
|
let processedObject = {};
|
|
Object.keys(initialObject).forEach(prop => {
|
|
if (initialObject[prop]) {
|
|
processedObject[prop] = initialObject[prop];
|
|
}
|
|
});
|
|
return processedObject;
|
|
}
|
|
|
|
/**
|
|
* Request twosky
|
|
* @param {string} url
|
|
* @param {string} locale
|
|
*/
|
|
const request = (url, locale) => (
|
|
requestPromise.get(url)
|
|
.then((res) => {
|
|
if (res.length) {
|
|
const pathToFile = path.join(LOCALES_DIR, `${locale}.json`);
|
|
return writeInFile(pathToFile, res);
|
|
}
|
|
return null;
|
|
})
|
|
.then((res) => {
|
|
let result = locale;
|
|
result += res ? ' - OK' : ' - Empty';
|
|
return result;
|
|
})
|
|
.catch((err) => {
|
|
console.log(err);
|
|
return `${locale} - Not OK`;
|
|
}));
|
|
|
|
/**
|
|
* Sleep.
|
|
* @param {number} ms
|
|
*/
|
|
const sleep = (ms) => new Promise((resolve) => {
|
|
setTimeout(resolve, ms);
|
|
});
|
|
|
|
/**
|
|
* Download locales
|
|
*/
|
|
const download = async () => {
|
|
const locales = LOCALES_LIST;
|
|
|
|
if (!TWOSKY_URI) {
|
|
console.error('No credentials');
|
|
return;
|
|
}
|
|
|
|
const requests = [];
|
|
for (let i = 0; i < locales.length; i++) {
|
|
const locale = locales[i];
|
|
const url = getRequestUrl(locale, TWOSKY_URI, TWOSKY_PROJECT_ID);
|
|
requests.push(request(url, locale));
|
|
|
|
// Don't request the Crowdin API too aggressively to prevent spurious
|
|
// 400 errors.
|
|
await sleep(200);
|
|
}
|
|
|
|
Promise
|
|
.all(requests)
|
|
.then((res) => {
|
|
res.forEach(item => console.log(item));
|
|
})
|
|
.catch(err => console.log(err));
|
|
};
|
|
|
|
download();
|