-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.js
More file actions
68 lines (55 loc) · 2.04 KB
/
translate.js
File metadata and controls
68 lines (55 loc) · 2.04 KB
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
var request = require('request');
var fs = require('fs');
var path = require('path');
function translateToken(token, index, sourceLang, targetLang) { {
var text = token.value;
var url = "https://translate.googleapis.com/translate_a/single?client=gtx&sl="
+ sourceLang + "&tl=" + targetLang + "&dt=t&q=" + encodeURI(sourceText);
return new Promise(function(resolve, reject) {
request(url, function (error, response, body) {
resolve(
{
"origValue": sourceText,
"index", index,
"translated": JSON.parse(body)[0][0][0])
}
)
// TODO: Error handling
});
});
}
function translate_file(config, target_file, SOURCE_LANG, DEST_LANG) {
// Load file from disk
let source = "";
fs.readFile(target_file, function(err, data) {
source = data;
});
let map_file_path = TRANSLATE_DICT_FILES_PATH + `${target_file}.map`;
let map = {};
fs.stat(map_file_path, function(err, stat) {
if(err == null) {
fs.readFile(map_file_path, function(err, data) {
map = JSON.parse(data);
});
}
});
let fextension = path.extname(target_file);
payload = {'doc': source, 'from': SOURCE_LANG,
'to': DEST_LANG, 'map': JSON.stringify(map), 'ext':fextension}
//TODO: Make some sort of request to the translation server
// Assume translated is the translated text and translation_map
// is the map for the translation
let translated;
let translation_map;
// Translations overwrite the translated file
translated_file_path = target_file
translation_map_path = map_file_path
// TODO: Create translation map folder if it does not exist
fs.writeFile(translation_file_path, translated);
fs.writeFile(translation_map_path, translation_map);
}
function translate_all(config, DEST_LANG, additional_ignores=[]) {
}
module.exports = {
test() {console.log("this is the test function for you david")}
}