-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.go
77 lines (63 loc) · 1.3 KB
/
language.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
package main
import (
"github.com/astaxie/beego/logs"
"gopkg.in/yaml.v2"
)
type LangItem struct {
Key string `json:"key"`
Value []string `json:"value"`
}
type LangCtrl struct {
idx int
cache map[string]*LangItem
Items []LangItem
}
var langCtrl *LangCtrl
func LanguageInit() error {
langCtrl = new(LangCtrl)
langCtrl.Items = make([]LangItem, 0)
langCtrl.cache = make(map[string]*LangItem, 1024)
body, err := BoxFile().Bytes("language.yaml")
if err != nil {
return err
}
err = yaml.Unmarshal(body, &langCtrl.Items)
if err != nil {
return err
}
for _, v := range langCtrl.Items {
langCtrl.cache[v.Key] = &LangItem{
Key: v.Key, Value: v.Value,
}
}
length := len(LangOptionGet())
idx := int(DataIntValueGet("Language"))
if length <= idx {
LangOptionSet(0)
} else {
langCtrl.idx = idx
}
return nil
}
func LangOptionIdx() int {
return int(DataIntValueGet("Language"))
}
func LangOptionGet() []string {
item, _ := langCtrl.cache["language"]
if item == nil {
return []string{}
}
return item.Value
}
func LangOptionSet(idx int) {
langCtrl.idx = idx
DataIntValueSet("language", uint32(idx))
}
func LangValue(key string) string {
item, _ := langCtrl.cache[key]
if item == nil {
logs.Error("lang value %s fail", key)
return ""
}
return item.Value[langCtrl.idx]
}