~ubuntu-branches/ubuntu/vivid/ruby-i18n/vivid

« back to all changes in this revision

Viewing changes to lib/i18n/backend/fallbacks.rb

  • Committer: Package Import Robot
  • Author(s): Praveen Arimbrathodiyil
  • Date: 2013-06-03 19:20:31 UTC
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: package-import@ubuntu.com-20130603192031-qk11as87x3cotd1n
Tags: upstream-0.6.1
ImportĀ upstreamĀ versionĀ 0.6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
      # the given options. If it does not find any result for any of the
32
32
      # locales it will then throw MissingTranslation as usual.
33
33
      #
34
 
      # The default option takes precedence over fallback locales
35
 
      # only when it's a Symbol. When the default contains a String or a Proc
 
34
      # The default option takes precedence over fallback locales only when
 
35
      # it's a Symbol. When the default contains a String, Proc or Hash
36
36
      # it is evaluated last after all the fallback locales have been tried.
37
37
      def translate(locale, key, options = {})
38
38
        return super if options[:fallback]
39
 
        default = extract_string_or_lambda_default!(options) if options[:default]
 
39
        default = extract_non_symbol_default!(options) if options[:default]
40
40
 
41
41
        options[:fallback] = true
42
42
        I18n.fallbacks[locale].each do |fallback|
51
51
        throw(:exception, I18n::MissingTranslation.new(locale, key, options))
52
52
      end
53
53
 
54
 
      def extract_string_or_lambda_default!(options)
 
54
      def extract_non_symbol_default!(options)
55
55
        defaults = [options[:default]].flatten
56
 
        if index = find_first_string_or_lambda_default(defaults)
57
 
          options[:default] = defaults[0, index]
58
 
          defaults[index]
 
56
        first_non_symbol_default = defaults.detect{|default| !default.is_a?(Symbol)}
 
57
        if first_non_symbol_default
 
58
          options[:default] = defaults[0, defaults.index(first_non_symbol_default)]
59
59
        end
 
60
        return first_non_symbol_default
60
61
      end
61
62
 
62
 
      def find_first_string_or_lambda_default(defaults)
63
 
        defaults.each_with_index { |default, ix| return ix if String === default || Proc === default }
64
 
        nil
65
 
      end
66
63
    end
67
64
  end
68
65
end