Module: I27r::GoogleTranslate

Defined in:
lib/generators/i18n/translation/lib/translator.rb

Instance Method Summary collapse

Instance Method Details

#_translate(word, lang) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/generators/i18n/translation/lib/translator.rb', line 7

def _translate(word, lang)
  w = CGI.escape ActiveSupport::Inflector.humanize(word)
  json = OpenURI.open_uri("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=#{w}&langpair=en%7C#{lang}").read
  result = if RUBY_VERSION >= '1.9'
    require 'json'
    ::JSON.parse json
  else
    ActiveSupport::JSON.decode(json)
  end
  if result['responseStatus'] == 200
    result['responseData']['translatedText']
  else
    raise TranslationError.new result.inspect
  end
end