Skip to content

Commit

Permalink
Rename extract_string_or_lambda_default! to `extract_non_symbol_def…
Browse files Browse the repository at this point in the history
…ault!` to be named as it acts.

Refactor `extract_non_symbol_default!` to be shorten and better readable with no performance hit.
  • Loading branch information
knapo committed Jul 10, 2011
1 parent 94f6aa1 commit efa436b
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions lib/i18n/backend/fallbacks.rb
Expand Up @@ -31,12 +31,12 @@ module Fallbacks
# the given options. If it does not find any result for any of the
# locales it will then throw MissingTranslation as usual.
#
# The default option takes precedence over fallback locales
# only when it's a Symbol. When the default contains a String or a Proc
# The default option takes precedence over fallback locales only when
# it's a Symbol. When the default contains a String, Proc or Hash
# it is evaluated last after all the fallback locales have been tried.
def translate(locale, key, options = {})
return super if options[:fallback]
default = extract_string_or_lambda_default!(options) if options[:default]
default = extract_non_symbol_default!(options) if options[:default]

options[:fallback] = true
I18n.fallbacks[locale].each do |fallback|
Expand All @@ -51,18 +51,15 @@ def translate(locale, key, options = {})
throw(:exception, I18n::MissingTranslation.new(locale, key, options))
end

def extract_string_or_lambda_default!(options)
def extract_non_symbol_default!(options)
defaults = [options[:default]].flatten
if index = find_first_string_or_lambda_default(defaults)
options[:default] = defaults[0, index]
defaults[index]
first_non_symbol_default = defaults.detect{|default| !default.is_a?(Symbol)}
if first_non_symbol_default
options[:default] = defaults[0, defaults.index(first_non_symbol_default)]
end
return first_non_symbol_default
end

def find_first_string_or_lambda_default(defaults)
defaults.each_with_index { |default, ix| return ix if default && !default.is_a?(Symbol) }
nil
end
end
end
end

0 comments on commit efa436b

Please sign in to comment.