Comments (2)
Hi,
I have added two new methods into the Bing translator class to make translate array to work.Here I am providing them If you find them as usefull keep in your gem.
def translate_array(array, params = {})
raise "Must provide :to." if params[:to].nil?
raise "provided an empty array.. #{array}" unless array.count > 0
params[:from] ||= "en"
params = {
'to' => CGI.escape(params[:to].to_s),
'texts' => array,
'category' => 'general',
'contentType' => 'text/plain',
'from' => CGI.escape(params[:from].to_s)
}
result = array_result @translate_array_uri, params
nokogiri_document = Nokogiri::XML::Document.parse( result.body )
hash = Hash.from_xml(nokogiri_document.to_s)
tr_array = []
hash["ArrayOfTranslateArrayResponse"]["TranslateArrayResponse"].each{|arr| tr_array << arr["TranslatedText"] }
tr_array
end
def array_result(uri, params={}, headers={})
get_access_token
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @skip_ssl_verify
end
xml_body1 = '<TranslateArrayRequest>
<AppId />
<From>'+params['from']+'</From>
<Options>
<Category xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<ContentType xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/plain</ContentType>
<ReservedFlags xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<State xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<Uri xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<User xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
</Options>
<Texts>'
xml_body2=''
params['texts'].each do |txt|
txt = txt.gsub("&","")
xml_body2+= '<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">'+txt+'</string>'
end
xml_body3 ='</Texts>
<To>'+params['to']+'</To>
</TranslateArrayRequest>'
xml_body = xml_body1+xml_body2+xml_body3
request = Net::HTTP::Post.new(uri.path)
request.add_field "Content-Type", "application/xml"
request.add_field 'Authorization',"Bearer #{@access_token['access_token']}"
request.add_field 'Content-length',1036
request.body = xml_body
results = http.request(request)
if results.response.code.to_i == 200
results
else
html = Nokogiri::HTML(results.body)
raise Exception, html.xpath("//text()").remove.map(&:to_s).join(' ')
end
end
And then its ready to use for translate an array,just fire it like below
spanish = translator.translate ['Hello.','This','will','be' ,'translated!'], :from => 'en', :to => 'es'
# without :from for auto language detection
spanish = translator.translate ['Hello.','This','will','be' ,'translated!'], :to => 'es'
Hope this helps!
from bing_translator-gem.
Hi, if this is still something you'd like, could you send it as a proper pull request against master
? I'm going to close this out for now to bump it out of my queue.
from bing_translator-gem.
Related Issues (20)
- Is there a quota imposed by MS? HOT 3
- \r \n are not preserved HOT 10
- balance method not available HOT 4
- getaddrinfo: Name or service not known HOT 2
- SSL Error while trying to use this gem HOT 1
- undefined method `headers' for HTTP:Module (BingTranslator::Exception) HOT 2
- bad URI(is not URI?) HOT 2
- raises a nil error when detecting white spaces
- Undefined method 'encoding' for nil:NilClass HOT 5
- How to test code using the bing_translator gem? HOT 1
- Are you releasing 5.0.0? HOT 1
- The received token is of incorrect token type. : ID=0261.V2_Soap.Translate.2BE299C8 HOT 1
- Detecting fails request
- Error with rubyntlm HOT 1
- Build is failing (missing key?) HOT 3
- Returning characters used/left per month HOT 1
- Error with savon version greater than 2.11.0 HOT 1
- Abbreviations? HOT 1
- Migration to API V3 HOT 16
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bing_translator-gem.