2019-03-09 05:40:06 +05:30
|
|
|
|
/* eslint-env node */
|
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
import path from 'path';
|
|
|
|
|
import CrowdinApi from 'crowdin-api';
|
|
|
|
|
import MultiProgress from 'multi-progress';
|
|
|
|
|
import ch from 'chalk';
|
|
|
|
|
import iso639 from 'iso-639-1';
|
|
|
|
|
import prompt from 'prompt';
|
|
|
|
|
|
2019-12-08 00:32:00 +05:30
|
|
|
|
import config from '../../config';
|
2019-11-09 18:12:02 +05:30
|
|
|
|
|
|
|
|
|
if (!config.crowdinApiKey) {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
console.error(ch.red`crowdinApiKey is required`);
|
|
|
|
|
process.exit(126);
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PROJECT_ID = 'elyby';
|
2019-11-09 18:12:02 +05:30
|
|
|
|
const PROJECT_KEY = config.crowdinApiKey;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
const CROWDIN_FILE_PATH = 'accounts/site.json';
|
|
|
|
|
const SOURCE_LANG = 'en';
|
2019-12-08 00:32:00 +05:30
|
|
|
|
const LANG_DIR = path.resolve(`${__dirname}/../app/i18n`);
|
2019-12-07 16:58:52 +05:30
|
|
|
|
const INDEX_FILE_NAME = 'index.js';
|
2019-03-09 05:40:06 +05:30
|
|
|
|
const MIN_RELEASE_PROGRESS = 80; // Minimal ready percent before translation can be published
|
|
|
|
|
|
|
|
|
|
const crowdin = new CrowdinApi({ apiKey: PROJECT_KEY });
|
|
|
|
|
const progressBar = new MultiProgress();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Locales that has been verified by core team members
|
|
|
|
|
*/
|
|
|
|
|
const RELEASED_LOCALES = ['be', 'fr', 'id', 'pt', 'ru', 'uk', 'vi', 'zh'];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Array of Crowdin locales to our internal locales representation
|
|
|
|
|
*/
|
|
|
|
|
const LOCALES_MAP = {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
'pt-BR': 'pt',
|
|
|
|
|
'zh-CN': 'zh',
|
2019-03-09 05:40:06 +05:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This array allows us to customise native languages names, because ISO-639-1 sometimes is strange
|
|
|
|
|
*/
|
|
|
|
|
const NATIVE_NAMES_MAP = {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
be: 'Беларуская',
|
|
|
|
|
id: 'Bahasa Indonesia',
|
|
|
|
|
lt: 'Lietuvių',
|
|
|
|
|
pl: 'Polski',
|
|
|
|
|
pt: 'Português do Brasil',
|
|
|
|
|
sr: 'Српски',
|
|
|
|
|
ro: 'Română',
|
|
|
|
|
zh: '简体中文',
|
2019-03-09 05:40:06 +05:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This arrays allows us to override Crowdin English languages names
|
|
|
|
|
*/
|
|
|
|
|
const ENGLISH_NAMES_MAP = {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
pt: 'Portuguese, Brazilian',
|
|
|
|
|
sr: 'Serbian',
|
|
|
|
|
zh: 'Simplified Chinese',
|
2019-03-09 05:40:06 +05:30
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Converts Crowdin's language code to our internal value
|
|
|
|
|
*
|
|
|
|
|
* @param {string} code
|
2019-11-27 14:33:32 +05:30
|
|
|
|
* @returns {string}
|
2019-03-09 05:40:06 +05:30
|
|
|
|
*/
|
|
|
|
|
function toInternalLocale(code: string): string {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
return LOCALES_MAP[code] || code;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Форматирует входящий объект с переводами в итоговую строку в том формате, в каком они
|
|
|
|
|
* хранятся в самом приложении
|
|
|
|
|
*
|
|
|
|
|
* @param {object} translates
|
2019-11-27 14:33:32 +05:30
|
|
|
|
* @returns {string}
|
2019-03-09 05:40:06 +05:30
|
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
|
function serializeToFormattedJson(
|
|
|
|
|
translates: { [key: string]: any },
|
|
|
|
|
{ asModule = false }: { asModule?: boolean } = {},
|
|
|
|
|
): string {
|
|
|
|
|
const src = JSON.stringify(sortByKeys(translates), null, 2);
|
|
|
|
|
|
|
|
|
|
return asModule ? `module.exports = ${src};\n` : `${src}\n`;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* http://stackoverflow.com/a/29622653/5184751
|
|
|
|
|
*
|
|
|
|
|
* @param {object} object
|
2019-11-27 14:33:32 +05:30
|
|
|
|
* @returns {object}
|
2019-03-09 05:40:06 +05:30
|
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
|
function sortByKeys(object: { [key: string]: any }): { [key: string]: any } {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
return Object.keys(object)
|
|
|
|
|
.sort()
|
|
|
|
|
.reduce((result, key) => {
|
|
|
|
|
result[key] = object[key];
|
|
|
|
|
|
|
|
|
|
return result;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}, {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ProjectInfoFile {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
node_type: 'file';
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
created: string;
|
|
|
|
|
last_updated: string;
|
|
|
|
|
last_accessed: string;
|
|
|
|
|
last_revision: string;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ProjectInfoDirectory {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
node_type: 'directory';
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
files: Array<ProjectInfoFile | ProjectInfoDirectory>;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface ProjectInfoResponse {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
details: {
|
|
|
|
|
source_language: {
|
2019-12-07 16:58:52 +05:30
|
|
|
|
name: string;
|
|
|
|
|
code: string;
|
|
|
|
|
};
|
|
|
|
|
name: string;
|
|
|
|
|
identifier: string;
|
|
|
|
|
created: string;
|
|
|
|
|
description: string;
|
|
|
|
|
join_policy: string;
|
|
|
|
|
last_build: string | null;
|
|
|
|
|
last_activity: string;
|
|
|
|
|
participants_count: string; // it's number, but string in the response
|
|
|
|
|
logo_url: string | null;
|
|
|
|
|
total_strings_count: string; // it's number, but string in the response
|
|
|
|
|
total_words_count: string; // it's number, but string in the response
|
|
|
|
|
duplicate_strings_count: number;
|
|
|
|
|
duplicate_words_count: number;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
invite_url: {
|
2019-12-07 16:58:52 +05:30
|
|
|
|
translator: string;
|
|
|
|
|
proofreader: string;
|
|
|
|
|
};
|
2019-11-27 14:33:32 +05:30
|
|
|
|
};
|
|
|
|
|
languages: Array<{
|
2019-12-07 16:58:52 +05:30
|
|
|
|
name: string; // English language name
|
|
|
|
|
code: string;
|
|
|
|
|
can_translate: 0 | 1;
|
|
|
|
|
can_approve: 0 | 1;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
}>;
|
|
|
|
|
files: Array<ProjectInfoFile | ProjectInfoDirectory>;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function pullLocales() {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
const { languages }: ProjectInfoResponse = await crowdin.projectInfo(
|
|
|
|
|
PROJECT_ID,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return languages;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface LanguageStatusNode {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
node_type: 'directory' | 'file';
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
phrases: number;
|
|
|
|
|
translated: number;
|
|
|
|
|
approved: number;
|
|
|
|
|
words: number;
|
|
|
|
|
words_translated: number;
|
|
|
|
|
words_approved: number;
|
|
|
|
|
files: Array<LanguageStatusNode>;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
function findFile(
|
|
|
|
|
root: Array<LanguageStatusNode>,
|
|
|
|
|
path: string,
|
|
|
|
|
): LanguageStatusNode | null {
|
|
|
|
|
const [nodeToSearch, ...rest] = path.split('/');
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
for (const node of root) {
|
|
|
|
|
if (node.name !== nodeToSearch) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
if (rest.length === 0) {
|
|
|
|
|
return node;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
return findFile(node.files, rest.join('/'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IndexFileEntry {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
code: string;
|
|
|
|
|
name: string;
|
|
|
|
|
englishName: string;
|
|
|
|
|
progress: number;
|
|
|
|
|
isReleased: boolean;
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function pull() {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
console.log('Pulling locales list...');
|
|
|
|
|
const locales = await pullLocales();
|
|
|
|
|
const checkingProgressBar = progressBar.newBar(
|
|
|
|
|
'| Pulling locales info :bar :percent | :current/:total',
|
|
|
|
|
{
|
|
|
|
|
total: locales.length,
|
|
|
|
|
incomplete: '\u2591',
|
|
|
|
|
complete: '\u2588',
|
|
|
|
|
width: locales.length,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
// Add prefix 'c' to current and total to prevent filling thees placeholders with real values
|
|
|
|
|
const downloadingProgressBar = progressBar.newBar(
|
|
|
|
|
'| Downloading translates :bar :percent | :cCurrent/:cTotal',
|
|
|
|
|
{
|
|
|
|
|
total: 100,
|
|
|
|
|
incomplete: '\u2591',
|
|
|
|
|
complete: '\u2588',
|
|
|
|
|
width: locales.length,
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
let downloadingTotal = 0;
|
|
|
|
|
let downloadingReady = 0;
|
|
|
|
|
const results = await Promise.all(
|
|
|
|
|
locales.map(async locale => {
|
|
|
|
|
const {
|
|
|
|
|
files,
|
|
|
|
|
}: { files: Array<LanguageStatusNode> } = await crowdin.languageStatus(
|
|
|
|
|
PROJECT_ID,
|
|
|
|
|
locale.code,
|
|
|
|
|
);
|
|
|
|
|
checkingProgressBar.tick();
|
|
|
|
|
const fileInfo = findFile(files, CROWDIN_FILE_PATH);
|
|
|
|
|
|
|
|
|
|
if (fileInfo === null) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Unable to find translation file. Please check the CROWDIN_FILE_PATH param.',
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const progress = (fileInfo.words_approved / fileInfo.words) * 100;
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
!RELEASED_LOCALES.includes(toInternalLocale(locale.code)) &&
|
|
|
|
|
progress < MIN_RELEASE_PROGRESS
|
|
|
|
|
) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
downloadingProgressBar.update(downloadingReady / ++downloadingTotal, {
|
|
|
|
|
cCurrent: downloadingReady,
|
|
|
|
|
cTotal: downloadingTotal,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const translatesFilePath = await crowdin.exportFile(
|
|
|
|
|
PROJECT_ID,
|
|
|
|
|
CROWDIN_FILE_PATH,
|
|
|
|
|
locale.code,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
downloadingProgressBar.update(++downloadingReady / downloadingTotal, {
|
|
|
|
|
cCurrent: downloadingReady,
|
|
|
|
|
cTotal: downloadingTotal,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
locale,
|
|
|
|
|
progress,
|
|
|
|
|
translatesFilePath,
|
|
|
|
|
};
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log('Locales are downloaded. Writing them to file system.');
|
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
|
const indexFileEntries: { [key: string]: IndexFileEntry } = {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
en: {
|
|
|
|
|
code: 'en',
|
|
|
|
|
name: 'English',
|
|
|
|
|
englishName: 'English',
|
|
|
|
|
progress: 100,
|
|
|
|
|
isReleased: true,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
await Promise.all(
|
|
|
|
|
results.map(
|
|
|
|
|
result =>
|
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
if (result === null) {
|
|
|
|
|
resolve();
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
return;
|
|
|
|
|
}
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
const {
|
|
|
|
|
locale: { code, name },
|
2019-03-09 05:40:06 +05:30
|
|
|
|
progress,
|
|
|
|
|
translatesFilePath,
|
2019-11-27 14:33:32 +05:30
|
|
|
|
} = result;
|
|
|
|
|
const ourCode = toInternalLocale(code);
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
indexFileEntries[ourCode] = {
|
2019-03-09 05:40:06 +05:30
|
|
|
|
code: ourCode,
|
|
|
|
|
name: NATIVE_NAMES_MAP[ourCode] || iso639.getNativeName(ourCode),
|
|
|
|
|
englishName: ENGLISH_NAMES_MAP[ourCode] || name,
|
|
|
|
|
progress: parseFloat(progress.toFixed(1)),
|
|
|
|
|
isReleased: RELEASED_LOCALES.includes(ourCode),
|
2019-11-27 14:33:32 +05:30
|
|
|
|
};
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
fs.copyFile(
|
|
|
|
|
translatesFilePath,
|
|
|
|
|
path.join(LANG_DIR, `${ourCode}.json`),
|
|
|
|
|
0,
|
|
|
|
|
err => {
|
|
|
|
|
err ? reject(err) : resolve();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
),
|
|
|
|
|
);
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
console.log('Writing an index file.');
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
fs.writeFileSync(
|
|
|
|
|
path.join(LANG_DIR, INDEX_FILE_NAME),
|
2019-12-07 16:58:52 +05:30
|
|
|
|
serializeToFormattedJson(indexFileEntries, { asModule: true }),
|
2019-11-27 14:33:32 +05:30
|
|
|
|
);
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
console.log(ch.green('The index file was successfully written'));
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function push() {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
prompt.start();
|
|
|
|
|
prompt.get(
|
|
|
|
|
{
|
|
|
|
|
properties: {
|
|
|
|
|
disapprove: {
|
|
|
|
|
description: 'Disapprove changed lines? [Y/n]',
|
|
|
|
|
pattern: /^y|n$/i,
|
|
|
|
|
message: 'Please enter "y" or "n"',
|
|
|
|
|
default: 'y',
|
|
|
|
|
before: value => value.toLowerCase() === 'y',
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
async (err, { disapprove }) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
reject(err);
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
return;
|
|
|
|
|
}
|
2019-03-09 05:40:06 +05:30
|
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
|
console.log(`Publishing ${ch.bold(SOURCE_LANG)} translates file...`);
|
|
|
|
|
|
|
|
|
|
await crowdin.updateFile(
|
|
|
|
|
PROJECT_ID,
|
|
|
|
|
{
|
|
|
|
|
[CROWDIN_FILE_PATH]: path.join(LANG_DIR, `${SOURCE_LANG}.json`),
|
|
|
|
|
},
|
|
|
|
|
{
|
2019-12-07 16:58:52 +05:30
|
|
|
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
2019-11-27 14:33:32 +05:30
|
|
|
|
update_option: disapprove
|
|
|
|
|
? 'update_as_unapproved'
|
|
|
|
|
: 'update_without_changes',
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(ch.green('Success'));
|
|
|
|
|
|
|
|
|
|
resolve();
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
const action = process.argv[2];
|
|
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
|
case 'pull':
|
|
|
|
|
pull();
|
|
|
|
|
break;
|
|
|
|
|
case 'push':
|
|
|
|
|
push();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
console.error(`Unknown action ${action}`);
|
|
|
|
|
}
|
2019-03-09 05:40:06 +05:30
|
|
|
|
} catch (exception) {
|
2019-11-27 14:33:32 +05:30
|
|
|
|
console.error(exception);
|
2019-03-09 05:40:06 +05:30
|
|
|
|
}
|