-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
It was breaking on locales with more than two segments (e.g., `zh-Hant-HK`) Beyond that, I pulled out the fallback code and exposed it so that downstream consumers can use it too. I structured it in a similar manner as `ruby-i18n`'s [`I18n::Locale::Fallbacks`](https://github.com/ruby-i18n/i18n/blob/535459ad1201e7cc97a071bd5d9f0e1d5406069e/lib/i18n/locale/fallbacks.rb). I also changed the keys and values in `ParentLocales` to be symbols, which simplifies the code. It's still not fully correct for locales that contain scripts (e.g., `ff-Adlm-GH`). That will be a different commit.
- Loading branch information
1 parent
e0576c2
commit 53f8ae2
Showing
6 changed files
with
55 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Cldr | ||
module Locale | ||
class Fallbacks < Hash | ||
def [](locale) | ||
defined_parents = Cldr::Export::Data::ParentLocales.new | ||
|
||
ancestry = [locale] | ||
loop do | ||
if defined_parents[ancestry.last] | ||
ancestry << defined_parents[ancestry.last] | ||
elsif I18n::Locale::Tag.tag(ancestry.last).parents.count > 0 | ||
ancestry << I18n::Locale::Tag.tag(ancestry.last).parents.first.to_sym | ||
else | ||
break | ||
end | ||
end | ||
ancestry << :root unless ancestry.last == :root | ||
store(locale, ancestry) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# encoding: utf-8 | ||
|
||
require File.expand_path(File.join(File.dirname(__FILE__) + '/../test_helper')) | ||
|
||
class TestFallbacks < Test::Unit::TestCase | ||
test "fallbacks does basic hyphen chopping" do | ||
assert_equal [:root], Cldr.fallbacks[:root] | ||
assert_equal [:en, :root], Cldr.fallbacks[:en] | ||
assert_equal [:"fr-CA", :fr, :root], Cldr.fallbacks[:"fr-CA"] | ||
assert_equal [:"zh-Hant-HK", :"zh-Hant", :root], Cldr.fallbacks[:"zh-Hant-HK"] | ||
end | ||
|
||
test "fallbacks observe explicit parent overrides" do | ||
assert_equal [:"az-Arab", :root], Cldr.fallbacks[:"az-Arab"] | ||
assert_equal [:"en-CH", :"en-150", :"en-001", :en, :root], Cldr.fallbacks[:"en-CH"] | ||
assert_equal [:"zh-Hant", :root], Cldr.fallbacks[:"zh-Hant"] | ||
end | ||
end |