Skip to content
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
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ typings/
# ignore this file that I sometime use while debugging
temp.json

examples-output
examples-output

# ignore IDE configuration
.idea
.vscode
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"translate-xlf": "./src/index.js"
},
"dependencies": {
"@vitalets/google-translate-api": "^2.4.0",
"bluebird": "^3.5.1",
"chalk": "^2.4.1",
"google-translate-api": "^2.3.0",
Expand Down
57 changes: 36 additions & 21 deletions src/translate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const googleTranslate = require('google-translate-api');
const googleTranslateVitalets = require('@vitalets/google-translate-api');
const bluebird = require('bluebird');
const chalk = require('chalk');
const log = require('./log');
Expand Down Expand Up @@ -54,33 +55,47 @@ function translate(input, from, to) {
// translation to the console, and replace the
// property's value with the translation

const translateParams = { from: from, to: to };
const translatePromise = googleTranslate(
textToTranslate,
{
from: from,
to: to
}
).then(res => {
log(
'Translating ' +
translateParams,
)
.catch(() =>
googleTranslateVitalets(
textToTranslate,
translateParams
)
)
.catch(() =>
googleTranslateVitalets(
textToTranslate,
Object.assign(
translateParams,
{ client: 'gtx' }
)
)
)
.then(res => {
log(
'Translating ' +
chalk.yellow(textToTranslate) +
' to ' +
chalk.green(res.text)
);
);

// update the object with the translation,
// make sure to match the format of the
// original <source> element
if (_.isString(value[0])) {
xlfObj['target'] = res.text;
} else if (
_.isObject(value[0]) &&
value[0]['_']
) {
xlfObj['target'] = _.cloneDeep(value);
xlfObj['target'][0]['_'] = res.text;
}
});
// update the object with the translation,
// make sure to match the format of the
// original <source> element
if (_.isString(value[0])) {
xlfObj['target'] = res.text;
} else if (
_.isObject(value[0]) &&
value[0]['_']
) {
xlfObj['target'] = _.cloneDeep(value);
xlfObj['target'][0]['_'] = res.text;
}
});

allPromises.push(translatePromise);
}
Expand Down