-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate-es6.js
85 lines (74 loc) · 2.53 KB
/
translate-es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/node
if (process.env.NODE_ENV != 'production') require('dotenv').config();
// Set up Algolia
const algolia = require('algoliasearch');
const client = algolia(
process.env.AG_APPLICATION_ID,
process.env.AG_API_WRITE_KEY
);
const index = client.initIndex(process.env.AG_INDEX_NAME);
// Set up Axios
const axios = require('axios');
// Take in the objectID from the index as an argument
// Example: $node translate 3dvf4fg
const algoliaObjectID = process.argv[2];
// Take in the target language (es, fr, de)
const targetLanguage = process.argv[3];
// Translate the text
const translate = inputText => {
console.log(
'Retrieved data for ' + algoliaObjectID + '. Waiting for translation...'
);
// Create an object of the input text, source lang and target lang
let objectToTranslate = {
text: inputText, // The words to be translated
source: 'en', // The language they are in
target: targetLanguage // The language you want them to be
};
// Request the translation from IBM Watson
axios({
method: 'post',
url:
'https://gateway.watsonplatform.net/language-translator/api/v2/translate',
data: objectToTranslate,
headers: { Accept: 'application/json' },
auth: {
username: process.env.IBM_USERNAME,
password: process.env.IBM_PASSWORD
}
})
.then(response => {
// The response object is long, let's shorten it.
const { translations } = response.data;
// Is there a translation?
if (translations.length > 0 && typeof translations !== 'undefined') {
let translatedObject = {
objectID: algoliaObjectID
};
// This is a new field we'll add to the existing object
// Here we're dynamically setting the key so that different language
// translations can be added to the same object
translatedObject['description_' + targetLanguage] =
translations[0].translation;
// If yes, send it to be added to our Algolia object
index
.partialUpdateObject(translatedObject)
.then(content =>
console.log('Translation added to ' + algoliaObjectID)
)
.catch(err =>
console.log('Error adding translation to ' + algoliaObjectID)
);
} else {
// If no translation then let us know that
console.log('Error translating ' + algoliaObjectID);
}
})
.catch(err => console.log(err));
};
index
.getObject(algoliaObjectID)
.then(content => translate(content.description_en))
.catch(err => {
throw err;
});