forked from pibigstar/go-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.go
60 lines (56 loc) · 1.37 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
package i18n
import (
"encoding/json"
"fmt"
"github.com/nicksnyder/go-i18n/v2/i18n"
"golang.org/x/text/language"
"gopkg.in/yaml.v2"
)
func LoadYaml() {
bundle := i18n.NewBundle(language.Chinese)
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
_, err := bundle.LoadMessageFile("zh-CN.yaml")
if err != nil {
panic(err)
}
localizer := i18n.NewLocalizer(bundle, "zh-CN")
info := localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "200",
DefaultMessage: &i18n.Message{
ID: "200",
Other: "未知错误",
},
})
fmt.Println(info)
}
func LoadJson() {
bundle := i18n.NewBundle(language.Chinese)
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
_, err := bundle.LoadMessageFile("zh-CN.json")
if err != nil {
panic(err)
}
localizer := i18n.NewLocalizer(bundle, "zh-CN")
info := localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "200",
DefaultMessage: &i18n.Message{
ID: "200",
Other: "未知错误",
},
})
fmt.Println(info)
}
func LoadJsonFile(bs []byte) {
bundle := i18n.NewBundle(language.Chinese)
bundle.RegisterUnmarshalFunc("json", json.Unmarshal)
bundle.MustParseMessageFileBytes(bs, "zh.json")
localizer := i18n.NewLocalizer(bundle, "zh-CN")
info := localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "200",
DefaultMessage: &i18n.Message{
ID: "200",
Other: "未知错误",
},
})
fmt.Println(info)
}