i18n_auto_scope.rb
893 Bytes
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
module I18nAutoScope
  extend ActiveSupport::Concern
  included do
    define_method :translate, I18n.method(:translate).to_proc unless self.respond_to? :translate
    alias_method_chain :translate, :auto_scope
    alias_method :t, :translate
  end
  DefaultScope = 'suppliers_plugin'
  # should be replaced on controller (e.g. controller)
  def i18n_scope
    DefaultScope
  end
  protected
  def translate_with_auto_scope key, options = {}
    # raise option is removed from hash, so reinsert each time
    options[:raise] = true
    translation = self.translate_without_auto_scope key, options rescue nil
    unless translation
      Array(i18n_scope).each do |scope|
        options[:scope] = scope
        options[:raise] = true
        return translation if (translation = self.translate_without_auto_scope key, options rescue nil)
      end
    end
    translation
  end
end