Module: Goobalize3

Defined in:
lib/goobalize3.rb,
lib/goobalize3/version.rb

Constant Summary collapse

VERSION =
'0.2.0'

Instance Method Summary collapse

Instance Method Details

#translate(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/goobalize3.rb', line 6

def translate(*args)
  if self.class.translates?
    cur_locale = I18n.locale
    targets = I18n.available_locales - [I18n.locale]
    args.map!{|l| l.to_s.downcase.to_sym}
    targets = targets & args unless args.empty?
    targets.each do |locale|
      autotranslated_attributes = {}
      self.class.translated_attribute_names.each do |attr_name|
        begin
          if defined?(GOOBALIZE_SERVICE) && GOOBALIZE_SERVICE.to_sym == :google
            autotranslated_attributes[attr_name] = GoogleTranslate.perform(:source => cur_locale, :target => locale, :q => self.send(attr_name, cur_locale))
          else
            autotranslated_attributes[attr_name] = BingTranslate.perform(:source => cur_locale, :target => locale, :q => self.send(attr_name, cur_locale))
          end
        rescue GoobalizeTranslateError => e
          Rails.logger.error(e.to_s)
        end
      end
      I18n.locale = locale
      self.update_attributes(autotranslated_attributes)
    end
    I18n.locale = cur_locale
  end
end