Class: Perfectline::T9n::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/t9n/util.rb

Class Method Summary collapse

Class Method Details

.google_translateObject

translate from google



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/t9n/util.rb', line 27

def self.google_translate
  I18n.available_locales.reject!{ |locale| I18n.default_locale?(locale) }.each do |locale|
    ::Translation.locale(locale).untranslated.each do |translation|

      unless self.needs_human_eyes?(translation.value)
        interpolation = translation.value.scan(/\{\{(.*?)\}\}/).flatten

        if interpolation.empty?
          translation.update_attribute(:value, Perfectline::T9n::GoogleTranslator.translate(translation.value, locale, I18n.default_locale))
        else
          placeholders = {}

          # sub out interpolations
          interpolation.each do |argument|
            placeholder = Time.now.to_i
            translation.value.gsub!("{{#{argument}}}", placeholder)
            placeholders[placeholder] = argument
          end

          # translate
          translated = Perfectline::T9n::GoogleTranslator.translate(translation.value, locale, I18n.default_locale)
          placeholders.each{|value, argument| translated.gsub!(value, "{{#{argument}}}")}

          translation.update_attribute(:value, translated)
        end
      end

    end
  end
end

.populateObject

sync non-default languages from the default one



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/t9n/util.rb', line 9

def self.populate
  ::Translation.locale(I18n.default_locale).each do |t|
    ::Translation.available_locales.reject!{ |locale| I18n.default_locale?(locale) }.each do |locale|
      unless ::Translation.locale(locale).exists?({:key => t.key, :translatable_id => t.translatable_id, :translatable_type => t.translatable_type})
        ::Translation.create!(
                :locale             => locale,
                :key                => t.key,
                :value              => nil,
                :translatable_id    => t.translatable_id,
                :translatable_type  => t.translatable_type
        )
        puts "Translation::Populate created a new translation in :#{locale} with key '#{t.key}'"
      end
    end
  end
end