GithubHelp home page GithubHelp logo

Comments (2)

shanmuk avatar shanmuk commented on August 16, 2024

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.

relrod avatar relrod commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.