| 1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
 | module I18n
  class << self
    def translate_with_fallback key, options={}, original=nil
      options[:locale] ||= I18n.locale
      begin
        self.translate_without_fallback(key, {raise: true}.update(options))
      rescue => e
        split = key.to_s.split('.')
        if split.size <= 2
          translate_without_fallback original || key, options
        else
          v = split.pop
          v2 = split.pop
          split.pop if v2 == "default"
          split << "default" << v
          new_key = split.join('.')
          translate_with_fallback new_key, options, original || key
        end
      end
    end
    alias_method_chain :translate, :fallback
    alias_method :t, :translate
  end
  def self.tc(key, params={})
    self.t('label_with_colon', label: key.t(params)).html_safe
  end
  def self.tmf(key, params={})
    model, col = key.split "."
    begin
      self.t "activerecord.attributes.#{key}", {raise: true}.update(params)
    rescue
      begin
        self.t "activerecord.attributes.common.#{col}", {raise: true}.update(params)
      rescue
        begin
          self.t "simple_form.labels.#{key}", {raise: true}.update(params)
        rescue
          "activerecord.attributes.#{key}".t params
        end
      end
    end
  end
  def self.tmfc(key, params={})
    self.t('label_with_colon', label: self.tmf(key, params)).html_safe
  end
  def self.missing_keys_logger
    @@my_logger ||= Logger.new("#{Rails.root}/log/missing_keys.log")
  end
  def self.log_missing_key key, params={}
    missing_keys_logger.info "key: '#{key}', locale: '#{I18n.locale}', params: #{params}"
  end
  def self.t_with_default(key, params={})
    begin
      self.t(key, {raise: true}.update(params))
    rescue
      if Rails.env.development?
        log_missing_key key, params
        "<span class='label label-danger' title='#{self.t(key, params)}'>!</span>#{key.split('.').last}".html_safe
      else
        key.split('.').last
      end
    end
  end
end
module EnhancedI18n
  def t(params={})
    I18n.t_with_default(self, params)
  end
  def tc(params={})
    I18n.tc(self, params)
  end
  def tmf(params={})
    I18n.tmf(self, params)
  end
  def tmfc(params={})
    I18n.tmfc(self, params)
  end
end
module EnhancedTimeI18n
  def l(params={})
    I18n.l(self, params)
  end
end
class Symbol
  include EnhancedI18n
end
class String
  include EnhancedI18n
end
class Time
  include EnhancedTimeI18n
end
class DateTime
  include EnhancedTimeI18n
end
class Date
  include EnhancedTimeI18n
end
module EnhancedModelI18n
  # Human name of the class (plural)
  def t opts={}
    "activerecord.models.#{i18n_key}".t({count: 2}.update(opts))
  end
  # Human name of the class (singular)
  def ts opts={}
    self.t({count: 1}.update(opts))
  end
  # Human name of the class (with comma)
  def tc(params={})
    I18n.tc(i18n_key, params)
  end
  # Human name of the attribute
  def tmf(attribute, params={})
    I18n.tmf "#{i18n_key}.#{attribute}", params
  end
  # Translate the given action on the model, with default
  def t_action(action, params={})
    key = case action.to_sym
    when :create
      :new
    when :update
      :edit
    else
      action
    end
    begin
      I18n.translate_without_fallback "#{i18n_key.pluralize}.#{key}.title", ({raise: true}.update(params))
    rescue
      I18n.translate_without_fallback "#{key}.title", params
    end
  end
  private
  def i18n_key
    model_name.to_s.underscore.gsub('/', '_')
  end
end
class ActiveRecord::Base
  extend EnhancedModelI18n
end
 |