Skip to content

Instantly share code, notes, and snippets.

@cpetschnig
Created January 11, 2012 13:32
Show Gist options
  • Select an option

  • Save cpetschnig/1594660 to your computer and use it in GitHub Desktop.

Select an option

Save cpetschnig/1594660 to your computer and use it in GitHub Desktop.

Revisions

  1. Christoph Petschnig revised this gist May 29, 2012. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions i18n_lookup.rb
    Original file line number Diff line number Diff line change
    @@ -10,14 +10,22 @@ def lookup(locale, key, scope = [], options = {})
    keys.inject(translations) do |result, _key|
    _key = _key.to_sym
    unless result.is_a?(Hash) && result.has_key?(_key)
    puts %|I18n: couldn't find "#{keys.map(&:to_s).join('.')}"\n (from #{caller.detect{|f| f !~ /i18n/}})}| # print key that was not found!
    print_debug_message(keys, caller) # print key that was not found!
    return nil
    end
    result = result[_key]
    result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
    result
    end
    end

    def print_debug_message(keys, caller)
    caller_line = caller.detect{|f| f !~ /i18n/}
    (Gem.paths.path.clone << ENV['HOME']).each { |base| caller_line.sub!(base, '...') }

    puts %|I18n: couldn't find "\e[1;33m#{keys.map(&:to_s).join('.')}\e[0;0m"\n (from #{caller_line})}|
    end

    end

    end
    end if Rails.env.development?
  2. Christoph Petschnig created this gist Jan 11, 2012.
    23 changes: 23 additions & 0 deletions i18n_lookup.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    class I18n::Backend::Simple
    module Implementation

    protected

    def lookup(locale, key, scope = [], options = {})
    init_translations unless initialized?
    keys = I18n.normalize_keys(locale, key, scope, options[:separator])

    keys.inject(translations) do |result, _key|
    _key = _key.to_sym
    unless result.is_a?(Hash) && result.has_key?(_key)
    puts %|I18n: couldn't find "#{keys.map(&:to_s).join('.')}"\n (from #{caller.detect{|f| f !~ /i18n/}})}| # print key that was not found!
    return nil
    end
    result = result[_key]
    result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
    result
    end
    end
    end

    end