class Glossync::MissingTranslationHandler::GoogleTranslate

This handler attempts to translate the given string to the target language using the Google Translate API

Public Class Methods

new(key) click to toggle source

@param key the key for the Google Translate API

# File lib/glossync/missing_translation_handler.rb, line 31
def initialize(key)
  @api_key = key
  @cache = {}
end

Public Instance Methods

translate(text, to) click to toggle source

Translate text to the to locale @param text the text to translate @param to the locale to translate to @return [String] the translated text or an error message

# File lib/glossync/missing_translation_handler.rb, line 40
def translate(text, to)
  EasyTranslate.translate(
    text,
    from: Glossync.options[:base_locale],
    to: to,
    key: @api_key
  )
rescue SocketError
  "failed to connect to google translate api"
end