Module: Para::I18n::Model

Extended by:
ActiveSupport::Concern
Defined in:
lib/para/i18n/model.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#model_translationsObject



35
36
37
38
39
40
41
42
43
# File 'lib/para/i18n/model.rb', line 35

def model_translations
  unless respond_to?(:_translations)
    raise "The model #{ self.class.name } is not translatable. " +
          "Please run `rails g i18n_admin:translate #{ self.model_name.element }` " +
          "generator to create the model's migration."
  end

  self._translations ||= {}
end

#read_translated_attribute(field, locale = ::I18n.locale) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/para/i18n/model.rb', line 11

def read_translated_attribute(field, locale = ::I18n.locale)
  return read_plain_or_store_attribute(field) if locale == ::I18n.default_locale

  if model_translations[locale.to_s]
    if (translation = model_translations[locale.to_s][field.to_s])
      return translation
    end
  end

  # If no translation was returned, try to fallback to the next locale
  if (fallback_locale = Fallbacks.i18n_fallback_for(locale))
    read_translated_attribute(field, fallback_locale)
  end
end

#translation_for(locale) ⇒ Object



45
46
47
48
49
50
# File 'lib/para/i18n/model.rb', line 45

def translation_for(locale)
  case locale
  when I18n.default_locale then default_locale_translations
  else model_translations[locale.to_s] || {}
  end.with_indifferent_access
end

#write_translated_attribute(field, value, locale = ::I18n.locale) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/para/i18n/model.rb', line 26

def write_translated_attribute field, value, locale = ::I18n.locale
  return write_plain_or_store_attribute(field, value) if locale == ::I18n.default_locale

  # did not us ||= here to fix first assignation.
  # Did not investigate on why ||= does not work
  model_translations[locale.to_s] = {} unless model_translations[locale.to_s]
  model_translations[locale.to_s][field.to_s] = value
end