Jump to content

Google Translate (Languages Configurable)


Recommended Posts

The workflow breaks also when installing another global php version (e.g. with homebrew). I had to fix the workflow with your proposed one-liner-fix.

PHP 5.6.13 (cli)

I have another question regarding your nice workflow: Is it possible to trigger the workflow with a hotkey? I've tried some combinations but I didn't get it done. My desired workflow would be: select some text -> press a shortcut -> fires up trans workflow with the selected text

 

The hotkey is a neat idea. I just added it to the workflow and push an update (1.3) to Packal.

 

Also, I rewrote getting / fetching mechanisms that should work with all versions. The configs did change a little bit, so you might have to reset them.

Link to comment

I love your workflow, yet I'm stuck with one question. Right now when you press enter, it copies the text to the clipboard. In many cases, I'm curious about the alternative translations. Would it be possible to open google translate in the browser with the picked languages so I can easily view the translations?

Link to comment

I love your workflow, yet I'm stuck with one question. Right now when you press enter, it copies the text to the clipboard. In many cases, I'm curious about the alternative translations. Would it be possible to open google translate in the browser with the picked languages so I can easily view the translations?

 

Hm. The workflow needs to be redesigned (not necessarily completely rewritten) to accommodate this. I'll think of a way to make it happen and get back to you.

Link to comment

The hotkey is a neat idea. I just added it to the workflow and push an update (1.3) to Packal.

 

Also, I rewrote getting / fetching mechanisms that should work with all versions. The configs did change a little bit, so you might have to reset them.

Thanks for updating!!!

 

For everyone else who has problems with the workflow after updating to v1.3:

 

  1. Open filter.php
  2. Delete the following code (line 91) 
    $trans = $alphred->get( $query, [ 'useragent' => 'alfredworkflow' ], 2419200, true );
    

    and replace it with the following code

  3. $ch = curl_init( $query );
    curl_setopt( $ch, CURLOPT_USERAGENT, 'alfredworkflow' );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $trans = curl_exec( $ch );
    curl_close( $ch );
    

    and you should be done.

Link to comment

 

Thanks for updating!!!

 

For everyone else who has problems with the workflow after updating to v1.3:

 

  1. Open filter.php
  2. Delete the following code (line 91) 
    $trans = $alphred->get( $query, [ 'useragent' => 'alfredworkflow' ], 2419200, true );
    

    and replace it with the following code

  3. $ch = curl_init( $query );
    curl_setopt( $ch, CURLOPT_USERAGENT, 'alfredworkflow' );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    $trans = curl_exec( $ch );
    curl_close( $ch );
    

    and you should be done.

 

 

Did you have problems with it? What were the errors? The Alphred model is nice because it will also cache the translations.

Link to comment

By "Cyrillic > latin" do you mean just transliterating the text? That should be easy to do without touching the Google API. As for speak, I'm not sure if that is possible. If you could help me figure out an API endpoint where I could get the sound file from, then it would be possible.

Link to comment

Okay, so I just uploaded a new version (1.3.2) that implements the two new options:

 

(1) Press cmd + enter on a translation to open the Google Translate page in the browser.

(2) Press opt + enter on a translation to listen to the audio of the translation.

 

