feature/plural: compute valid forms for a language
Needed for plural.Select.
Change-Id: I52ccca45ec3622d7ac000da021a16b1efcdf08ba
Reviewed-on: https://go-review.googlesource.com/41090
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
diff --git a/feature/plural/plural.go b/feature/plural/plural.go
index 41958c6..2b4cfe3 100644
--- a/feature/plural/plural.go
+++ b/feature/plural/plural.go
@@ -124,6 +124,22 @@
return matchPlural(p, index, n, f, scale)
}
+func validForms(p *Rules, t language.Tag) (forms []Form) {
+ index, _ := language.CompactIndex(t)
+ offset := p.langToIndex[index]
+ rules := p.rules[p.index[offset]:p.index[offset+1]]
+
+ forms = append(forms, Other)
+ last := Other
+ for _, r := range rules {
+ if cat := Form(r.cat & formMask); cat != andNext && last != cat {
+ forms = append(forms, cat)
+ last = cat
+ }
+ }
+ return forms
+}
+
func (p *Rules) matchComponents(t language.Tag, n, f, scale int) Form {
index, _ := language.CompactIndex(t)
return matchPlural(p, index, n, f, scale)
diff --git a/feature/plural/plural_test.go b/feature/plural/plural_test.go
index 9c75f6b..e5524c5 100644
--- a/feature/plural/plural_test.go
+++ b/feature/plural/plural_test.go
@@ -6,6 +6,7 @@
import (
"fmt"
+ "reflect"
"strconv"
"strings"
"testing"
@@ -65,6 +66,25 @@
return b
}
+func TestValidForms(t *testing.T) {
+ testCases := []struct {
+ tag language.Tag
+ want []Form
+ }{
+ {language.AmericanEnglish, []Form{Other, One}},
+ {language.Portuguese, []Form{Other, One}},
+ {language.Latvian, []Form{Other, Zero, One}},
+ {language.Arabic, []Form{Other, Zero, One, Two, Few, Many}},
+ {language.Russian, []Form{Other, One, Few, Many}},
+ }
+ for _, tc := range testCases {
+ got := validForms(cardinal, tc.tag)
+ if !reflect.DeepEqual(got, tc.want) {
+ t.Errorf("validForms(%v): got %v; want %v", tc.tag, got, tc.want)
+ }
+ }
+}
+
func TestOrdinal(t *testing.T) {
testPlurals(t, Ordinal, ordinalTests)
}