Skip to content

Commit 5a744bd

Browse files
committed
fixed #26
fixed some bugs polish code
1 parent e5d7ffc commit 5a744bd

File tree

5 files changed

+220
-145
lines changed

5 files changed

+220
-145
lines changed

index.js

Lines changed: 70 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,216 +1,178 @@
11
"use strict";
22
var alfy = require('alfy');
33
var tts = require("./tts");
4-
var translate = require("./translate");
4+
var translator = require("./translate");
55
var configstore = require('configstore');
66
var os = require('os');
7-
var fs = require('fs');
87
var uuidv4 = require('uuid/v4');
98
var languagePair = new configstore('language-config-pair');
109
var history = new configstore("translate-history");
1110

12-
var g_data = {
11+
var g_config = {
1312
voice: process.env.voice || 'remote',
1413
save: process.env.save_count || 20,
1514
domain: process.env.domain || 'https://translate.google.com',
1615
input: alfy.input,
1716
from: {
1817
lang: 'auto',
19-
ttsfile: os.tmpdir() + '/' + uuidv4() + ".mp3",
20-
text: []
18+
ttsfile: os.tmpdir() + '/' + uuidv4() + ".mp3"
2119
},
2220
to: {
2321
lang: 'en',
24-
ttsfile: os.tmpdir() + '/' + uuidv4() + ".mp3",
25-
text: []
22+
ttsfile: os.tmpdir() + '/' + uuidv4() + ".mp3"
2623
}
2724
};
2825

2926
var pair = languagePair.get('pair');
3027
if (pair) {
3128
// auto
3229
// language detect
33-
translate(g_data.input, {raw: true, from: 'auto', to: 'en', domain: g_data.domain})
30+
translator
31+
.translate(g_config.input, {from: g_config.from.lang, to: g_config.to.lang, domain: g_config.domain})
3432
.then(function (res) {
35-
var detect = JSON.parse(res.raw)[2];
33+
var detect = res.from.language.iso;
3634
if (pair[0] === detect) {
37-
g_data.from.lang = pair[0];
38-
g_data.to.lang = pair[1];
35+
g_config.from.lang = pair[0];
36+
g_config.to.lang = pair[1];
3937
} else if (pair[1] === detect) {
40-
g_data.from.lang = pair[1];
41-
g_data.to.lang = pair[0];
38+
g_config.from.lang = pair[1];
39+
g_config.to.lang = pair[0];
4240
}
4341

44-
doTranslate(g_data);
42+
doTranslate();
4543
});
4644

4745
return;
4846
} else {
4947
// manual
5048
if (languagePair.get('source') && languagePair.get('target')) {
51-
g_data.from.lang = languagePair.get('source');
52-
g_data.to.lang = languagePair.get('target');
49+
g_config.from.lang = languagePair.get('source');
50+
g_config.to.lang = languagePair.get('target');
5351
}
5452

55-
doTranslate(g_data);
53+
doTranslate();
5654
}
5755

58-
function doTranslate(data) {
56+
function doTranslate() {
5957
//文档上说cmd+L时会找largetype,找不到会找arg,但是实际并不生效。
6058
//同时下一步的发音模块中query变量的值为arg的值。
61-
translate(data.input, {raw: true, from: data.from.lang, to: data.to.lang, domain: data.domain})
59+
translator
60+
.translate(g_config.input, {from: g_config.from.lang, to: g_config.to.lang, domain: g_config.domain})
6261
.then(function (res) {
6362
var items = [];
6463

65-
if (res.from.text.autoCorrected) {
64+
if (res.from.corrected.corrected || res.from.corrected.didYouMean) {
6665

67-
var corrected = res.from.text.value
66+
var corrected = res.from.corrected.value
6867
.replace(/\[/, "")
6968
.replace(/\]/, "");
7069

7170
// Correct
7271
items.push({
73-
title: res.text,
72+
title: res.to.text.value,
7473
subtitle: `Show translation for ${corrected}?`,
7574
autocomplete: corrected
7675
});
7776

7877
} else {
7978

80-
var rawObj = JSON.parse(res.raw);
81-
82-
var translation = rawObj[0];
83-
var indexOfStandard = 0;
84-
translation.forEach(obj => {
85-
if (obj[0]) {
86-
data.from.text.push(obj[1]);
87-
data.to.text.push(obj[0]);
88-
indexOfStandard++;
89-
}
90-
});
91-
var standard = rawObj[0][indexOfStandard] || [];
92-
93-
var fromStandard = standard[3] || '';
94-
var fromText = data.from.text.join(' ');
95-
var fromArg = data.voice === 'remote' ? data.from.ttsfile : data.voice === 'local' ? fromText : '';
79+
var fromPhonetic = res.from.text.phonetic;
80+
var fromText = res.from.text.value;
81+
var fromArg = g_config.voice === 'remote' ? g_config.from.ttsfile : g_config.voice === 'local' ? fromText : '';
9682
// Input
9783
items.push({
9884
title: fromText,
99-
subtitle: fromStandard,
100-
quicklookurl: `${data.domain}/#view=home&op=translate&sl=${data.from.lang}&tl=${data.to.lang}&text=${encodeURIComponent(data.from.text)}`,
85+
subtitle: `Phonetic: ${fromPhonetic || ''}`,
86+
quicklookurl: `${g_config.domain}/#view=home&op=translate&sl=${g_config.from.lang}&tl=${g_config.to.lang}&text=${encodeURIComponent(fromText)}`,
10187
arg: fromArg,
10288
text: {
10389
copy: fromText,
10490
largetype: fromText
10591
},
10692
icon: {
107-
path: data.voice === 'none' ? 'icon.png' : 'tts.png'
93+
path: g_config.voice === 'none' ? 'icon.png' : 'tts.png'
10894
}
10995
});
11096

111-
var toStandard = standard[2] || '';
112-
var toText = data.to.text.join(' ');
113-
var toArg = data.voice === 'remote' ? data.to.ttsfile : data.voice === 'local' ? toText : '';
97+
var toPhonetic = res.to.text.phonetic;
98+
var toText = res.to.text.value;
99+
var toArg = g_config.voice === 'remote' ? g_config.to.ttsfile : g_config.voice === 'local' ? toText : '';
114100
// Translation
115101
items.push({
116102
title: toText,
117-
subtitle: toStandard,
118-
quicklookurl: `${data.domain}/#view=home&op=translate&sl=${data.to.lang}&tl=${data.from.lang}&text=${encodeURIComponent(data.to.text)}`,
103+
subtitle: `Phonetic: ${toPhonetic || ''}`,
104+
quicklookurl: `${g_config.domain}/#view=home&op=translate&sl=${g_config.to.lang}&tl=${g_config.from.lang}&text=${encodeURIComponent(toText)}`,
119105
arg: toArg,
120106
text: {
121107
copy: toText,
122108
largetype: toText
123109
},
124110
icon: {
125-
path: data.voice === 'none' ? 'icon.png' : 'tts.png'
111+
path: g_config.voice === 'none' ? 'icon.png' : 'tts.png'
126112
}
127113
});
128114

129115
// Definitions
130-
if (rawObj[12]) {
131-
rawObj[12].forEach(obj => {
132-
var partsOfSpeech = obj[0];
133-
obj[1].forEach(x => {
134-
var definitions = x[0];
135-
var example = x[2];
136-
items.push({
137-
title: `Definition[${partsOfSpeech}]: ${definitions}`,
138-
subtitle: `Example: "${example || 'none'}"`,
139-
quicklookurl: `${data.domain}/#view=home&op=translate&sl=${data.from.lang}&tl=${data.to.lang}&text=${encodeURIComponent(data.from.text)}`,
140-
text: {
141-
copy: definitions,
142-
largetype: `Definitions: ${definitions}\nExample: "${example || 'none'}"`
143-
}
144-
});
116+
if (res.to.definitions.length > 0) {
117+
res.to.definitions.forEach(definition => {
118+
items.push({
119+
title: `Definition[${definition.partsOfSpeech}]: ${definition.value}`,
120+
subtitle: `Example: ${definition.example || ''}`,
121+
quicklookurl: `${g_config.domain}/#view=home&op=translate&sl=${g_config.from.lang}&tl=${g_config.to.lang}&text=${encodeURIComponent(fromText)}`,
122+
text: {
123+
copy: definition.value,
124+
largetype: `Definitions: ${definition.value}\n\nExample: ${definition.example || ''}`
125+
}
145126
});
146127
});
147128
}
148129

149130
// Translation Of
150-
if (rawObj[1]) {
151-
rawObj[1].forEach(obj => {
152-
var partsOfSpeech = obj[0];
153-
obj[2].forEach(x => {
154-
var text = x[0];
155-
var synonyms = x[1];
156-
var frequency = x[3];
157-
items.push({
158-
title: `Translation[${partsOfSpeech}]: ${text}`,
159-
subtitle: `Frequency: ${frequency ? frequency.toFixed(4) : '0.0000'} Synonyms: ${synonyms ? synonyms.join(', ') : 'none'}`,
160-
text: {
161-
copy: text,
162-
largetype: `${text}\nSynonyms: ${synonyms ? synonyms.join(', ') : 'none'}`
163-
}
164-
});
131+
if (res.to.translations.length > 0) {
132+
res.to.translations.forEach(translation => {
133+
items.push({
134+
title: `Translation[${translation.partsOfSpeech}]: ${translation.value}`,
135+
subtitle: `Frequency: ${translation.frequency ? translation.frequency.toFixed(4) : '0.0000'} Synonyms: ${translation.synonyms ? translation.synonyms.join(', ') : ''}`,
136+
text: {
137+
copy: translation.value,
138+
largetype: `${translation.value}\n\nSynonyms: ${translation.synonyms ? translation.synonyms.join(', ') : ''}`
139+
}
165140
});
166141
});
167142
}
168143
}
169144

170145
alfy.output(items);
171146

172-
return data;
147+
return res;
173148
})
174-
.then(function (data) {
149+
.then(res => {
175150
// history
176-
if (data.save > 0 && data.from.text.length > 0 && data.to.text.length > 0) {
151+
if (g_config.save > 0) {
177152
var value = {
178153
time: Date.now(),
179-
from: data.from.text.join(' '),
180-
to: data.to.text.join(' ')
154+
from: res.from.text.value,
155+
to: res.to.text.value
181156
};
182157
var histories = history.get('history') ? JSON.parse(history.get('history')) : [];
183-
if (histories.length >= data.save) histories.shift();
158+
if (histories.length >= g_config.save) histories.shift();
184159
histories.push(value);
185160
history.set('history', JSON.stringify(histories));
186161
}
187162

188-
return data;
163+
return res;
189164
})
190-
.then(data => {
165+
.then(res => {
191166
// tts
192-
if (data.voice === 'remote') {
193-
createtts(data.domain, data.from.text.reverse(), data.from.lang, data.from.ttsfile, true);
194-
createtts(data.domain, data.to.text.reverse(), data.to.lang, data.to.ttsfile, true);
167+
if (g_config.voice === 'remote') {
168+
var fromArray = [];
169+
res.from.text.array.forEach(o => tts.split(o).forEach(t => fromArray.push(t)));
170+
tts.multi(fromArray, {to: g_config.from.lang, domain: g_config.domain, file: g_config.from.ttsfile});
171+
172+
var toArray = [];
173+
res.to.text.array.forEach(o => tts.split(o).forEach(t => toArray.push(t)));
174+
tts.multi(toArray, {to: g_config.to.lang, domain: g_config.domain, file: g_config.to.ttsfile});
195175
}
196-
});
197-
}
198-
199-
function createtts(domain, data, lang, file, create) {
200-
var text = data.pop();
201-
if (!text) return;
202-
tts(text, {to: lang, domain: domain})
203-
.then(buffer => {
204-
if (create) {
205-
fs.writeFile(file, buffer, function (err) {
206-
if (err) throw err;
207-
createtts(domain, data, lang, file, false);
208-
});
209-
} else {
210-
fs.appendFile(file, buffer, function (err) {
211-
if (err) throw err;
212-
createtts(domain, data, lang, file, false);
213-
});
214-
}
215-
});
176+
})
177+
;
216178
}

info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
<key>escaping</key>
8585
<integer>0</integer>
8686
<key>script</key>
87-
<string>if [ "$read" = "local" ];then
87+
<string>if [ "$voice" = "local" ];then
8888
say {query} -v Samantha
8989
else
9090
afplay {query}

token.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ var window = {
7373
TKK: config.get('TKK') || '0'
7474
};
7575

76-
function updateTKK(domain) {
76+
function updateTKK(url) {
7777
return new Promise(function (resolve, reject) {
7878
var now = Math.floor(Date.now() / 3600000);
7979

8080
if (Number(window.TKK.split('.')[0]) === now) {
8181
resolve();
8282
} else {
83-
got(domain).then(function (res) {
83+
got(url).then(function (res) {
8484
var matches = res.body.match(/tkk:\s?'(.+?)'/i);
8585

8686
if (matches) {
@@ -104,8 +104,10 @@ function updateTKK(domain) {
104104
});
105105
}
106106

107-
function get(text, domain) {
108-
return updateTKK(domain).then(function () {
107+
function get(text, opts) {
108+
opts = opts || {};
109+
110+
return updateTKK(opts.domain).then(function () {
109111
var tk = sM(text);
110112
tk = tk.replace('&tk=', '');
111113
return {name: 'tk', value: tk};

0 commit comments

Comments
 (0)