-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources.go
209 lines (193 loc) · 3.96 KB
/
resources.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package langpacks
import (
"bufio"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"github.com/tinybear1976/go-langpacks/redisdb"
)
type LoadResult struct {
LangTag string
FileName string
Estimate int
Reality int
}
type LoadMode uint8
const (
InMemory LoadMode = 0
InRedis LoadMode = 1
)
var (
version string
langs map[string]map[int]string
loadmode LoadMode
redis_ip_port string
redis_pwd string
redis_db int
lps_path string
lps_suffix string
lps_separator string
is_loaded bool
)
func init() {
loadmode = InMemory
lps_path = "./"
lps_suffix = ".lps"
lps_separator = "~"
is_loaded = false
}
func InitLangPacksDefault() {
InitLangPacks("", "", "", "", "", 0)
}
func InitLangPacksDefaultRedis(ipWithPort, pwd string, db int) {
InitLangPacks("", "", "", ipWithPort, pwd, db)
SetLoadMode(InRedis)
}
func InitLangPacks(lpsPath string, lpsSuffix string, separator string, ipWithPort, pwd string, db int) {
redis_ip_port = ipWithPort
redis_pwd = pwd
redis_db = db
if lpsPath != "" {
lps_path = lpsPath
}
if lpsSuffix != "" {
lps_suffix = strings.ToLower(lpsSuffix)
}
if separator != "" {
lps_separator = separator
}
}
func SetLoadMode(mode LoadMode) {
loadmode = mode
is_loaded = false
}
func Query(langTag string, textId int) (str string) {
if !is_loaded {
return
}
switch loadmode {
case InRedis:
key := "lang::" + langTag + "::" + strconv.Itoa(textId)
str, _ = redisdb.GET("lang", key)
case InMemory:
m, ok := langs[langTag]
if ok {
str = m[textId]
}
}
return
}
func Load() (rst []LoadResult, err error) {
dir, err := ioutil.ReadDir(lps_path)
if err != nil {
return nil, err
}
switch loadmode {
case InRedis:
redisdb.Destroy()
redisdb.New("go-langpacks", redis_ip_port, redis_pwd, redis_db)
case InMemory:
langs = make(map[string]map[int]string)
}
for _, fi := range dir {
if fi.IsDir() {
continue
}
if strings.HasSuffix(strings.ToLower(fi.Name()), lps_suffix) {
file := path.Join(lps_path, fi.Name())
switch loadmode {
case InMemory:
langpack := make(map[int]string)
tag, es, rely := loadLangPacksbyMemory(file, lps_separator, langpack)
if tag != "" {
langs[tag] = langpack
rst = append(rst, LoadResult{
LangTag: tag,
FileName: file,
Estimate: es,
Reality: rely,
})
}
case InRedis:
tag, es, rely := loadLangPacks(file, lps_separator)
if tag != "" {
rst = append(rst, LoadResult{
LangTag: tag,
FileName: file,
Estimate: es,
Reality: rely,
})
}
}
}
}
is_loaded = true
return
}
func loadLangPacks(filenameWithPath string, separator string) (langTag string, estimate int, reality int) {
file, err := os.Open(filenameWithPath)
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
firtRow := true
for scanner.Scan() {
line := scanner.Text()
if firtRow {
firtRow = false
langTag = strings.Trim(line, " ")
if langTag == "" {
return
}
continue
}
segs := strings.Split(line, separator)
if len(segs) != 2 {
continue
}
estimate++
no, ok := strconv.Atoi(strings.Trim(segs[0], " "))
if ok == nil {
key := "lang::" + langTag + "::" + strconv.Itoa(no)
err := redisdb.SET("go-langpacks", key, segs[1])
if err == nil {
reality++
}
}
}
return
}
func loadLangPacksbyMemory(filenameWithPath string, separator string, m map[int]string) (langTag string, estimate int, reality int) {
file, err := os.Open(filenameWithPath)
if err != nil {
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
firtRow := true
for scanner.Scan() {
line := scanner.Text()
if firtRow {
firtRow = false
langTag = strings.Trim(line, " ")
if langTag == "" {
return
}
continue
}
segs := strings.Split(line, separator)
if len(segs) != 2 {
continue
}
estimate++
no, ok := strconv.Atoi(strings.Trim(segs[0], " "))
if ok == nil {
m[no] = segs[1]
reality++
}
}
return
}