module Trimmer::I18n

Constants

MERGER

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

Public Instance Methods

filter(translations, scopes) click to toggle source

Filter translations according to the specified scope.

# 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) click to toggle source
# File lib/trimmer/i18n.rb, line 6
def raise_all_exceptions(*args)
  raise args.first.to_exception
end
to_hash(options = {}) click to toggle source

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”

# 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
translations() click to toggle source

Initialize and return translations

# 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) { || ... } click to toggle source
# 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_fallbacks() { || ... } click to toggle source
# 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

Private Instance Methods

translate_without_fallbacks() click to toggle source
# File lib/trimmer/i18n.rb, line 93
def translate_without_fallbacks
  method_name = RUBY_VERSION =~ /1\.9/ ? :translate : 'translate'
  ancestor_module_with_translate = (::I18n.backend.class.included_modules - [::I18n::Backend::Fallbacks]).select {|m| m.instance_methods(false).include?(method_name) rescue false}
  ancestor_module_with_translate.first.instance_method(method_name) rescue nil
end