Module: Babelphish::Translator

Defined in:
lib/babelphish/translator.rb

Class Method Summary collapse

Class Method Details

.detect_language(text) ⇒ Object

Sends a string to google to attempt to detect the language.

Returns an array indicating success/fail and the resulting data from google in a hash: “confidence”=>0.08594032, “isReliable”=>false


149
150
151
152
153
154
155
# File 'lib/babelphish/translator.rb', line 149

def detect_language(text)
  request = Babelphish.google_ajax_url + "detect?v=1.0&q=" + CGI.escape(text) 
  # send get request
  response = Net::HTTP.get_response( URI.parse( request ) )
  json = JSON.parse( response.body )
  [json['responseStatus'] == 200, json['responseData']]
end

.multiple_translate(text, tos, from = 'en', tries = 0) ⇒ Object

translate from the 'from' language into all available languages multiple strings and multiple languages

ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en|it&q=goodbye&langpair=en|fr results from google look like this: [{“responseData”:{“translatedText”:“ciao mondo”,“responseDetails”:null,“responseStatus”:200},revoir”,“responseDetails”:null,“responseStatus”:200}], “responseDetails”: null, “responseStatus”: 200}

One string into multiple languages ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=hello%20world&langpair=en|it&langpair=en|fr results from google look like this: [{“responseData”:{“translatedText”:“ciao mondo”,“responseDetails”:null,“responseStatus”:200},le Monde”,“responseDetails”:null,“responseStatus”:200}], “responseDetails”: null, “responseStatus”: 200}


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/babelphish/translator.rb', line 93

def multiple_translate(text, tos, from = 'en', tries = 0)
  return {} if text.to_s.strip.empty? # Google doesn't like it when you send them an empty string

  tos.each do |to|
    if !Babelphish::GoogleTranslate::LANGUAGES.include?(to)
      raise Exceptions::GoogleResponseError, "#{to} is not a valid Google Translate code.  Please be sure language codes are one of: #{Babelphish::GoogleTranslate::LANGUAGES.join(',')}"
    end
  end
  
  if Babelphish.api_version == 'v2'
    results = {}
    tos.each do |to|
      results[to] = translate(text, to, from)
    end
    results
  else
    base = Babelphish.google_ajax_url + 'translate'
    # assemble query params
    params = {
      :q => text,
      :v => 1.0
    }
    query = params.map{ |k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')

    tos.each do |to|
      query <<  "&langpair=" + CGI.escape("#{from}|#{to}")
    end

    response = Net::HTTP.get_response( URI.parse( "#{base}?#{query}" ) )
    json = JSON.parse( response.body )

    if json['responseStatus'] == 200
      results = {}
      if json['responseData'].is_a?(Array)
        json['responseData'].each_with_index do |data, index|
          results[tos[index]] = data['responseData']['translatedText']
        end
      else
        results[tos[0]] = json['responseData']['translatedText']
      end
      results
    else
      if tries <= Babelphish::MAX_RETRIES
        # Try again a few more times
        multiple_translate(text, tos, from, tries+=1)
      else
        raise Exceptions::GoogleResponseError, "A problem occured while translating.  #{response} -- #{response.body} -- From: #{from} -- Text: #{text}"
      end
    end
  end

end

.supported_languagesObject


157
158
159
# File 'lib/babelphish/translator.rb', line 157

def supported_languages
  Babelphish::GoogleTranslate::LANGUAGES
end

.translate(text, to, from = 'en', tries = 0) ⇒ Object

from: ruby.geraldbauer.ca/google-translation-api.html translate text from 'from' to 'to'


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/babelphish/translator.rb', line 8

def translate(text, to, from = 'en', tries = 0)
  return if to == from
  if text.is_a? Symbol 
     return text
  end
  if text.length > 1000  #actually the USI length limit is 2000
     text_now = ""
     text_rem = ""
     text.split(".").each do |text_chunk|
        if text_now.length + text_chunk.length < 1000
           text_now += (text_chunk + ".")
        else
           text_rem += (text_chunk + ".")
        end 
     end
     return translate(text_now, to, from) + "." + translate(text_rem, to, from)
  end
  
  base = Babelphish.google_ajax_url 
  if Babelphish.api_version == 'v2'
    # assemble query params
    params = {
      :source => "#{from}", 
      :target => "#{to}", 
      :q => text,
      :key => Babelphish.settings['api_key']
    }
  else
    base << 'translate' 
    # assemble query params
    params = {
      :langpair => "#{from}|#{to}", 
      :source => "#{from}", 
      :target => "#{to}", 
      :q => text,
      :v => 1.0  
    }
  end
  
  # send get request
  uri = URI.parse(base)
  uri.query = params.map{ |k,v| "#{k}=#{CGI.escape(v.to_s)}" }.join('&')
  http = Net::HTTP.new( uri.host, uri.port )
  http.use_ssl = true if uri.scheme == "https" # enable SSL/TLS
  
  # TODO kind of dangerous to turn off all verification. Should try to get a valid cert file at some point.
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  # cacert_file = File.join(File.expand_path("~"), "cacert.pem")
  # if File.exist?(cacert_file)
  #   http.ca_file = cacert_file
  # end
  
  response = nil
  
  http.start {|http| response = http.request_get(uri.request_uri) }
  
  if response.code == "200" 
    json = JSON.parse(response.body)
    if Babelphish.api_version == 'v2'
      json['data']['translations'][0]['translatedText']
    else
      json['responseData']['translatedText']
    end
  else
    if tries <= Babelphish::MAX_RETRIES
      # Try again a few more times
      translate(text, to, from, tries+=1)
    else
      raise Exceptions::GoogleResponseError, "A problem occured while translating from #{from} to #{to}.  #{response.body}"
    end
  end
end