Note: audio translations are supported to about 100 characters according to what I found on the net, so it probably won't work well if you try to listen to more than that. Also, you can't change voices for the translations (they're just audio files that are downloaded from Google).

Link to comment

Okay, so I just uploaded a new version (1.3.2) that implements the two new options:

 

(1) Press cmd + enter on a translation to open the Google Translate page in the browser.

(2) Press opt + enter on a translation to listen to the audio of the translation.

 

Note: audio translations are supported to about 100 characters according to what I found on the net, so it probably won't work well if you try to listen to more than that. Also, you can't change voices for the translations (they're just audio files that are downloaded from Google).

Sweet, this is awesome!

Link to comment

Wow, amazing. The audio 'listen' is a great addition! Thanks, that's going to help a lot. Fun!

And, yeah transliteration would be nice as well. :)

 

Well, transliterating should be just as easy as having php run code as simple as:

$args = json_decode( $argv[1], true );
print iconv( mb_detect_encoding( $args['translation'] ), "ISO-8859-1//TRANSLIT", $args['translation']);

but it doesn't seem to be working so well. I keep getting some esoteric errors about characters not found in the charsets (of which they are a part), so there might be a bug in the iconv builds, which has happened before. I'll play around a bit more and see if I can figure out a way around the problem without having to write a transliteration map.

Link to comment

It looks like Google may have changed the behavior of the API endpoint. Let me know if you run into problems. The debugger will probably say something like:

[STDERR: alfred.workflow.input.scriptfilter] [11:54:42][filter.php:103][ERROR] Request completely failed, and no cached data exists. cURL debug information follows:
[11:54:42][filter.php:103][ERROR] cURL error number: 22
[11:54:42][filter.php:103][ERROR] cURL error message: `The requested URL returned error: 503 Service Unavailable`.

If this error is widespread, then Google did change the API (or the API behavior).

Link to comment

The workflow works for me with the three default languages (greek, german and french). When I set new languages I get this error:

[STDERR: alfred.workflow.input.scriptfilter] [08:30:53][filter.php:103][ERROR] Request completely failed, and no cached data exists. cURL debug information follows:
[08:30:53][filter.php:103][ERROR] cURL error number: 22
[08:30:53][filter.php:103][ERROR] cURL error message: `The requested URL returned error: 503 Service Unavailable`.
[08:30:53][filter.php:103][ERROR] Request completely failed, and no cached data exists. cURL debug information follows:
[08:30:53][filter.php:103][ERROR] cURL error number: 22
[08:30:53][filter.php:103][ERROR] cURL error message: `The requested URL returned error: 503 Service Unavailable`.
[08:30:54][filter.php:103][ERROR] Request completely failed, and no cached data exists. cURL debug information follows:
[08:30:54][filter.php:103][ERROR] cURL error number: 22
[08:30:54][filter.php:103][ERROR] cURL error message: `The requested URL returned error: 503 Service Unavailable`.
[ERROR: alfred.workflow.input.scriptfilter] XML Parse Error 'The operation couldn’t be completed. (NSXMLParserErrorDomain error 4.)'. Row 1, Col 1: 'Document is empty' in XML:
[EDIT]
Edited by xilopaint
Link to comment
Quote

The workflow works for me with the three default languages (greek, german and french). When I set new languages I get this error:

 

It might just be that you're trying to translate strings that you already translated with the old ones. The newer version of the workflow caches the translations for some added speed, so it's probably just grabbing that information. I'm pretty sure that the unofficial API is dead, or, at the very least, I haven't figured out a way to get around it, and I haven't seen anyone else find a reliable way to do it either.

 

I did rewrite the workflow to use the Official (paid) translation API, but I haven't posted that anywhere. While it's cooler if it's free, using the API doesn't cost too much. It works out to $20 / 2 million characters, and you're just charged for what you use. I can post that version somewhere soon if people are interested.

Edited by deanishe
Replace real name
Link to comment

Okay, so if you look here: http://www.packal.org/workflow/translate-api, you'll find a new copy of this workflow that uses the paid translation API. It seems to work just fine with my tests. You'll have to get an API key from Google.

 

The workflow has a different bundleid, so it won't replace the old one, and the keyword for this one is just "tr".

 

I changed the behavior so that it outputs only one language at a time so that you don't pay for what you don't need. Also, the workflow is setup so that translations are cached for about a month, so if you keep translating the same word over and over, it won't actually hit the Google API after the first time.

 

Lastly, the "speak" action was taken out because that was also part of the unofficial API that is no longer working. I'll see if I can get that back in there sometime.

Link to comment

Okay, so if you look here: http://www.packal.org/workflow/translate-api, you'll find a new copy of this workflow that uses the paid translation API. It seems to work just fine with my tests. You'll have to get an API key from Google.

 

The workflow has a different bundleid, so it won't replace the old one, and the keyword for this one is just "tr".

 

I changed the behavior so that it outputs only one language at a time so that you don't pay for what you don't need. Also, the workflow is setup so that translations are cached for about a month, so if you keep translating the same word over and over, it won't actually hit the Google API after the first time.

 

Lastly, the "speak" action was taken out because that was also part of the unofficial API that is no longer working. I'll see if I can get that back in there sometime.

 

The language code for Brazilian Portuguese is wrong (pt-BZ instead pt-BR).

Link to comment

Okay, so if you look here: http://www.packal.org/workflow/translate-api, you'll find a new copy of this workflow that uses the paid translation API. It seems to work just fine with my tests. You'll have to get an API key from Google.

 

The workflow has a different bundleid, so it won't replace the old one, and the keyword for this one is just "tr".

 

I changed the behavior so that it outputs only one language at a time so that you don't pay for what you don't need. Also, the workflow is setup so that translations are cached for about a month, so if you keep translating the same word over and over, it won't actually hit the Google API after the first time.

 

Lastly, the "speak" action was taken out because that was also part of the unofficial API that is no longer working. I'll see if I can get that back in there sometime.

Mmm, that's a shame, need to go through some hassle to get this api key. If someone doesn't mind me joining theirs for 5 phrases a week, let me know ;)

Link to comment

Behavior seemed to be erratic and the debug was throwing errors.  I grabbed the latest and now it does not work at all.  

 
Starting debug for 'Translate'
 
