Module: I18n

Defined in:
lib/rails_i18n_extended/i18n_overrides.rb

Class Method Summary collapse

Class Method Details

.alert(key, params = {}) ⇒ Object



69
70
71
72
73
# File 'lib/rails_i18n_extended/i18n_overrides.rb', line 69

def alert(key, params={})
  scope = [:alert] + params[:scope].to_a

  flash(key, params.update(scope: scope))
end

.attribute_key(obj, attr) ⇒ Object



49
50
51
# File 'lib/rails_i18n_extended/i18n_overrides.rb', line 49

def attribute_key(obj, attr)
  "activerecord.attributes.#{model_key(obj)}.#{attr}"
end

.custom_attribute_key(obj, attr) ⇒ Object



53
54
55
# File 'lib/rails_i18n_extended/i18n_overrides.rb', line 53

def custom_attribute_key(obj, attr)
  "activerecord.custom_display_attributes.#{model_key(obj)}.#{attr}"
end

.error(key, params = {}) ⇒ Object



75
76
77
78
79
# File 'lib/rails_i18n_extended/i18n_overrides.rb', line 75

def error(key, params={})
  scope = [:error] + params[:scope].to_a

  flash(key, params.update(scope: scope))
end

.flash(key, params = {}) ⇒ Object



57
58
59
60
61
# File 'lib/rails_i18n_extended/i18n_overrides.rb', line 57

def flash(key, params={})
  scope = [:flash] + params[:scope].to_a

  t(key, params.update(scope: scope))
end

.model_key(obj) ⇒ Object



45
46
47
# File 'lib/rails_i18n_extended/i18n_overrides.rb', line 45

def model_key(obj)
  obj.model_name.to_s.underscore.gsub('/', '_')
end

.notice(key, params = {}) ⇒ Object



63
64
65
66
67
# File 'lib/rails_i18n_extended/i18n_overrides.rb', line 63

def notice(key, params={})
  scope = [:notice] + params[:scope].to_a

  flash(key, params.update(scope: scope))
end

.translate_with_default(key, options = {}, original = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rails_i18n_extended/i18n_overrides.rb', line 6

def translate_with_default key, options={}, original=nil
  defaults = []
  chain = key.to_s.split('.')
  unless chain[-2] == 'defaults'
    last = chain.pop
    while chain.size > 0
      chain.pop
      defaults << (chain + ['defaults', last]).join('.').to_sym
    end
  end

  translate_without_default(key, **{default: defaults}.update(options))
end

.translate_with_fallback(key = nil, **options) ⇒ Object Also known as: translate



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rails_i18n_extended/i18n_overrides.rb', line 24

def translate_with_fallback(key = nil, **options)
  locale = options[:locale] || I18n.locale
  intended_to_raise = options[:raise]

  translate_without_fallback(key, options.dup.update(raise: true))
rescue I18n::MissingTranslationData => e
  raise if locale == I18n.default_locale && intended_to_raise

  begin
    return translate_without_fallback(key, options.dup.update(locale: I18n.default_locale, raise: true))
  rescue I18n::MissingTranslationData
    raise e if intended_to_raise
  end

  translate_without_fallback(key, options)
end