Class: MachineTranslation::Google

Inherits:
Object
  • Object
show all
Defined in:
lib/machine_translation/google.rb

Constant Summary collapse

GOOGLE_API_HOST =
'http://ajax.googleapis.com'
GOOGLE_API_PATH =
'/ajax/services/language/translate'
GOOGLE_API_VERSION =
'1.0'
GOOGLE_LANGUAGES_PAIRS =
{
  'af' => 'afrikaans',
  'sq' => 'albanian',
  'ar' => 'arabic',
  'be' => 'belarusian',
  'bg' => 'bulgarian',
  'zh-CN' => 'chinese simplified',
  'zh-TW' => 'chinese-traditional',
  'ca' => 'catalan',
  'hr' => 'croatian',
  'cs' => 'czech',
  'da' => 'danish',
  'nl' => 'dutch',
  'en' => 'english',
  'et' => 'estonian',
  'tl' => 'filipino',
  'fi' => 'finnish',
  'fr' => 'french',
  'gl' => 'galician',
  'de' => 'german',
  'el' => 'greek',
  'ht' => 'haitian creole',
  'he' => 'hebrew',
  'hi' => 'hindi',
  'hu' => 'hungarian',
  'is' => 'icelandic',
  'id' => 'indonesian',
  'ga' => 'irish',
  'it' => 'italian',
  'ja' => 'japanese',
  'ko' => 'korean',
  'lv' => 'latvian',
  'lt' => 'lithuanian',
  'mk' => 'macedonian',
  'ms' => 'malay',
  'mt' => 'maltese',
  'no' => 'norwegian',
  'fa' => 'persian',
  'pl' => 'polish',
  'pt' => 'portuguese',
  'ro' => 'romanian',
  'ru' => 'russian',
  'es' => 'spanish',
  'sr' => 'serbian',
  'sk' => 'slovak',
  'sl' => 'slovenian',
  'sw' => 'swahili',
  'sv' => 'swedish',
  'th' => 'thai',
  'tr' => 'turkish',
  'uk' => 'ukrainian',
  'vi' => 'vietnamese',
  'cy' => 'welsh',
  'yi' => 'yiddish'
}

Class Method Summary collapse

Class Method Details

.translate(from, to, content) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/machine_translation/google.rb', line 79

def translate(from, to, content)
  lp = "#{from}|#{to}"
  begin
    resource = RestClient::Resource.new(GOOGLE_API_HOST, { :headers => { 'Referer' => MachineTranslation.configuration.google_referer } })
    response = resource["#{GOOGLE_API_PATH}?v=#{GOOGLE_API_VERSION}&langpair=#{CGI::escape(lp)}&q=#{CGI::escape(content)}"].get
    case response.code
    when 200
      JSON.parse(response.body)['responseData']['translatedText']
    end
  end
end