Core

/app/util/plural.go (892 B)

 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
package util

import (
"fmt"
"unicode"

"github.com/gertd/go-pluralize"
)

var plrl *pluralize.Client

func plrlSvc() {
if plrl == nil {
plrl = pluralize.NewClient()
}
}

func StringToPlural(s string) string {
plrlSvc()
ret := plrl.Plural(s)
if len(ret) < 3 {
return ret
}
if ret[len(ret)-1] == 'S' {
runes := []rune(ret)
c2 := runes[len(runes)-2]
c3 := runes[len(runes)-3]
if unicode.IsUpper(c2) && unicode.IsUpper(c3) {
runes[len(runes)-1] = 's'
ret = string(runes)
}
}
return ret
}

func StringToSingular(s string) string {
plrlSvc()
return plrl.Singular(s)
}

func StringForms(s string) (string, string) {
return StringToSingular(s), StringToPlural(s)
}

func StringPlural(count int, s string) string {
if count == 1 || count == -1 {
return fmt.Sprintf("%d %s", count, StringToSingular(s))
}
return fmt.Sprintf("%d %s", count, StringToPlural(s))
}