relrod / bing_translator-gem Goto Github PK
View Code? Open in Web Editor NEWA simple Ruby interface to Bing's translation API.
Home Page: https://rubygems.org/gems/bing_translator
License: MIT License
A simple Ruby interface to Bing's translation API.
Home Page: https://rubygems.org/gems/bing_translator
License: MIT License
This is a great gem, by the way.
The balance method does not seem to be available.
audio = translator.speak "#{bing_target}", :language => :fr, :format => 'audio/mp3', :options => 'MaxQuality'
File.open(audio_path, 'wb') { |f| f.write audio }
log("processed: #{bing_target}, balance: #{translator.balance}")
Error (via delayed_job log)
NoMethodError: undefined method `balance' for #<BingTranslator:0x00000004dab4d0>
BingTranslator::Exception: (a:DeserializationFailed) The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'Translate'. The maximum string content length quota (30720) has been exceeded while reading XML data. This quota may be increased by changing the MaxStringContentLength property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 128, position 113.
from /Users/nguyenchiencong/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/bing_translator-4.3.0/lib/bing_translator.rb:133:in `rescue in result'
Getting nil class error when trying to translate some text. Stacktrace points to this line:
https://github.com/relrod/bing_translator-gem/blob/4.5.0/lib/bing_translator.rb#L174
As ruby novice I can't sort out the error I receive when using the basic code shown in the documentation. I was hoping someone could point me in the right direction
require 'bing_translator'
auth = YAML.load_file('bing_translate.yml')
translator = BingTranslator.new(auth['client_id'],auth['client_secret'],false,auth['azure_key'])
#This works great so I'm authorized and talking to the API
puts translator.balance
#This code here kicks out the exception
spanish = translator.translate('Hello. This will be translated!', :from => 'en', :to => 'es')
puts spanish
undefined method 'headers' for HTTP:Module (BingTranslator::Exception)
from /usr/local/rvm/gems/ruby-2.2.1/gems/bing_translator-4.4.1/lib/bing_translator.rb:158:in 'result
from /usr/local/rvm/gems/ruby-2.2.1/gems/bing_translator-4.4.1/lib/bing_translator.rb:45:in 'translate'
Firstly - whoah, this is an immense gem, thanks for putting it together!
Secondly, given that (assuming devs wish to stick with free translations :-) ), would it be possible to return the number of available translations left per month? This would be useful to monitor usage - not that I expect to reach 2,000,000 characters, but it might be a useful feature.
I recognise this feature might not be available in the API, so this might not be possible.
Getting an error when attempting to use the translate method. I can create a translator and get my balance but the actual translate method returns this error:
BingTranslator::Exception: bad URI(is not URI?): {:host=>"api.microsofttranslator.com", :path=>"/V2/soap.svc", :port=>"80", :query=>"wsdl", :scheme=>"http", :headers=>{}, :body=>nil, :ssl_verify_peer=>true}
I am processing a large number of words and using this brilliant gem to generate an audio recording for each. My code uses a delayed_job process to run.
I keep getting this error below - then if I restart it runs ok for a while before hitting it again. I guess it could be my code also but have you seen this before?
BingTranslator::Exception: getaddrinfo: Name or service not known
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/bing_translator-4.3.0/lib/bing_translator.rb:133:in `rescue in result'
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/bing_translator-4.3.0/lib/bing_translator.rb:127:in `result'
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/bing_translator-4.3.0/lib/bing_translator.rb:70:in `speak'
from /home/fluent/fluent/app/workers/bing.rb:37:in `block in lookup_words'
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/activerecord-4.1.0/lib/active_record/relation/delegation.rb:46:in `each'
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/activerecord-4.1.0/lib/active_record/relation/delegation.rb:46:in `each'
from /home/fluent/fluent/app/workers/bing.rb:21:in `lookup_words'
from (irb):7
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands/console.rb:90:in `start'
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands/console.rb:9:in `start'
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/fluent/.rvm/gems/ruby-2.1.2@rails-stripe-membership-saas/gems/railties-4.1.0/lib/rails/commands.rb:17:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
This is the codeblock (below). I assume the error is coming from Bing itself... but I don't understand what the issue is.
# Public: performs actual request to Bing Translator SOAP API
def result(action, params = {}, &block)
# Specify SOAP namespace in tag names (see https://github.com/savonrb/savon/issues/340 )
params = Hash[params.map{|k,v| ["v2:#{k}", v]}]
begin
soap_client.call(action, message: params, &block)
rescue AuthenticationException
raise
rescue StandardError => e
# Keep old behaviour: raise only internal Exception class
raise Exception, e.message
end
end
Hey there,
maybe not the best place but I cannot find any other. First of all: Thanks for the gem, makes my life a lot easier dealing with the bing API. Now, in an application I want to mock the response coming from the service in order to write some some tests for a wrapper I've built around your gem. Is there any possiblity to do that without setting up a test API key calling the API every time I'm running a bing-related test?
best and thanks,
Andi
Hi, I followed all the steps in the documentation but I still keep getting this SSL Error. Is there any workaround for this?
spanish_trans.rb:4: warning: already initialized constant OpenSSL::SSL::VERIFY_PEER
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bing_translator-4.4.1/lib/bing_translator.rb:164:in `rescue in result': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (BingTranslator::Exception)
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bing_translator-4.4.1/lib/bing_translator.rb:158:in `result'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/bing_translator-4.4.1/lib/bing_translator.rb:45:in `translate'
from spanish_trans.rb:12:in `<main>'
Hello Rick! @relrod
Thanks for creating this. Do you have a list of all the abbreviations for the languages?
Also, can this easily be used to offer a 'translate' button in a forum, and when pressed, it asks which language you want it translated into, and then it translates the text?
Thanks!
-Monroe
Hi @relrod , this gem looks really helpful to my app. However I ran into an error
Bundler could not find compatible versions for gem "savon":
In snapshot (Gemfile.lock):
savon (= 2.11.2)
In Gemfile:
savon (~> 2.11.0)
bing_translator (= 5.2.0) was resolved to 5.2.0, which depends on
savon (~> 2.10.0)
It seems not compatible with savon 2.11.0 version or higher.
Thank you
Hey @relrod , this gem looks really useful.
I tried this out but I ran into the following error, inspite of the fact that I am already on the latest version of the rubyntlm
.
[4] pry(main)> spanish = translator.translate('Hello. This will be translated!', :from => 'en', :to => 'es')
ArgumentError: Invalid version of rubyntlm. Please use v0.3.2+.
from /Users/eklavya/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/httpi-2.4.2/lib/httpi/adapter/net_http.rb:63:in `check_net_ntlm_version!'
translator = BingTranslator.new(your_client_id, your_client_secret)
translator.detect(' ')
this would raise a NoMethodError: undefined method
to_sym' for nil:NilClass`, we should properly handle this either with a custom error or return a symbol for an error.
right now the work around I have is to make sure the phrase is not blank, but a better error would definitely be handier than a NoMethodError
Hey guys, I ran into a Problem trying to translate a text.
translator = BingTranslator.new('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx')
translator.translate('test', from: :de, to: :en)
returns
BingTranslator::Exception: (s:Client) ArgumentException: The received token is of incorrect token type. : ID=0261.V2_Soap.Translate.2BE299C8
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/bing_translator-5.0.0/lib/bing_translator.rb:152:in `rescue in result'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/bing_translator-5.0.0/lib/bing_translator.rb:150:in `result'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/bing_translator-5.0.0/lib/bing_translator.rb:38:in `translate'
from (irb):16
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/console.rb:110:in `start'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/console.rb:9:in `start'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `require'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:274:in `require'
from /Users/dochlast/projects/buddy/bin/rails:9:in `<top (required)>'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/dochlast/.rvm/gems/ruby-2.3.3/gems/activesupport-4.2.8/lib/active_support/dependencies.rb:268:in `load'
from /Users/dochlast/.rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/dochlast/.rvm/rubies/ruby-2.3.3/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
Any1 else running into this issue? How can we solve this? And is Azure really a SOAP Endpoint, not a REST Endpoint? And why are specs failing?
The CI is failing on every test.
1) BingTranslator translates text
Failure/Error: result = translator.translate message_en, :from => :en, :to => :ru
BingTranslator::Exception:
(s:Client) ArgumentException: The received token is of incorrect token type. : ID=0243.V2_Soap.Translate.440CE7CC
# ./lib/bing_translator.rb:152:in `rescue in result'
# ./lib/bing_translator.rb:150:in `result'
# ./lib/bing_translator.rb:38:in `translate'
# ./spec/bing_translator_spec.rb:19:in `block (2 levels) in <top (required)>'
Is the Bing key missing from the CI or is there some other issue?
As the bing API is providing translate array method its better to have one method for array to avoid bulk API calls.
http://msdn.microsoft.com/en-us/library/ff512407.aspx
A sampling of the methods that the API provides to the developer are listed below:
Translate — Translates text from one language to another
TranslateArray — Translates an array of texts from one language to another
Speak (Text to Speech) — Returns audio of the text spoken in the desired language
Detect — Detects the language of the string
Detect Array — Detects the language of an array of strings
GetTranslations — Returns an array of alternative translations for the text
GetTranslationsArray — Returns an array of alternative translations for a text array
GetLanguageNames, GetLanguagesForSpeak, GetLanguagesForTranslate — Return a list of languages supported by name, for audio and by code
AddTranslation, AddTranslationArray — Allows developers to add their own translations to the Microsoft Translator infrastructure.
Hey guys,
I'm not sure if you saw the emails Microsoft was sending out to their customers, but they' re going to discontinue the service of Bing translator V2 on April 30, 2019.
I'm wondering if you plan on getting this reflected in the Gem and switch to V3? I'm happy to help!
Best,
Kamil
Hi @relrod , I was using this gem and looks very nice. But how to know if request was fails by Bing API ? Thank you
hopefully I implemented something incorrectly and you can correct me.
I'm seeing carriage returns stripped from translations.
e.g.
t=BingTranslator.new(ENV.fetch('BING_TRANSLATE_APPID', nil), ENV.fetch('BING_TRANSLATE_SECRET', nil))
s =
"<script>alert(‘hi’)</script>\r\n\r\nAlso, here's a paragraph\r\nIf you’re clear about your position, click one of the icons below (hover over the decision buttons for a description of what each one means). \r\nYou’ll be prompted to make a short statement about the reason for your decision. This makes it easy to see a summary of what everyone thinks and why. You can change your mind and edit your decision freely until the proposal closes."
t.translate(s, to: 'fr') =
"<script>alert(‘hi’)</script>Aussi, voici un paragraphe, si vous êtes clair sur votre poste, cliquez sur une des icônes ci-dessous (survolez les boutons de décision pour obtenir une description de ce que signifie chacun d'eux). \nVous serez invité à faire une brève déclaration sur la raison de votre décision. Il est facile de voir un résumé de ce que tout le monde pense et pourquoi. Vous pouvez changer d'avis et modifier votre décision librement jusqu'à ce que la proposition ferme."
The 5.0.0 tag which uses the new COGNITIVE_SUBSCRIPTION_KEY isn't released. Is there plans to release this? The docs or page are useless as 4.6.0 seems obsolete.
irb(main):004:0> p translator.translate 'Hello. This will be translated!', :from => 'en', :to => 'es'
warning: peer certificate won't be verified in this SSL session
Nokogiri::XML::XPath::SyntaxError: Undefined namespace prefix: //xmlns:string
from /usr/lib/ruby/gems/1.8/gems/nokogiri-1.5.4/lib/nokogiri/xml/node.rb:159:in evaluate' from /usr/lib/ruby/gems/1.8/gems/nokogiri-1.5.4/lib/nokogiri/xml/node.rb:159:in
xpath'
from /usr/lib/ruby/gems/1.8/gems/nokogiri-1.5.4/lib/nokogiri/xml/node.rb:150:in map' from /usr/lib/ruby/gems/1.8/gems/nokogiri-1.5.4/lib/nokogiri/xml/node.rb:150:in
xpath'
from /usr/lib/ruby/gems/1.8/gems/bing_translator-2.0.0/lib/bing_translator.rb:41:in `translate'
from (irb):4
from �:0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.