Module: Trimmer::I18n

Defined in:
lib/trimmer/i18n.rb

Constant Summary collapse

MERGER =

deep_merge by Stefan Rusterholz, see www.ruby-forum.com/topic/142809

proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &MERGER) : v2 }

Instance Method Summary collapse

Instance Method Details

#deep_merge!(target, hash) ⇒ Object

:nodoc:



87
88
89
# File 'lib/trimmer/i18n.rb', line 87

def deep_merge!(target, hash) # :nodoc:
  target.merge!(hash, &MERGER)
end

#filter(translations, scopes) ⇒ Object

Filter translations according to the specified scope.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/trimmer/i18n.rb', line 61

def filter(translations, scopes)
  scopes = scopes.split(".") if scopes.is_a?(String)
  scopes = scopes.clone
  scope = scopes.shift

  if scope == "*"
    results = {}
    translations.each do |scope, translations|
      tmp = scopes.empty? ? translations : filter(translations, scopes)
      results[scope.to_sym] = tmp unless tmp.nil?
    end
    return results
  elsif translations.has_key?(scope.to_sym)
    return {scope.to_sym => scopes.empty? ? translations[scope.to_sym] : filter(translations[scope.to_sym], scopes)}
  end
  nil
end

#raise_all_exceptions(*args) ⇒ Object



6
7
8
# File 'lib/trimmer/i18n.rb', line 6

def raise_all_exceptions(*args)
  raise args.first.to_exception
end

#scoped_translations(scopes) ⇒ Object

:nodoc:



50
51
52
53
54
55
56
57
58
# File 'lib/trimmer/i18n.rb', line 50

def scoped_translations(scopes) # :nodoc:
  result = {}

  [scopes].flatten.each do |scope|
    deep_merge! result, filter(translations, scope)
  end

  result
end

#to_hash(options = {}) ⇒ Object

Exports translations from the I18n backend to a Hash :locale. If specified, will dump only translations for the given locale. :only. If specified, will dump only keys that match the pattern. “*.date”



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/trimmer/i18n.rb', line 34

def to_hash options = {}
  options.reverse_merge!(:only => "*")

  if options[:only] == "*"
    data = translations
  else
    data = scoped_translations options[:only]
  end

  if options[:locale]
    data[options[:locale].to_sym]
  else
    data
  end
end

#translationsObject

Initialize and return translations



80
81
82
83
84
85
# File 'lib/trimmer/i18n.rb', line 80

def translations
  self.backend.instance_eval do
    init_translations unless initialized?
    translations
  end
end

#with_exception_handler(tmp_exception_handler = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/trimmer/i18n.rb', line 10

def with_exception_handler(tmp_exception_handler = nil)
  if tmp_exception_handler
    current_exception_handler = self.exception_handler
    self.exception_handler    = tmp_exception_handler
  end
  yield
ensure
  self.exception_handler = current_exception_handler if tmp_exception_handler
end

#without_fallbacksObject



20
21
22
23
24
25
26
# File 'lib/trimmer/i18n.rb', line 20

def without_fallbacks
  current_translate_method = ::I18n.backend.method(:translate)
  ::I18n.backend.class.send(:define_method, 'translate', translate_without_fallbacks)
  yield
ensure
  ::I18n.backend.class.send(:remove_method, 'translate')
end