diff --git a/lib/i18n.rb b/lib/i18n.rb index d3369704..0075a360 100644 --- a/lib/i18n.rb +++ b/lib/i18n.rb @@ -331,12 +331,11 @@ def with_locale(tmp_locale = nil) # keys are Symbols. def normalize_keys(locale, key, scope, separator = nil) separator ||= I18n.default_separator + locale = locale.to_sym if locale - [ - *normalize_key(locale, separator), - *normalize_key(scope, separator), - *normalize_key(key, separator) - ] + result = [locale] + result.concat(normalize_key(scope, separator)) if scope + result.concat(normalize_key(key, separator)) end # Returns true when the passed locale, which can be either a String or a diff --git a/lib/i18n/backend/base.rb b/lib/i18n/backend/base.rb index 217c3ff0..57756758 100644 --- a/lib/i18n/backend/base.rb +++ b/lib/i18n/backend/base.rb @@ -54,7 +54,7 @@ def translate(locale, key, options = EMPTY_HASH) end deep_interpolation = options[:deep_interpolation] - values = Utils.except(options, *RESERVED_KEYS) + values = Utils.except(options, *RESERVED_KEYS) unless options.empty? if values entry = if deep_interpolation deep_interpolate(locale, entry, values) @@ -123,7 +123,12 @@ def subtrees? # first translation that can be resolved. Otherwise it tries to resolve # the translation directly. def default(locale, object, subject, options = EMPTY_HASH) - options = options.reject { |key, value| key == :default } + if options.size == 1 && options.has_key?(:default) + options = {} + else + options = Utils.except(options, :default) + end + case subject when Array subject.each do |item| diff --git a/lib/i18n/interpolate/ruby.rb b/lib/i18n/interpolate/ruby.rb index dab8f0ed..bfe484e8 100644 --- a/lib/i18n/interpolate/ruby.rb +++ b/lib/i18n/interpolate/ruby.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + # heavily based on Masao Mutoh's gettext String interpolation extension # http://github.com/mutoh/gettext/blob/f6566738b981fe0952548c421042ad1e0cdfb31e/lib/gettext/core_ext/string.rb @@ -10,6 +12,11 @@ module I18n INTERPOLATION_PATTERN = Regexp.union(DEFAULT_INTERPOLATION_PATTERNS) deprecate_constant :INTERPOLATION_PATTERN + INTERPOLATION_PATTERNS_CACHE = Hash.new do |hash, patterns| + hash[patterns] = Regexp.union(patterns) + end + private_constant :INTERPOLATION_PATTERNS_CACHE + class << self # Return String or raises MissingInterpolationArgument exception. # Missing argument's logic is handled by I18n.config.missing_interpolation_argument_handler. @@ -20,7 +27,8 @@ def interpolate(string, values) end def interpolate_hash(string, values) - string.gsub(Regexp.union(config.interpolation_patterns)) do |match| + pattern = INTERPOLATION_PATTERNS_CACHE[config.interpolation_patterns] + string.gsub(pattern) do |match| if match == '%%' '%' else