Class: AbAdmin::I18nTools::ModelTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/ab_admin/i18n_tools/model_translator.rb

Constant Summary collapse

IGNORE_COLUMNS =
%w(id reset_password_sent_at remember_created_at current_sign_in_at confirmation_token
reset_password_token password_salt failed_attempts)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModelTranslator

Returns a new instance of ModelTranslator


8
9
10
11
12
# File 'lib/ab_admin/i18n_tools/model_translator.rb', line 8

def initialize
  @locales = Globalize.available_locales
  @models = AbAdmin.translate_models.map{|m| m.constantize }
  @models_i18n_hash = {}
end

Class Method Details

.i18n_models!Object


60
61
62
63
64
# File 'lib/ab_admin/i18n_tools/model_translator.rb', line 60

def self.i18n_models!
  model_translator = new
  model_translator.make_hash
  model_translator.write_yaml
end

Instance Method Details

#ha(model, attr, locale) ⇒ Object


49
50
51
# File 'lib/ab_admin/i18n_tools/model_translator.rb', line 49

def ha(model, attr, locale)
  model.human_attribute_name(attr, locale: locale)
end

#make_hashObject


14
15
16
17
18
19
20
21
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
# File 'lib/ab_admin/i18n_tools/model_translator.rb', line 14

def make_hash
  @locales.each do |locale|
    I18n.with_locale(locale) do
      @models_i18n_hash[locale] = {'activerecord' => {'attributes' => {}, 'models' => {}}}

      models_hash = @models.each_with_object({}) do |model, h|
        model_i18n = {
            'zero' => model.model_name.human(count: 0),
            'one' => model.model_name.human(count: 1),
            'few' => (model.model_name.human(count: 2) rescue model.model_name.human(count: 1)),
            'many' => (model.model_name.human(count: 9) rescue model.model_name.human(count: 1)),
            'other' => (model.model_name.human(count: 9) rescue model.model_name.human(count: 1))
        }
        @models_i18n_hash[locale]['activerecord']['models'][model.model_name.i18n_key.to_s]= model_i18n
        attributes = model.columns.map(&:name)
        attributes.concat(model.translated_attribute_names.map(&:to_s)) if model.try!(:translates?)
        attributes.reject! { |el| IGNORE_COLUMNS.include?(el) }
        h[model.model_name.i18n_key.to_s] = attributes.each_with_object({}) do |attr, o|
          o[attr] = ha(model, attr, locale).presence || attr
          model.reflect_on_all_associations.map(&:name).map(&:to_s).reject { |a| a =~ /^translation/ }.each do |assoc|
            o[assoc] = ha(model, assoc, locale)
          end
          if model.new.respond_to?("#{attr}_#{locale.to_s}".to_sym)
            @locales.each do |locale_1|
              attr_suffix = I18n.t(locale_1, scope: [:attrs], default: locale_1.to_s.humanize)
              o["#{attr}_#{locale_1.to_s}"] = "#{ha(model, attr, locale)} (#{attr_suffix})"
            end
          end
        end.sort.to_h
      end
      @models_i18n_hash[locale]['activerecord']['attributes'] = models_hash
    end
  end
end

#write_yamlObject


53
54
55
56
57
58
# File 'lib/ab_admin/i18n_tools/model_translator.rb', line 53

def write_yaml
  locale_dir = Rails.root.join('config/locales')
  @locales.each do |locale|
    Locator.save(locale_dir.join("#{locale}.attrs.yml"), {locale.to_s => @models_i18n_hash[locale]})
  end
end