class GoogleTranslator

Class for the Google Translate service.

Public Class Methods

info() click to toggle source
# File lib/translators/google_translator.rb, line 54
def self.info
  File.open(Application.strings_dir + '/google_info.txt').read
end
name() click to toggle source
# File lib/translators/google_translator.rb, line 11
def self.name
  'Google Translate'
end

Public Instance Methods

langs() click to toggle source
# File lib/translators/google_translator.rb, line 19
def langs
  { 'af'  => 'Afrikaans',   'sq'  => 'Albanian',        'ar'  => 'Arabic',
    'hy'  => 'Armenian',    'az'  => 'Azerbaijani',     'eu'  => 'Basque',
    'be'  => 'Belarusian',  'bg'  => 'Bulgarian',       'ca'  => 'Catalan',
    'hr'  => 'Croatian',    'cs'  => 'Czech',           'et'  => 'Estonian',
    'da'  => 'Danish',      'nl'  => 'Dutch',           'en'  => 'English',
    'tl'  => 'Filipino',    'fi'  => 'Finnish',         'fr'  => 'French',
    'gl'  => 'Galician',    'ht'  => 'Haitian, Creole', 'is'  => 'Icelandic',
    'ka'  => 'Georgian',    'de'  => 'German',          'el'  => 'Greek',
    'iw'  => 'Hebrew',      'hi'  => 'Hindi',           'hu'  => 'Hungarian',
    'id'  => 'Indonesian',  'ga'  => 'Irish',           'it'  => 'Italian',
    'ko'  => 'Korean',      'la'  => 'Latin',           'lv'  => 'Latvian',
    'mk'  => 'Macedonian',  'ms'  => 'Malay',           'mt'  => 'Maltese',
    'fa'  => 'Persian',     'pl'  => 'Polish',          'pt'  => 'Portuguese',
    'ro'  => 'Romanian',    'sl'  => 'Slovenian',       'th'  => 'Thai',
    'ja'  => 'Japanese',    'lt'  => 'Lithuanian',      'no'  => 'Norwegian',
    'vi'  => 'Vietnamese',  'cy'  => 'Welsh',           'yi'  => 'Yiddish',
    'ru'  => 'Russian',     'sr'  => 'Serbian',         'sk'  => 'Slovak',
    'es'  => 'Spanish',     'sw'  => 'Swahili',         'sv'  => 'Swedish',
    'tr'  => 'Turkish',     'uk'  => 'Ukrainian',       'ur'  => 'Urdu',
    'zh-TW' => 'Chinese, (Traditional)', 'zh-CN' => 'Chinese, (Simplified)' }
end
provide_tts?() click to toggle source
# File lib/translators/google_translator.rb, line 15
def provide_tts?
  true
end
translate(text, source, target, options = {}) click to toggle source
# File lib/translators/google_translator.rb, line 42
def translate(text, source, target, options = {})
  fail "Unknown language code '#{source}'" unless langs.include?(source)
  fail "Unknown language code '#{target}'" unless langs.include?(target)

  if options[:tts]
    audio_file = StringUtil.tts_file_name(text, source, 'google')
    get_pronunciation(text, source, audio_file) unless File.exist?(audio_file)
  end
  json = JSON.parse(get_data(text, source, target, options))
  extract_translation(json)
end

Private Instance Methods

extract_translation(json) click to toggle source
# File lib/translators/google_translator.rb, line 60
def extract_translation(json)
  translation = ''
  if json.include?('sentences')
    json['sentences'].each do |entry|
      if entry.include?('trans')
        translation = entry['trans']
        break
      end
    end
  end
  translation
end