From 2553f7c2e7f13c245b8d32457371f4f3f81099a7 Mon Sep 17 00:00:00 2001 From: Nick Snyder Date: Mon, 28 May 2018 13:57:51 -0700 Subject: [PATCH] handle zh correctly --- v2/internal/plural/rules.go | 12 ++++++++---- v2/internal/plural/rules_test.go | 16 ++++++++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/v2/internal/plural/rules.go b/v2/internal/plural/rules.go index 373372a2..87eb8369 100644 --- a/v2/internal/plural/rules.go +++ b/v2/internal/plural/rules.go @@ -8,13 +8,17 @@ type Rules map[language.Tag]*Rule // Rule returns the closest matching plural rule for the language tag // or nil if no rule could be found. func (r Rules) Rule(tag language.Tag) *Rule { + t := tag for { - if rule := r[tag]; rule != nil { + if rule := r[t]; rule != nil { return rule } - tag = tag.Parent() - if tag.IsRoot() { - return nil + t = t.Parent() + if t.IsRoot() { + break } } + base, _ := tag.Base() + baseTag, _ := language.Parse(base.String()) + return r[baseTag] } diff --git a/v2/internal/plural/rules_test.go b/v2/internal/plural/rules_test.go index b7666d67..759241d8 100644 --- a/v2/internal/plural/rules_test.go +++ b/v2/internal/plural/rules_test.go @@ -49,6 +49,22 @@ func TestRules(t *testing.T) { tag: language.EuropeanPortuguese, rule: expectedRule, }, + { + name: "zh-Hans", + rules: Rules{ + language.Chinese: expectedRule, + }, + tag: language.SimplifiedChinese, + rule: expectedRule, + }, + { + name: "zh-Hant", + rules: Rules{ + language.Chinese: expectedRule, + }, + tag: language.TraditionalChinese, + rule: expectedRule, + }, } for _, testCase := range testCases {