Class: GettextSimpleRails::I18nInjector

Inherits:
Object
  • Object
show all
Defined in:
lib/gettext_simple_rails/i18n_injector.rb

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ I18nInjector

Returns a new instance of I18nInjector.



2
3
4
5
# File 'lib/gettext_simple_rails/i18n_injector.rb', line 2

def initialize(args = {})
  @debug = args[:debug]
  @i18n_backend = I18n.config.backend
end

Instance Method Details

#inject_model_translations(gettext_simple) ⇒ Object



22
23
24
25
26
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gettext_simple_rails/i18n_injector.rb', line 22

def inject_model_translations(gettext_simple)
  GettextSimpleRails::ModelInspector.model_classes do |inspector|
    model = {}
    attributes = {}
    
    data = {
      "activerecord" => {
        "models" => {
          inspector.snake_name => model
        },
        "attributes" => {
          inspector.snake_name => attributes
        }
      }
    }
    
    I18n.available_locales.each do |locale|
      locale = locale.to_s
      
      unless gettext_simple.locale_exists?(locale)
        debug "Skipping locale because it hasn't been loaded into GettextSimple: #{locale}" if @debug
        next
      end
      
      debug "Loading locale: #{locale}" if @debug
      
      one_translation = gettext_simple.translate_with_locale(locale, inspector.gettext_key_one)
      if one_translation != inspector.gettext_key_one
        model["one"] = one_translation
      end
      
      other_translation = gettext_simple.translate_with_locale(locale, inspector.gettext_key_other)
      if other_translation != inspector.gettext_key_other
        model["other"] = other_translation
      end
      
      inspector.attributes do |attribute|
        attribute_translation = gettext_simple.translate_with_locale(locale, attribute.gettext_key)
        if attribute_translation != attribute.gettext_key
          debug "Translating #{inspector.snake_name}.#{attribute.name} to #{attribute_translation}" if @debug
          attributes[attribute.name.to_s] = attribute_translation
        else
          debug "Skipping #{inspector.snake_name}.#{attribute.name}" if @debug
        end
      end
      
      inspector.relationships do |name, reflection|
        relationship_translation = gettext_simple.translate_with_locale(locale, inspector.relationship_gettext_key(name))
        if relationship_translation != inspector.relationship_gettext_key(name)
          attributes[name.to_s] = relationship_translation
        end
      end
      
      @i18n_backend.store_translations(locale.to_sym, data)
    end
  end
end

#inject_translator_translations(gettext_simple) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gettext_simple_rails/i18n_injector.rb', line 7

def inject_translator_translations(gettext_simple)
  @translator_translations = {}
  
  GettextSimpleRails::Translators.load_all.each do |translator_data|
    translator = translator_data[:class].new
    next unless translator.detected?
    
    I18n.available_locales.each do |locale|
      next unless gettext_simple.locale_exists?(locale.to_s)
      locale = locale.to_s
      injector_recursive gettext_simple, locale, translator.translations
    end
  end
end