[sTDERR: alfred.workflow.input.scriptfilter] [23:02:19][filter.php:103][ERROR] Request completely failed, and no cached data exists. cURL debug information follows:
[23:02:19][filter.php:103][ERROR] cURL error number: 22
[23:02:19][filter.php:103][ERROR] cURL error message: `The requested URL returned error: 503 Service Unavailable`.
[23:02:20][filter.php:103][ERROR] Request completely failed, and no cached data exists. cURL debug information follows:
[23:02:20][filter.php:103][ERROR] cURL error number: 22
[23:02:20][filter.php:103][ERROR] cURL error message: `The requested URL returned error: 503 Service Unavailable`.
[23:02:20][filter.php:103][ERROR] Request completely failed, and no cached data exists. cURL debug information follows:
[23:02:20][filter.php:103][ERROR] cURL error number: 22
[23:02:20][filter.php:103][ERROR] cURL error message: `The requested URL returned error: 503 Service Unavailable`.
[23:02:21][filter.php:103][ERROR] Request completely failed, and no cached data exists. cURL debug information follows:
[23:02:21][filter.php:103][ERROR] cURL error number: 22
[23:02:21][filter.php:103][ERROR] cURL error message: `The requested URL returned error: 503 Service Unavailable`.
[23:02:21][filter.php:103][ERROR] Request completely failed, and no cached data exists. cURL debug information follows:
[23:02:21][filter.php:103][ERROR] cURL error number: 22
[23:02:21][filter.php:103][ERROR] cURL error message: `The requested URL returned error: 503 Service Unavailable`.
[ERROR: alfred.workflow.input.scriptfilter] XML Parse Error 'The operation couldn’t be completed. (NSXMLParserErrorDomain error 4.)'. Row (null), Col (null): 'Document is empty' in XML:
Warning: Invalid argument supplied for foreach() in /Users/username/Dropbox/Sync/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.ECA96D19-FE23-4E7B-ACCC-936A70338790/functions.php on line 22
 
Warning: Invalid argument supplied for foreach() in /Users/username/Dropbox/Sync/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.ECA96D19-FE23-4E7B-ACCC-936A70338790/functions.php on line 22
 
Warning: Invalid argument supplied for foreach() in /Users/username/Dropbox/Sync/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.ECA96D19-FE23-4E7B-ACCC-936A70338790/functions.php on line 22
 
Warning: Invalid argument supplied for foreach() in /Users/username/Dropbox/Sync/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.ECA96D19-FE23-4E7B-ACCC-936A70338790/functions.php on line 22
 
Warning: Invalid argument supplied for foreach() in /Users/username/Dropbox/Sync/Alfred 2/Alfred.alfredpreferences/workflows/user.workflow.ECA96D19-FE23-4E7B-ACCC-936A70338790/functions.php on line 22
<?xml version="1.0" encoding="UTF-8"?>
<items>
 <item arg="{"action":"translate","translation":"","original":"sit","code":"en","listen":""}" valid="yes">
  <title></title>
  <subtitle>English: sit</subtitle>
  <icon>icons/en.png</icon>
  <subtitle mod="cmd">Open in Web Browser</subtitle>
  <subtitle mod="alt">Listen to Translation</subtitle>
 </item>
 <item arg="{"action":"translate","translation":"","original":"sit","code":"es","listen":""}" valid="yes">
  <title></title>
  <subtitle>Spanish: sit</subtitle>
  <icon>icons/es.png</icon>
  <subtitle mod="cmd">Open in Web Browser</subtitle>
  <subtitle mod="alt">Listen to Translation</subtitle>
 </item>
 <item arg="{"action":"translate","translation":"","original":"sit","code":"it","listen":""}" valid="yes">
  <title></title>
  <subtitle>Italian: sit</subtitle>
  <icon>icons/it.png</icon>
  <subtitle mod="cmd">Open in Web Browser</subtitle>
  <subtitle mod="alt">Listen to Translation</subtitle>
 </item>
 <item arg="{"action":"translate","translation":"","original":"sit","code":"fr","listen":""}" valid="yes">
  <title></title>
  <subtitle>French: sit</subtitle>
  <icon>icons/fr.png</icon>
  <subtitle mod="cmd">Open in Web Browser</subtitle>
  <subtitle mod="alt">Listen to Translation</subtitle>
 </item>
 <item arg="{"action":"translate","translation":"","original":"sit","code":"de","listen":""}" valid="yes">
  <title></title>
  <subtitle>German: sit</subtitle>
  <icon>icons/de.png</icon>
  <subtitle mod="cmd">Open in Web Browser</subtitle>
  <subtitle mod="alt">Listen to Translation</subtitle>
 </item>

 

</items>
Edited by mikedvzo
Link to comment

 

Behavior seemed to be erratic and the debug was throwing errors.  I grabbed the latest and now it does not work at all.  

 
Starting debug for 'Translate'

 

Yes. This behavior is expected.

 

If you read back through the last few messages in this thread, you'll see that Google shutdown the API that the Translate workflow relied on, so there is nothing that I can do to fix this. Instead, you can download the Translate API workflow that does basically the same thing, but, instead, it uses the official Google Translate API (which is a paid API, although it is dirt cheap).

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...