summaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/language/pluralspec.go
diff options
context:
space:
mode:
authorCorey Hulen <corey@hulen.com>2016-01-19 13:33:57 -0600
committerCorey Hulen <corey@hulen.com>2016-01-19 13:33:57 -0600
commit36c5c46e24f745ee80b49f47363217fcb740ce53 (patch)
treed66189c2ba3c76b76488a3d0dd6ae210ddfc0096 /Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/language/pluralspec.go
parent80bfdf71f2ab7271dd199c61229fa2b8a7e0213c (diff)
parent4389571dedb9a68d801427c37ad971c8c488991f (diff)
downloadchat-36c5c46e24f745ee80b49f47363217fcb740ce53.tar.gz
chat-36c5c46e24f745ee80b49f47363217fcb740ce53.tar.bz2
chat-36c5c46e24f745ee80b49f47363217fcb740ce53.zip
Merge pull request #1921 from mattermost/PLT-7-server-libs
PLT-7 adding server side libraries
Diffstat (limited to 'Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/language/pluralspec.go')
-rw-r--r--Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/language/pluralspec.go74
1 files changed, 74 insertions, 0 deletions
diff --git a/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/language/pluralspec.go b/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/language/pluralspec.go
new file mode 100644
index 000000000..fc3522682
--- /dev/null
+++ b/Godeps/_workspace/src/github.com/nicksnyder/go-i18n/i18n/language/pluralspec.go
@@ -0,0 +1,74 @@
+package language
+
+import "strings"
+
+// PluralSpec defines the CLDR plural rules for a language.
+// http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html
+// http://unicode.org/reports/tr35/tr35-numbers.html#Operands
+type PluralSpec struct {
+ Plurals map[Plural]struct{}
+ PluralFunc func(*operands) Plural
+}
+
+var pluralSpecs = make(map[string]*PluralSpec)
+
+func normalizePluralSpecID(id string) string {
+ id = strings.Replace(id, "_", "-", -1)
+ id = strings.ToLower(id)
+ return id
+}
+
+func registerPluralSpec(ids []string, ps *PluralSpec) {
+ for _, id := range ids {
+ id = normalizePluralSpecID(id)
+ pluralSpecs[id] = ps
+ }
+}
+
+// Plural returns the plural category for number as defined by
+// the language's CLDR plural rules.
+func (ps *PluralSpec) Plural(number interface{}) (Plural, error) {
+ ops, err := newOperands(number)
+ if err != nil {
+ return Invalid, err
+ }
+ return ps.PluralFunc(ops), nil
+}
+
+// getPluralSpec returns the PluralSpec that matches the longest prefix of tag.
+// It returns nil if no PluralSpec matches tag.
+func getPluralSpec(tag string) *PluralSpec {
+ tag = NormalizeTag(tag)
+ subtag := tag
+ for {
+ if spec := pluralSpecs[subtag]; spec != nil {
+ return spec
+ }
+ end := strings.LastIndex(subtag, "-")
+ if end == -1 {
+ return nil
+ }
+ subtag = subtag[:end]
+ }
+}
+
+func newPluralSet(plurals ...Plural) map[Plural]struct{} {
+ set := make(map[Plural]struct{}, len(plurals))
+ for _, plural := range plurals {
+ set[plural] = struct{}{}
+ }
+ return set
+}
+
+func intInRange(i, from, to int64) bool {
+ return from <= i && i <= to
+}
+
+func intEqualsAny(i int64, any ...int64) bool {
+ for _, a := range any {
+ if i == a {
+ return true
+ }
+ }
+ return false
+}