This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
113 lines (100 loc) · 3.33 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const ipcMain = require("electron").ipcMain;
const menubar = require("menubar");
const apis = require("./lib/apis");
const config = require("./config");
// there are 15 langCodes, including 'unknown'.
const langInfo = config.languageSet.langInfo;
const langCodes = config.languageSet.langCodes;
const mb = menubar({
width: config.width,
height: config.height,
dir: config.dir,
minWidth: config.minWidth
});
mb.on("ready", function ready() {
// main process
});
ipcMain.on("search", (e, { text, target }) => {
apis
.detect(text) // response example : { "langCode" : "ko" }
.then(res => checkRequestCode({ source: res.body.langCode, target }))
.then(res => requestTranslate({ source: res.source, target: res.target, text}))
.then(result => sendTranslatedText(e, result))
.catch(err => errorHandling(e, err));
});
ipcMain.on("hide", () => {
mb.app.hide();
});
function checkRequestCode({ source, target } = {}) {
if (source === "unk") {
let error = new Error("어느 나라 말인가요?");
error.name = "DetactError";
throw error;
}
return isSupported(setCodeForTranslate({ source, target }));
}
function setCodeForTranslate({ source, target } = {}) {
// set target if 'target' is undefined.
if (source === "ko") target = target || "en";
else target = target || "ko";
// adjust source name of Chinese for query.
if (source === "zh-cn") source = "zh-CN";
else if (source === "zh-tw") source = "zh-TW";
// adjust target name of Chinese for query.
if (target === "zh-cn") target = "zh-CN";
else if (target === "zh-tw") target = "zh-TW";
return { source, target };
}
function isSupported({ source, target } = {}) {
if (
!(
langInfo.translationKeys.includes(source) &&
langCodes[source].supportedTargets.includes(target)
)
) {
let error = new Error(
`아직 ${langCodes[source].name}에서 ${langCodes[target].name}로 번역할 줄은 몰라요.`
);
error.name = "TranslateError";
throw error;
} else {
return { source, target };
}
}
function requestTranslate({ source, target, text } = {}) {
return new Promise((resolve, reject) => {
apis
.translate({ source, target, text })
.then(res =>
resolve({
translatedText: res.body.message.result.translatedText,
source,
target,
text
})
)
.catch(err => reject(err));
});
}
function sendTranslatedText(e, result) {
// we may send a result Object including translatedText, source, target, text etc.
e.sender.send("async-search", true, result.translatedText);
}
function errorHandling(e, err) {
let message = "에러가 났네요. 다시 한 번 시도해주세요 :D";
if (err.name === "DetactError" || err.name === "TranslateError") {
message = err.message;
} else {
let errorCode = err.response.body.errorCode;
if (errorCode.startsWith('LD')) {
message = "언어감지 오류";
if (errorCode === "LD01") message += ": 문장을 넣어주세요.";
else message += ": 다시 한 번 시도해주세요.";
} else if (errorCode.startsWith('N2MT')) {
message = "번역 오류";
if (errorCode == "N2MT08") message += ": 문장이 너무 길어요!"; // up to 5000 characters.
else message += ": 다시 한 번 시도해주세요.";
}
}
e.sender.send("async-search", false, message);
}