Jump to content
rice.shawn

Google Translate (Languages Configurable)

Recommended Posts

Oh, yeah. The fix for the PHP version is only for 10.10.5 right now. Apple just used the wrong name for the dylib, and so when you tried to execute PHP it would send an error that said that it couldn't find that particular dylib. So, if any PHP command works (i.e. php -v), then it's fine.

 

So, now I'm even a bit more confused as to why it's working just fine for me but not working for either of you. I try to keep my system pretty stock so that it's easier to debug. And we're all using php 5.5.24. ( PHP 5.5.24 (cli) (built: May 19 2015 10:10:05) )

 

I had a thought that it might be geography based (because sometimes companies do that sort of thing), but that would have meant that the Ruby call would have died too.

 

I'll try to brainstorm more tomorrow as to what could be going wrong.

Share this post


Link to post

Hi,

 

I don't know if it helps, but I'm syncing my workflows with Dropbox between 2 macs. The funny thing is, that it works on one of them. On the other I get

[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: file_get_contents(http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=good+morngin+&hl=en&sl=auto&tl=de): failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable
 in /Users/foobar/Dropbox/ManualSync/Alfred/Alfred.alfredpreferences/workflows/user.workflow.67348FF0-C5F5-47B8-8E83-BB06E9815B44/filter.php on line 58

Warning: Invalid argument supplied for foreach() in /Users/foobar/Dropbox/ManualSync/Alfred/Alfred.alfredpreferences/workflows/user.workflow.67348FF0-C5F5-47B8-8E83-BB06E9815B44/filter.php on line 67

Warning: file_get_contents(http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=good+morngin+&hl=en&sl=auto&tl=en): failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable
 in /Users/foobar/Dropbox/ManualSync/Alfred/Alfred.alfredpreferences/workflows/user.workflow.67348FF0-C5F5-47B8-8E83-BB06E9815B44/filter.php on line 58

Warning: Invalid argument supplied for foreach() in /Users/foobar/Dropbox/ManualSync/Alfred/Alfred.alfredpreferences/workflows/user.workflow.67348FF0-C5F5-47B8-8E83-BB06E9815B44/filter.php on line 67

Warning: file_get_contents(http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=good+morngin+&hl=en&sl=auto&tl=fr): failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable
 in /Users/foobar/Dropbox/ManualSync/Alfred/Alfred.alfredpreferences/workflows/user.workflow.67348FF0-C5F5-47B8-8E83-BB06E9815B44/filter.php on line 58

Warning: Invalid argument supplied for foreach() in /Users/foobar/Dropbox/ManualSync/Alfred/Alfred.alfredpreferences/workflows/user.workflow.67348FF0-C5F5-47B8-8E83-BB06E9815B44/filter.php on line 67
<?xml version="1.0"?>
<items><item arg="" valid="yes"><arg></arg><title></title><subtitle>German: good morngin </subtitle><icon>icons/de.png</icon></item><item arg="" valid="yes"><arg></arg><title></title><subtitle>English: good morngin </subtitle><icon>icons/en.png</icon></item><item arg="" valid="yes"><arg></arg><title></title><subtitle>French: good morngin </subtitle><icon>icons/fr.png</icon></item></items>

is this a problem when syncing with Dropbox or has it something todo with PHP version?

Share this post


Link to post

is this a problem when syncing with Dropbox or has it something todo with PHP version?

 

I don't think that it has a problem to do with Dropbox because I also sync my workflows with Dropbox (and it works just fine for me). I really can't understand what's going on with this because I can't find the consistency in the problem yet to solve it.

 

Can you tell me the difference between the two Macs?

Share this post


Link to post

Hey, it quite working for me as well. I have php 5.6.10 that I loaded with Homebrew. I am using OS X 10.10.4. Not sure when it quit working as I have not used it in about a week or so. If everyone that is using it is out of the US, then it might be a location limitation on the API?

 

Testing says yes. I opened my VPN to a US location and the workflow works great. Therefore, this workflow will only work if you are in the US (or some short list of countries). It doesn't work here in Thailand anymore!  But, I can get it to work with my VPN.

Share this post


Link to post

Hey, it quite working for me as well. I have php 5.6.10 that I loaded with Homebrew. I am using OS X 10.10.4. Not sure when it quit working as I have not used it in about a week or so. If everyone that is using it is out of the US, then it might be a location limitation on the API?

 

Testing says yes. I opened my VPN to a US location and the workflow works great. Therefore, this workflow will only work if you are in the US (or some short list of countries). It doesn't work here in Thailand anymore!  But, I can get it to work with my VPN.

 

I can confirm that using a US IP address "solves" the issue (I am currently in Serbia)

Tried a German IP as well, didn't work though.

Share this post


Link to post

The geographic problem makes sense. I think that the API is really just something that Google uses, so maybe it's used mostly for internal reasons (but left open) for US servers. Maybe?

 

Let's confirm this more: can people outside of the US try the Ruby one-liner?

ruby -r open-uri -e 'puts open("http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=hello+there&hl=en&sl=auto&tl=no") { |io| io.read }'

If that one doesn't work, then I think we've found the problem.

Share this post


Link to post


{~/} what now... ruby -r open-uri -e 'puts open("http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=hello+there&hl=en&sl=auto&tl=no") { |io| io.read }'

[[["Hei der","hello there",,,10]],,"en",,,[["hello there",32000,[["Hei der",0,true,false],["hello there",0,true,false],["hei på",0,true,false],["hei på deg",0,true,false],["hei",0,true,false]],[[0,11]],"hello there",0,0]],0.036865726,,[["en"],,[0.036865726]],,,,,,[["Hello there!","there","hello"]]]

Share this post


Link to post

PHP?

 

I meant the original workflow. If you don't have it installed (I know you wrote your own solutions for this problem). Can you test this one-liner:

php -r "print_r( file_get_contents('http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=hello+there&hl=en&sl=auto&tl=no' ) );"

It should spit out the same garbled, pseudo-json.

Share this post


Link to post

I meant the original workflow. If you don't have it installed (I know you wrote your own solutions for this problem). Can you test this one-liner:

php -r "print_r( file_get_contents('http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=hello+there&hl=en&sl=auto&tl=no' ) );"
It should spit out the same garbled, pseudo-json.

 

 

 

❯ ruby -v
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin13.4.0]
❯ ruby -r open-uri -e 'puts open("http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=hello+there&hl=en&sl=auto&tl=no") { |io| io.read }'
[[["Hei der","hello there",,,10]],,"en",,,[["hello there",32000,[["Hei der",0,true,false],["hello there",0,true,false],["hei på",0,true,false],["hei på deg",0,true,false],["hei",0,true,false]],[[0,11]],"hello there",0,0]],0.036865726,,[["en"],,[0.036865726]],,,,,,[["Hello there!","there","hello"]]]

