Class: YamlTranslator::Adapters::GoogleTranslateAdapter

Inherits:
BaseAdapter
  • Object
show all
Defined in:
lib/yaml-translator/adapters/google_translate_adapter.rb

Instance Method Summary collapse

Methods inherited from BaseAdapter

#default_options

Constructor Details

#initialize(api_key = nil) ⇒ GoogleTranslateAdapter

Returns a new instance of GoogleTranslateAdapter.



6
7
8
# File 'lib/yaml-translator/adapters/google_translate_adapter.rb', line 6

def initialize(api_key=nil)
  @api_key = api_key
end

Instance Method Details

#translate(values, options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/yaml-translator/adapters/google_translate_adapter.rb', line 9

def translate(values, options={})
  keys = []
  texts = []
  opts = default_options.merge(options)

  values.each_with_index do |(key, text)|
    keys << key # 0: a.b, 1: a.b.c
    texts << text # 0: a, 1: b
  end
  translated_texts = EasyTranslate.translate(texts, :to => opts[:to], :key => @api_key)
  Hash[keys.zip(translated_texts)]
end