Module: Common::GoogleTranslator

Included in:
WebStuff
Defined in:
lib/common/google_translator.rb

Class Method Summary collapse

Class Method Details

.translate(str, from_locale, to_locale) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/common/google_translator.rb', line 9

def self.translate(str, from_locale, to_locale)
  str = str.strip
  return "" if str.blank?
  params = {
    "client" => "t",
    "sl" => from_locale,
    "tl" => to_locale,
    "hl" => "en",
    "sc" => "2",
    "ie" => "UTF-8",
    "oe" => "UTF-8",
    "oc" => "1",
    "otf" => "2",
    "ssel" => "3",
    "tsel" => "6",
    "q" => str
  }
  result = agent.get("http://translate.google.com.vn/translate_a/t", params)
  json = JSON(result.body.match(/^\[(\[\[.*?\]\])/)[1])
  json[0][0]
end