❯ php -v
PHP 5.6.10 (cli) (built: Jun 12 2015 10:07:24)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
❯ php -r "print_r( file_get_contents('http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=hello+there&hl=en&sl=auto&tl=no' ) );"
PHP Warning:  file_get_contents(http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=hello+there&hl=en&sl=auto&tl=no): failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable
 in Command line code on line 1

Warning: file_get_contents(http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=hello+there&hl=en&sl=auto&tl=no): failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable
 in Command line code on line 1

Share this post


Link to post

I meant the original workflow. If you don't have it installed (I know you wrote your own solutions for this problem). Can you test this one-liner:

php -r "print_r( file_get_contents('http://translate.google.com/translate_a/single?client=json&ie=UTF-8&oe=UTF-8&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&dt=at&q=hello+there&hl=en&sl=auto&tl=no' ) );"

It should spit out the same garbled, pseudo-json.

 

:D I don't even know which workflow we're talking about.

 

The PHP works, too.

Share this post


Link to post

:D I don't even know which workflow we're talking about.

 

Haha. It's obviously the one this thread is devoted to. Duh.

 

So, it looks like:

(1) both work for the part of Germany Dean is in (or at least where his VPN is registering him to be),

(2) the Ruby version works everywhere tested,

(3) the PHP version works only in certain places.

 

This is such a weird bug.

Share this post


Link to post

So, for everyone who this workflow isn't working for, try this:

 

Open filter.php in the workflow. Somewhere around line 60, you'll see the line

$trans = file_get_contents( $query );

Change that to

$trans = exec( "ruby -r open-uri -e 'puts open(\"{$query}\") { |io| io.read }'" );

Let me go ahead and make it clear that this is the hackiest hackery of a hack that I've ever put out there. But it seems like it should work until I get time to rewrite it.

 

Please get back to me and let me know if the hack works for you.

Share this post


Link to post

Well, yeah. But I'm not sure if this is the one I actually have installed. Hang on …

 

Nope, I have Martin's and it doesn't work.

 

(1) Yarp. VPN. I also tried my own IP address (also DE) and the Dutch VPN endpoint. All works! PHP & Ruby.

 

And yeah, I do have my own solution 'cos I pay for access to the official Google Translate API. If anyone uses a translation workflow regularly, it's well worth considering. It's ridiculously cheap. I often put a couple of hundred pages through it per month, and it still only comes to a couple of euros.

Share this post


Link to post

So, for everyone who this workflow isn't working for, try this:

 

Open filter.php in the workflow. Somewhere around line 60, you'll see the line

$trans = file_get_contents( $query );

Change that to

$trans = exec( "ruby -r open-uri -e 'puts open(\"{$query}\") { |io| io.read }'" );

Let me go ahead and make it clear that this is the hackiest hackery of a hack that I've ever put out there. But it seems like it should work until I get time to rewrite it.

 

Please get back to me and let me know if the hack works for you.

 

Have you thought about using libcurl and specifying a (possibly random) user agent?

Share this post


Link to post

Have you thought about using libcurl and specifying a (possibly random) user agent?

 

My initial tests with cURL failed, so I wrote it off, but I forgot about specifying the useragent. But, yeah. Setting the useragent worked.

 

Okay, here are two PHP based solutions:

 

If you're going the Alphred route (see above), then download that and put it in the root of your workflow directory. Then, in filter.php, add these two lines near the top:

require_once( 'Alphred.phar' );
$a = new Alphred;

And the new $trans line is:

$trans = $a->get( $query, [ 'useragent' => 'alfredworkflow' ], 2419200, true );

If you don't want to use Alphred, then add in this code to replace the $trans line:

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

The Alphred version has the added benefit of caching the result for about a month. Since they're translations, they shouldn't change. The caching speeds things up and shouldn't take much disk space.

Share this post


Link to post

Hey, with your mentioned fix 

$trans = exec( "ruby -r open-uri -e 'puts open(\"{$query}\") { |io| io.read }'" );

my workflow works now.

Maybe it is a problem with the PHP version? (PHP 5.6.12 (cli))

Share this post


Link to post

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

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...