forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.go
166 lines (138 loc) · 3.46 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package i18n
import (
"crypto/sha256"
"fmt"
"strings"
"sync"
"time"
"github.com/yaoapp/gou/lang"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/data"
"gopkg.in/yaml.v3"
)
type langCache = struct {
data map[string]interface{}
mu sync.RWMutex
}
var cache langCache = langCache{
data: map[string]interface{}{},
mu: sync.RWMutex{},
}
var timezone *time.Location
func init() {
lang.RegisterWidget("logins", "login")
lang.RegisterWidget("tables", "table")
lang.RegisterWidget("forms", "form")
lang.RegisterWidget("charts", "chart")
lang.RegisterWidget("kanban", "page")
lang.RegisterWidget("screen", "page")
lang.RegisterWidget("pages", "page")
}
// Load language packs
func Load(cfg config.Config) error {
err := loadFromBin()
if err != nil {
return err
}
// Load langs
err = lang.Load("langs")
if err != nil {
return err
}
if _, has := lang.Dicts[cfg.Lang]; !has {
log.Error("The language pack %s does not found", cfg.Lang)
return nil
}
lang.Pick(cfg.Lang).AsDefault()
// Load Timezone
if cfg.TimeZone != "" {
loc, err := time.LoadLocation(cfg.TimeZone)
if err != nil {
log.Error("Load timezone error %s", cfg.TimeZone)
}
timezone = loc
}
// Clear lang cache
cache.mu.Lock()
defer cache.mu.Unlock()
cache.data = map[string]interface{}{}
return nil
}
// Trans translate dsl
func Trans(langName string, widgets []string, data interface{}) (interface{}, error) {
// Get From cache
hash := sha256.Sum256([]byte(fmt.Sprintf("%v", data)))
key := fmt.Sprintf("%s::%s::%s", langName, strings.Join(widgets, "::"), hash)
if res, has := cache.data[key]; has {
return res, nil
}
var dict *lang.Dict = lang.Default
if langName != "" {
dict = lang.Pick(langName)
}
res, err := dict.ReplaceClone(widgets, data)
if err != nil {
return nil, err
}
cacheSet(dict, widgets, res)
return res, nil
}
// cacheSet cache set
func cacheSet(dict *lang.Dict, widgets []string, value interface{}) {
cache.mu.Lock()
defer cache.mu.Unlock()
hash := sha256.Sum256([]byte(fmt.Sprintf("%v", value)))
key := fmt.Sprintf("%s::%s::%s", dict.Name, strings.Join(widgets, "::"), hash)
cache.data[key] = value
}
func loadFromBin() error {
dirs := map[string][]struct {
File string
Widget string
IsGlobal bool
}{
"zh-cn": {
{File: "yao/langs/zh-cn/global.yml", IsGlobal: true},
{File: "yao/langs/zh-cn/logins/admin.login.yml", Widget: "login.admin"},
{File: "yao/langs/zh-cn/logins/user.login.yml", Widget: "login.user"},
},
"zh-hk": {
{File: "yao/langs/zh-hk/global.yml", IsGlobal: true},
{File: "yao/langs/zh-hk/logins/admin.login.yml", Widget: "login.admin"},
{File: "yao/langs/zh-hk/logins/user.login.yml", Widget: "login.user"},
},
}
for langName, files := range dirs {
dict := &lang.Dict{
Name: langName,
Global: lang.Words{},
Widgets: map[string]lang.Words{},
}
for _, f := range files {
data, err := data.Read(f.File)
if err != nil {
return fmt.Errorf("%s: %s", f.File, err.Error())
}
words := lang.Words{}
err = yaml.Unmarshal(data, &words)
if err != nil {
return fmt.Errorf("%s: %s", f.File, err.Error())
}
if f.IsGlobal {
dict.Global = words
continue
}
if _, has := dict.Widgets[f.Widget]; !has {
dict.Widgets[f.Widget] = lang.Words{}
}
dict.Widgets[f.Widget] = words
}
if _, has := lang.Dicts[langName]; has {
lang.Dicts[langName].Merge(dict)
continue
}
lang.Dicts[langName] = dict
}
return nil
}