Skip to content

Commit

Permalink
Merge pull request #570 from bagilevi/fix-argument-error-in-fallbacks
Browse files Browse the repository at this point in the history
Fix ArgumentError when Fallbacks#map used as in Hash
  • Loading branch information
radar committed Jan 26, 2022
2 parents 2d4ee9e + 7551ea4 commit 07e1e83
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/i18n/locale/fallbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ def [](locale)
super || store(locale, compute(locale))
end

def map(mappings)
mappings.each do |from, to|
from, to = from.to_sym, Array(to)
to.each do |_to|
@map[from] ||= []
@map[from] << _to.to_sym
def map(*args, &block)
if args.count == 1 && !block_given?
mappings = args.first
mappings.each do |from, to|
from, to = from.to_sym, Array(to)
to.each do |_to|
@map[from] ||= []
@map[from] << _to.to_sym
end
end
else
@map.map(*args, &block)
end
end

Expand Down
12 changes: 12 additions & 0 deletions test/locale/fallbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,15 @@ def setup
end
end
end

class I18nFallbacksHashCompatibilityTest < I18n::TestCase
def setup
super
@fallbacks = Fallbacks.new(:'en-US', :"de-AT" => :"de-DE")
end

test "map is compatible with Hash#map" do
result = @fallbacks.map { |key, value| [key, value] }
assert_equal([[:"de-AT", [:"de-DE"]]], result)
end
end

0 comments on commit 07e1e83

Please sign in to comment.