Module: AbAdmin::I18nTools::GoogleTranslate

Defined in:
lib/ab_admin/i18n_tools/google_translate.rb

Class Method Summary collapse

Class Method Details

.t(text, from, to) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ab_admin/i18n_tools/google_translate.rb', line 7

def self.t(text, from, to)
  return '' if text.blank?
  return text if from == to
  base = 'https://www.googleapis.com/language/translate/v2'
  params = {
      key: configatron.else.retrieve(:google_api_key, ENV['GOOGLE_API_KEY']),
      format: 'html',
      source: from,
      target: to,
      q: text
  }
  response = RestClient.post(base, params, 'X-HTTP-Method-Override' => 'GET')

  if response.code == 200
    json = MultiJson.decode(response)
    json['data']['translations'][0]['translatedText']
  else
    raise StandardError, response.inspect
  end
end