-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathi18n.go
99 lines (86 loc) · 2.14 KB
/
i18n.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package i18n
import (
"io"
"github.com/Xuanwo/go-locale"
log "github.com/sirupsen/logrus"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
var p *message.Printer
func newMatcher(t []language.Tag) *matcher {
tags := &matcher{make(map[language.Tag]int)}
for i, tag := range t {
ct, err := language.All.Canonicalize(tag)
if err != nil {
ct = tag
}
tags.index[ct] = i
}
return tags
}
type matcher struct {
index map[language.Tag]int
}
func (m matcher) Match(want ...language.Tag) (language.Tag, int, language.Confidence) {
for _, t := range want {
ct, err := language.All.Canonicalize(t)
if err != nil {
ct = t
}
conf := language.Exact
for {
if index, ok := m.index[ct]; ok {
return ct, index, conf
}
if ct == language.Und {
break
}
ct = ct.Parent()
conf = language.High
}
}
return language.Und, 0, language.No
}
var supported = newMatcher([]language.Tag{
language.AmericanEnglish,
language.English,
language.SimplifiedChinese,
language.Chinese,
})
// Init will init i18n support via input language.
func Init(lang language.Tag) {
tag, _, _ := supported.Match(lang)
switch tag {
case language.AmericanEnglish, language.English:
initEnUS(lang)
case language.SimplifiedChinese, language.Chinese:
initZhCN(lang)
default:
initEnUS(lang)
}
}
// Fprintf is like fmt.Fprintf, but using language-specific formatting.
func Fprintf(w io.Writer, key message.Reference, a ...interface{}) (n int, err error) {
return p.Fprintf(w, key, a...)
}
// Printf is like fmt.Printf, but using language-specific formatting.
func Printf(format string, a ...interface{}) {
_, _ = p.Printf(format, a...)
}
// Sprintf formats according to a format specifier and returns the resulting string.
func Sprintf(format string, a ...interface{}) string {
return p.Sprintf(format, a...)
}
// Sprint is like fmt.Sprint, but using language-specific formatting.
func Sprint(a ...interface{}) string {
return p.Sprint(a...)
}
func init() {
tag, err := locale.Detect()
if err != nil {
log.Errorf("locale detect failed: [%v], use en_us instead", err)
tag = language.AmericanEnglish
}
Init(tag)
p = message.NewPrinter(tag)
}