Skip to content

Commit

Permalink
Merge branch 'v2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
xfslove committed Jun 11, 2019
2 parents 883de42 + 6831386 commit 4330ff0
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 87 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
.idea
package-lock.json
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,17 @@
*Requires [Node.js](https://nodejs.org) 4+ and the Alfred [Powerpack](https://www.alfredapp.com/powerpack/).*

- install with `npm install -g alfred-google-translate`
- or [download](https://github.com/xfslove/alfred-google-translate/releases/tag/v2.0.0) workflow
- or [download](https://github.com/xfslove/alfred-google-translate/releases/tag/v2.0.1) workflow

## [Changelog](https://github.com/xfslove/alfred-google-translate/releases)

## Notice

for chinese.

2.x版本是支持多种语言翻译的,所以翻译所使用的域名为`translate.google.com`,这个域名目前在大陆无法访问。

因此可以继续使用[1.x版本](https://github.com/xfslove/alfred-google-translate/tree/v1.x).

如果使用2.x版本,需在workflow的环境变量中加入`domain=translate.google.cn`,如下:

![domain.gif](media/domain.gif)

## Usage

Before using this workflow, You must config the language pair. see [alfred-language-configuration](https://github.com/xfslove/alfred-language-configuration)

Alfred workflow Keyword `tr`.

the items explain:
the shown items explain:

The first item is input word or sentence (webster phonetic if avaliable).

Expand All @@ -45,11 +33,29 @@ at the first and second item You can:

if You input wrong word, the workflow will correct your input, and You can press <kbd>enter</kbd> to see.

## Environment Variables

| name | default value | description |
| ---------- | ---------------------------- | ------------------------------------------------------------ |
| domain | https://translate.google.com | if you cannot access the default domain, you can config this. 大陆访问不了默认域名,所以如果使用2.x版本需要将这个变量设置为https://translate.google.cn。或者还是使用[1.x版本](https://github.com/xfslove/alfred-google-translate/tree/v1.x) |
| read | remote | avaliable values: remote: fetch voice from google, local: use macOS local voice (notice: maybe only works on English), none: dont get voice |
| save_count | 20 | limit count of translate history, see [alfred-translate-history](https://github.com/xfslove/alfred-translate-history) |

##### environment variables config snapshot:

![env-config.png](media/env-config.png)

![env.png](media/env.png)

## Hotkey

if you download the workflow, you may lost the hotkey, so you can manual config this, like:
if you download the workflow, you may lost the hotkey, so you can manual config this.

##### hotkey config snapshot:

![hotkey.png](media/hotkey.png)

![hotkey](media/hotkey.gif)
![hotkey-config.png](media/hotkey-config.png)

##### hotkey and largetype snapshot:

Expand Down
148 changes: 89 additions & 59 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
"use strict";
const alfy = require('alfy');
const tts = require("./tts");
const translate = require("./translate");
const configstore = require('configstore');
const os = require('os');
const fs = require('fs');
const uuidv4 = require('uuid/v4');

const languagePair = new configstore('language-config-pair');
const domain = process.env.domain || 'https://translate.google.com';
const q = alfy.input;
var alfy = require('alfy');
var tts = require("./tts");
var translate = require("./translate");
var configstore = require('configstore');
var os = require('os');
var fs = require('fs');
var uuidv4 = require('uuid/v4');
var languagePair = new configstore('language-config-pair');
var history = new configstore("translate-history");

var data = {
read: process.env.read || 'remote',
save: process.env.save_count || 20,
domain: process.env.domain || 'https://translate.google.com',
input: alfy.input,
from: {
lang: languagePair.get('source') || 'en',
ttsfile: os.tmpdir() + '/' + uuidv4() + ".mp3",
text: [],
standard: ''
text: []
},
to: {
lang: languagePair.get('target') || 'en',
ttsfile: os.tmpdir() + '/' + uuidv4() + ".mp3",
text: [],
standard: ''
text: []
}
}
};

//文档上说cmd+L时会找largetype,找不到会找arg,但是实际并不生效。
//同时下一步的发音模块中query变量的值为arg的值。
translate(q, { raw: true, from: data.from.lang, to: data.to.lang, domain: domain })
.then(res => {
translate(data.input, { raw: true, from: data.from.lang, to: data.to.lang, domain: data.domain })
.then(function (res) {
var items = [];

if (res.from.text.autoCorrected) {

const corrected = res.from.text.value
var corrected = res.from.text.value
.replace(/\[/, "")
.replace(/\]/, "");

Expand All @@ -46,9 +47,9 @@ translate(q, { raw: true, from: data.from.lang, to: data.to.lang, domain: domain

} else {

const rawObj = JSON.parse(res.raw);
var rawObj = JSON.parse(res.raw);

const translation = rawObj[0];
var translation = rawObj[0];
var indexOfStandard = 0;
translation.forEach(obj => {
if (obj[0]) {
Expand All @@ -57,50 +58,55 @@ translate(q, { raw: true, from: data.from.lang, to: data.to.lang, domain: domain
indexOfStandard++;
}
});
data.from.standard = rawObj[0][indexOfStandard][3];
data.to.standard = rawObj[0][indexOfStandard][2];
var standard = rawObj[0][indexOfStandard];

var fromStandard = standard[3] || '';
var fromText = data.from.text.join(' ');
var fromArg = data.read === 'remote' ? data.from.ttsfile : data.read === 'local' ? fromText : '';
// Input
items.push({
title: data.from.text.join(' '),
subtitle: data.from.standard || '',
quicklookurl: `${domain}/#view=home&op=translate&sl=${data.from.lang}&tl=${data.to.lang}&text=${encodeURIComponent(data.from.text)}`,
arg: data.from.ttsfile,
title: fromText,
subtitle: fromStandard,
quicklookurl: `${data.domain}/#view=home&op=translate&sl=${data.from.lang}&tl=${data.to.lang}&text=${encodeURIComponent(data.from.text)}`,
arg: fromArg,
text: {
copy: data.from.text.join(' '),
largetype: data.from.text.join(' ')
copy: fromText,
largetype: fromText
},
icon: {
path: 'tts.png'
path: data.read === 'none'? 'icon.png' : 'tts.png'
}
});


var toStandard = standard[2] || '';
var toText = data.to.text.join(' ');
var toArg = data.read === 'remote' ? data.to.ttsfile : data.read === 'local' ? toText : '';
// Translation
items.push({
title: data.to.text.join(' '),
subtitle: data.to.standard || '',
quicklookurl: `${domain}/#view=home&op=translate&sl=${data.to.lang}&tl=${data.from.lang}&text=${encodeURIComponent(data.to.text)}`,
arg: data.to.ttsfile,
title: toText,
subtitle: toStandard,
quicklookurl: `${data.domain}/#view=home&op=translate&sl=${data.to.lang}&tl=${data.from.lang}&text=${encodeURIComponent(data.to.text)}`,
arg: toArg,
text: {
copy: data.to.text.join(' '),
largetype: data.to.text.join(' ')
copy: toText,
largetype: toText
},
icon: {
path: 'tts.png'
path: data.read === 'none'? 'icon.png' : 'tts.png'
}
});

// Definitions
if (rawObj[12]) {
rawObj[12].forEach(obj => {
const partsOfSpeech = obj[0];
obj[1].forEach(m => {
const definitions = m[0];
const example = m[2];
var partsOfSpeech = obj[0];
obj[1].forEach(x => {
var definitions = x[0];
var example = x[2];
items.push({
title: `Definition[${partsOfSpeech}]: ${definitions}`,
subtitle: `Example: "${example || 'none'}"`,
quicklookurl: `${domain}/#view=home&op=translate&sl=${data.from.lang}&tl=${data.to.lang}&text=${encodeURIComponent(data.from.text)}`,
quicklookurl: `${data.domain}/#view=home&op=translate&sl=${data.from.lang}&tl=${data.to.lang}&text=${encodeURIComponent(data.from.text)}`,
text: {
copy: definitions,
largetype: `Definitions: ${definitions}\nExample: "${example || 'none'}"`
Expand All @@ -113,55 +119,79 @@ translate(q, { raw: true, from: data.from.lang, to: data.to.lang, domain: domain
// Translation Of
if (rawObj[1]) {
rawObj[1].forEach(obj => {
const partsOfSpeech = obj[0];
var partsOfSpeech = obj[0];
obj[2].forEach(x => {
const text = x[0];
const synonyms = x[1];
const frequency = x[3];
var text = x[0];
var synonyms = x[1];
var frequency = x[3];
items.push({
title: `Translation[${partsOfSpeech}]: ${text}`,
subtitle: `Frequency: ${frequency ? frequency.toFixed(4) : '0.0000'} Synonyms: ${synonyms ? synonyms.join(', ') : 'none'}`
subtitle: `Frequency: ${frequency ? frequency.toFixed(4) : '0.0000'} Synonyms: ${synonyms ? synonyms.join(', ') : 'none'}`,
text: {
copy: text,
largetype: `${text}\nSynonyms: ${synonyms ? synonyms.join(', ') : 'none'}`
}
});
});
});
}
}

alfy.output(items);

return data;
})
.then(function (data) {
// history
if (data.save > 0 && data.from.text.length > 0 && data.to.text.length > 0) {
var value = {
time: Date.now(),
from: data.from.text.join(' '),
to: data.to.text.join(' ')
};
var histories = history.get('history') ? JSON.parse(history.get('history')) : [];
if (histories.length >= data.save) histories.shift();
histories.push(value);
history.set('history', JSON.stringify(histories));
}

return data;
})
.then(function() {
.then(data => {
// tts
createtts(data.from.text.reverse(), data.from.lang, data.from.ttsfile, true);
createtts(data.to.text.reverse(), data.to.lang, data.to.ttsfile, true);
if (data.read === 'remote') {
createtts(data.domain, data.from.text.reverse(), data.from.lang, data.from.ttsfile, true);
createtts(data.domain, data.to.text.reverse(), data.to.lang, data.to.ttsfile, true);
}
})
.catch(error => {

alfy.output([{
title: `Error: maybe input wrong language [${data.from.lang}].`,
subtitle: `current language configuration [${data.from.lang}>${data.to.lang}], Press ⌘L to see the full error.`,
text: {
largetype: error.stack || error
},
icon: {
path: 'warn.png'
}
largetype: error.stack || error
},
icon: {
path: 'warn.png'
}
}]);
});

function createtts(data, lang, file, create) {
function createtts(domain, data, lang, file, create) {
var text = data.pop();
if (!text) return;
tts(text, { to: lang, domain: domain })
.then(buffer => {
if (create) {
fs.writeFile(file, buffer, function(err) {
if (err) throw err;
createtts(data, lang, file, false);
createtts(domain, data, lang, file, false);
});
} else {
fs.appendFile(file, buffer, function(err) {
if (err) throw err;
createtts(data, lang, file, false);
createtts(domain, data, lang, file, false);
});
}
});
Expand Down
17 changes: 6 additions & 11 deletions info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<key>createdby</key>
<string>hanwen.huang</string>
<key>description</key>
<string>translate with google tanslate api</string>
<string>Alfred 3 workflow to translate with google tanslate api</string>
<key>disabled</key>
<false/>
<key>name</key>
Expand Down Expand Up @@ -84,7 +84,11 @@
<key>escaping</key>
<integer>0</integer>
<key>script</key>
<string>afplay {query}</string>
<string>if [ "$read" = "local" ];then
say {query} -v Samantha
else
afplay {query}
fi</string>
<key>scriptargtype</key>
<integer>0</integer>
<key>scriptfile</key>
Expand Down Expand Up @@ -173,15 +177,6 @@
<integer>160</integer>
</dict>
</dict>
<key>variables</key>
<dict>
<key>domain</key>
<string>https://translate.google.cn</string>
</dict>
<key>variablesdontexport</key>
<array>
<string>domain</string>
</array>
<key>version</key>
<string></string>
<key>webaddress</key>
Expand Down
Binary file removed media/domain.gif
Binary file not shown.
Binary file added media/env-config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/env.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/hotkey-config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed media/hotkey.gif
Binary file not shown.
Binary file added media/hotkey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alfred-google-translate",
"version": "2.0.0",
"version": "2.0.1",
"description": "Alfred 3 workflow to translate with google tanslate api",
"license": "MIT",
"repository": "xfslove/alfred-google-translate",
Expand Down

0 comments on commit 4330ff0

Please sign in to comment.