module EasyTranslate::Translation

Public Instance Methods

translate(texts, options = {}, http_options = {}) click to toggle source

Translate text @param [String, Array] texts - A single string or set of strings to translate @option options [Fixnum] :batch_size - Maximum keys per request (optional, default 100) @option options [Fixnum] :concurrency - Maximum concurrent requests (optional, default 4) @option options [String, Symbol] :source - The source language (optional) @option options [String, Symbol] :target - The target language (required) @option options [Boolean] :html - Whether or not the supplied string is HTML (optional) @return [String, Array] Translated text or texts

# File lib/easy_translate/translation.rb, line 19
def translate(texts, options = {}, http_options = {})
  threaded_process(:request_translations, texts, options, http_options)
end

Private Instance Methods

request_translations(texts, options = {}, http_options = {}) click to toggle source

Perform a single request to translate texts @param [Array] texts - Texts to translate @option options [String, Symbol] :source - The source language (optional) @option options [String, Symbol] :target - The target language (required) @option options [Boolean] :html - Whether or not the supplied string is HTML (optional) @return [String, Array] Translated text or texts

# File lib/easy_translate/translation.rb, line 31
def request_translations(texts, options = {}, http_options = {})
  request = TranslationRequest.new(texts, options, http_options)
  # Turn the response into an array of translations
  raw = request.perform_raw
  JSON.parse(raw)['data']['translations'].map do |res|
    raw_translation = res['translatedText']
    CGI.unescapeHTML(raw_translation)
  end
end