-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrules.go
60 lines (51 loc) · 1.01 KB
/
rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package timeago
import (
"strings"
"github.com/SerhiiCho/timeago/v3/internal/utils"
)
type Rule struct {
Zero bool
One bool
Two bool
Few bool
Many bool
Other bool
}
var grammarRules = func(num int) map[string]*Rule {
end := num % 10
return map[string]*Rule{
"en,nl,de,es,fr": {
Zero: num == 0,
One: num == 1,
Two: num == 2,
Few: num > 1,
Many: num > 1,
},
"ru,uk,be": {
Zero: num == 0,
One: num == 1 || (num > 20 && end == 1),
Two: num == 2,
Few: (end == 2 || end == 3 || end == 4) && (num < 10 || num > 20),
Many: (num >= 5 && num <= 20) || end == 0 || end >= 5,
},
"zh,ja": {
Zero: true,
One: true,
Two: true,
Few: true,
Many: true,
},
}
}
func identifyGrammarRules(num int, lang string) (*Rule, error) {
rules := grammarRules(num)
if v, ok := rules[lang]; ok {
return v, nil
}
for langs, v := range rules {
if strings.Contains(langs, lang) {
return v, nil
}
}
return nil, utils.Errorf("Language '" + lang + "' not found")
}