Module: EasyTranslate::Translation

Included in:
EasyTranslate
Defined in:
lib/easy_translate/translation.rb

Defined Under Namespace

Classes: TranslationRequest

Instance Method Summary collapse

Instance Method Details

#translate(texts, options = {}, http_options = {}) ⇒ String, Array

Translate text

Parameters:

  • texts (String, Array)
    • A single string or set of strings to translate

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :source (String, Symbol)
    • The source language (optional)

  • :target (String, Symbol)
    • The target language (required)

  • :html (Boolean)
    • Whether or not the supplied string is HTML (optional)

Returns:

  • (String, Array)

    Translated text or texts



14
15
16
17
18
19
20
21
22
23
# File 'lib/easy_translate/translation.rb', line 14

def translate(texts, options = {}, http_options={})
  request = TranslationRequest.new(texts, options, http_options)
  # Turn the response into an array of translations 
  raw = request.perform_raw
  translations = JSON.parse(raw)['data']['translations'].map do |res|
    res['translatedText']
  end
  # And then return, if they only asked for one, only give one back
  request.multi? ? translations : translations.first
end