Skip to content

Instantly share code, notes, and snippets.

@szabcsee
Last active October 13, 2015 23:08
Show Gist options
  • Save szabcsee/4270782 to your computer and use it in GitHub Desktop.
Save szabcsee/4270782 to your computer and use it in GitHub Desktop.
GoogleFish Ruby gem usage, how it works with Sinatra
get '/translate' do
doc = open(params[:url]) { |f| Hpricot (f) }
doc.search("//iframe").remove
doc.search("//script").remove
doc.search("//.headline_meta").remove
doc.search("//frameset").remove
article = (doc/".post").inner_html
google = GoogleFish.new('YOUR-API-KEY')
@result = google.translate( params[:language_from], params[:language_to], 'Good night, sleep tight')
erb :translated
end
@szabcsee
Copy link
Author

So that's how I use it and it works. I put the static text instead the article to save on the API consumption while testing. If I use HTML code in the text it gets removed. :html => true does not work, that throws error.
I tried :format => html but nothing happens. I would like the text to be translated with html code being left intact inside.

@szabcsee
Copy link
Author

google.translate( params[:language_from], params[:language_to], 'Good night, sleep tight', :format => 'html') that works

@seenmyfate
Copy link

The html: true option isn’t directly passed to the Google API, it’s internally converted to format=html - the second example you give here will not pass the format option to the API endpoint. If you could raise an issue (https://github.com/onthebeach/google_fish/issues) I'll take a look when I have some free time

@szabcsee
Copy link
Author

Thanks for the comment. I tried and now it seems to be working.

@szabcsee
Copy link
Author

Only does not work when I pass on the @Article variable in my program, but if I pass in a small text with html tags, it works fine.

@szabcsee
Copy link
Author

I tried to run it from irb and got these errors:

GoogleFish::Request::ApiError: GoogleFish::Request::ApiError
from /Users/szabcsee/.rvm/gems/ruby-1.9.3-p194/gems/google_fish-0.2.2/lib/google_fish.rb:61:in get' from /Users/szabcsee/.rvm/gems/ruby-1.9.3-p194/gems/google_fish-0.2.2/lib/google_fish.rb:34:inperform_translation'
from /Users/szabcsee/.rvm/gems/ruby-1.9.3-p194/gems/google_fish-0.2.2/lib/google_fish.rb:19:in request_translation' from /Users/szabcsee/.rvm/gems/ruby-1.9.3-p194/gems/google_fish-0.2.2/lib/google_fish.rb:12:intranslate'
from (irb):16
from /Users/szabcsee/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `

'
1.9.3-p194 :017 >

@duhanov
Copy link

duhanov commented Oct 20, 2014

I Have same error!

I tried to run it from irb and got these errors:

GoogleFish::Request::ApiError: GoogleFish::Request::ApiError
from /Users/szabcsee/.rvm/gems/ruby-1.9.3-p194/gems/google_fish-0.2.2/lib/google_fish.rb:61:in get' from /Users/szabcsee/.rvm/gems/ruby-1.9.3-p194/gems/google_fish-0.2.2/lib/google_fish.rb:34:inperform_translation'
from /Users/szabcsee/.rvm/gems/ruby-1.9.3-p194/gems/google_fish-0.2.2/lib/google_fish.rb:19:in request_translation' from /Users/szabcsee/.rvm/gems/ruby-1.9.3-p194/gems/google_fish-0.2.2/lib/google_fish.rb:12:intranslate'
from (irb):16
from /Users/szabcsee/.rvm/rubies/ruby-1.9.3-p194/bin/irb:16:in `'
1.9.3-p194 :017 >

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment