Permalink
Browse files

Allow mutually recursive mappings

Where translations are drawn from different sources which may be using
different codes for the same language it can be useful to have mappings
where two codes fallback to each other.

This patch allows that by ensuring that the recursion in the computation
of the mapping terminates instead of running out of stack.
  • Loading branch information...
1 parent 89ea337 commit 025d4127153f00506819df32404ec6830a227f43 @tomhughes tomhughes committed Dec 18, 2011
Showing with 15 additions and 3 deletions.
  1. +3 −3 lib/i18n/locale/fallbacks.rb
  2. +12 −0 test/locale/fallbacks_test.rb
@@ -82,10 +82,10 @@ def map(mappings)
protected
- def compute(tags, include_defaults = true)
+ def compute(tags, include_defaults = true, exclude = [])
result = Array(tags).collect do |tag|
- tags = I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym }
- tags.each { |_tag| tags += compute(@map[_tag]) if @map[_tag] }
+ tags = I18n::Locale::Tag.tag(tag).self_and_parents.map! { |t| t.to_sym } - exclude
+ tags.each { |_tag| tags += compute(@map[_tag], false, exclude + tags) if @map[_tag] }
tags
end.flatten
result.push(*defaults) if include_defaults
@@ -121,4 +121,16 @@ def setup
test "with a mapping :de => :en, :he => :en defined it [:he, :en] for :de" do
assert_equal [:he, :"en-US", :en], @fallbacks[:he]
end
+
+ # Test allowing mappings that fallback to each other
+
+ test "with :no => :nb, :nb => :no defined :no returns [:no, :nb, :en-US, :en]" do
+ @fallbacks.map(:no => :nb, :nb => :no)
+ assert_equal [:no, :nb, :"en-US", :en], @fallbacks[:no]
+ end
+
+ test "with :no => :nb, :nb => :no defined :nb returns [:nb, :no, :en-US, :en]" do
+ @fallbacks.map(:no => :nb, :nb => :no)
+ assert_equal [:nb, :no, :"en-US", :en], @fallbacks[:nb]
+ end
end

0 comments on commit 025d412

Please sign in to comment.