Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Trigger machine translation on upload #30

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,26 @@ module.exports = (argv) => ({
return apiCall(argv, 'export', { branch });
},

async preTranslate(engine, languages, files, branch) {
// Crowdin API does not support a branch argument so we add it to the file
// paths instead.
const fileSources = files.map(
(file) => (branch ? `${branch}/${file.source}` : file.source),
);

return dryApiCall(
argv,
'pre-translate',
{},
{
method: 'mt',
engine,
'languages[]': languages,
'files[]': fileSources,
},
);
},

getDownloadTranslationUrl(branch, language, file) {
return getUri(argv.projectIdentifier, 'export-file', {
key: argv.apiKey,
Expand Down
33 changes: 26 additions & 7 deletions lib/commands/upload_cmds/sources.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,36 @@ module.exports = {
for (const file of argv.files) {
process.stdout.write(`- ${file.source}...`);
const fileDir = path.dirname(file.source);
createdDirs = await checkDirExists(
API,
createdDirs,
fileDir,
branch,
info,
);
if (fileDir !== '.') {
createdDirs = await checkDirExists(
API,
createdDirs,
fileDir,
branch,
info,
);
}

await uploadFile(API, file, branch, info);
console.log(chalk.green('OK'));
}

if (
argv.preTranslationEngine &&
argv.preTranslationLanguages &&
argv.preTranslationLanguages.length > 0
) {
process.stdout.write(
`Pre-translating ${argv.preTranslationLanguages.join(', ')} with ${argv.preTranslationEngine} engine...`
);
await API.preTranslate(
argv.preTranslationEngine,
argv.preTranslationLanguages,
argv.files,
branch,
);
console.log(chalk.green('OK'));
}
/* eslint-enable */
},
};
15 changes: 6 additions & 9 deletions lib/utils/getCrowdinBranch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ const getBasePath = require('./getBasePath');

module.exports = async function getCrowdinBranch(argv) {
const { baseBranches, branch } = argv;
const projectBranch = await gitBranch(path.dirname(getBasePath(argv)));

return new Promise((resolve) => {
const targetBranch = branch || projectBranch;
const targetBranch = branch || await gitBranch(path.dirname(getBasePath(argv)));

if (!targetBranch || baseBranches.includes(targetBranch)) {
resolve(undefined);
} else {
resolve(targetBranch.replace(/\//g, '_'));
}
});
if (targetBranch && !baseBranches.includes(targetBranch)) {
return targetBranch.replace(/\//g, '_');
} else {
return undefined;
}
};