Skip to content

Commit

Permalink
Merge branch 'master' into fix-interpolate-same-object
Browse files Browse the repository at this point in the history
  • Loading branch information
radar authored Feb 28, 2023
2 parents 439c23a + f75520a commit 54dd66f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
9 changes: 4 additions & 5 deletions lib/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions lib/i18n/backend/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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|
Expand Down
13 changes: 9 additions & 4 deletions lib/i18n/interpolate/ruby.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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.
Expand All @@ -20,10 +27,8 @@ def interpolate(string, values)
end

def interpolate_hash(string, values)
interpolated = false
interpolated_string = string.gsub(Regexp.union(config.interpolation_patterns)) do |match|
interpolated = true

pattern = INTERPOLATION_PATTERNS_CACHE[config.interpolation_patterns]
string.gsub(pattern) do |match|
if match == '%%'
'%'
else
Expand Down
16 changes: 6 additions & 10 deletions test/i18n_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -501,18 +501,14 @@ def call(exception, locale, key, options); key; end

test "can reserve a key" do
begin
reserved_keys_were = I18n::RESERVED_KEYS.dup
stub_const(I18n, :RESERVED_KEYS, []) do
I18n.reserve_key(:foo)
I18n.reserve_key("bar")

assert !I18n::RESERVED_KEYS.include?(:foo)
assert !I18n::RESERVED_KEYS.include?(:bar)

I18n.reserve_key(:foo)
I18n.reserve_key("bar")

assert I18n::RESERVED_KEYS.include?(:foo)
assert I18n::RESERVED_KEYS.include?(:bar)
assert I18n::RESERVED_KEYS.include?(:foo)
assert I18n::RESERVED_KEYS.include?(:bar)
end
ensure
I18n::RESERVED_KEYS = reserved_keys_were
I18n.instance_variable_set(:@reserved_keys_pattern, nil)
end
end
Expand Down
10 changes: 10 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ def store_translations(locale, data, options = I18n::EMPTY_HASH)
def locales_dir
File.dirname(__FILE__) + '/test_data/locales'
end

def stub_const(klass, constant, new_value)
old_value = klass.const_get(constant)
klass.send(:remove_const, constant)
klass.const_set(constant, new_value)
yield
ensure
klass.send(:remove_const, constant)
klass.const_set(constant, old_value)
end
end

class DummyRackApp
Expand Down

0 comments on commit 54dd66f

Please sign in to comment.