5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-10 11:00:27 +00:00
matterbridge/vendor/github.com/mattermost/go-i18n/i18n/language/pluralspec.go

76 lines
1.7 KiB
Go
Raw Normal View History

2016-04-10 21:39:38 +00:00
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{}
2020-08-09 22:29:54 +00:00
PluralFunc func(*Operands) Plural
2016-04-10 21:39:38 +00:00
}
var pluralSpecs = make(map[string]*PluralSpec)
func normalizePluralSpecID(id string) string {
id = strings.Replace(id, "_", "-", -1)
id = strings.ToLower(id)
return id
}
2020-08-09 22:29:54 +00:00
// RegisterPluralSpec registers a new plural spec for the language ids.
func RegisterPluralSpec(ids []string, ps *PluralSpec) {
2016-04-10 21:39:38 +00:00
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
}
2020-08-09 22:29:54 +00:00
// GetPluralSpec returns the PluralSpec that matches the longest prefix of tag.
2016-04-10 21:39:38 +00:00
// It returns nil if no PluralSpec matches tag.
2020-08-09 22:29:54 +00:00
func GetPluralSpec(tag string) *PluralSpec {
2016-04-10 21:39:38 +00:00
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
}