-
Notifications
You must be signed in to change notification settings - Fork 3
/
i18n.go
441 lines (368 loc) · 9.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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package i18n
import (
"bytes"
"fmt"
"os"
"strconv"
"strings"
"github.com/abulo/ratel/config/parser/ini"
"github.com/pkg/errors"
)
const (
// I18n.LoadMode language file load mode
// FileMode language name is file name. "en" -> "lang/en.ini"
FileMode uint8 = 0
// DirMode language name is dir name, will load all file in the dir. "en" -> "lang/en/*.ini"
DirMode uint8 = 1
// I18n.TransMode translate message mode.
// SprintfMode render message arguments by fmt.Sprintf
SprintfMode uint8 = 0
// ReplaceMode render message arguments by string replace
ReplaceMode uint8 = 1
)
// I18n language manager
type I18n struct {
// languages data
data map[string]*ini.Ini
// language files directory
langDir string
// language list {en:English, zh-CN:简体中文}
languages map[string]string
// loaded lang files
// loadedFiles []string
// ------------ config for i18n ------------
// mode for the load language files.
// 0 single language file
// 1 multi language directory
LoadMode uint8
// TODO translate mode.
// 0 sprintf
// 1 replace
TransMode uint8
// default language name. eg. "en"
DefaultLang string
// spare(fallback) language name. eg. "en"
FallbackLang string
}
/************************************************************
* default instance
************************************************************/
// default instance
var defI18n = NewEmpty()
// Default get default i18n instance
func Default() *I18n {
return defI18n
}
// T translate language key to value string
func T(lang string, key string, args ...interface{}) string {
return defI18n.T(lang, key, args...)
}
// Tr translate language key to value string
func Tr(lang string, key string, args ...interface{}) string {
return defI18n.Tr(lang, key, args...)
}
// Dt translate language key from default language
func Dt(key string, args ...interface{}) string {
return defI18n.DefTr(key, args...)
}
// DefTr translate language key from default language
func DefTr(key string, args ...interface{}) string {
return defI18n.DefTr(key, args...)
}
// Init the default language instance
func Init(langDir string, defLang string, languages map[string]string) *I18n {
defI18n.langDir = langDir
defI18n.languages = languages
defI18n.DefaultLang = defLang
return defI18n.Init()
}
/************************************************************
* create instance
************************************************************/
// New an i18n instance
func New(langDir string, defLang string, languages map[string]string) *I18n {
return &I18n{
data: make(map[string]*ini.Ini),
// language data config
langDir: langDir,
languages: languages,
// set default lang
DefaultLang: defLang,
}
}
// NewEmpty nwe an empty i18n instance
func NewEmpty() *I18n {
return &I18n{
data: make(map[string]*ini.Ini),
// init languages
languages: make(map[string]string),
}
}
// NewWithInit a i18n instance and call init
func NewWithInit(langDir string, defLang string, languages map[string]string) *I18n {
m := New(langDir, defLang, languages)
return m.Init()
}
/************************************************************
* translate message
************************************************************/
// Dt translate from default lang
func (l *I18n) Dt(key string, args ...interface{}) string {
return l.Tr(l.DefaultLang, key, args...)
}
// DefTr translate from default lang
func (l *I18n) DefTr(key string, args ...interface{}) string {
return l.Tr(l.DefaultLang, key, args...)
}
// T translate from a lang by key
func (l *I18n) T(lang, key string, args ...interface{}) string {
return l.Tr(lang, key, args...)
}
// Tr translate from a lang by key
// site.name => [site]
// name = my blog
func (l *I18n) Tr(lang, key string, args ...interface{}) string {
if !l.HasLang(lang) {
// find from fallback lang
msg := l.transFromFallback(key)
if msg == "" {
return key
}
// if has args for the message
if len(args) > 0 {
msg = l.renderMessage(msg, args...)
}
return msg
}
// find message by key
msg, ok := l.data[lang].GetValue(key)
if !ok {
// key not exists, find from fallback lang
msg = l.transFromFallback(key)
}
// message is empty
if msg == "" {
return key
}
// if has args for the message
if len(args) > 0 {
msg = l.renderMessage(msg, args...)
}
return msg
}
// HasKey in the language data
func (l *I18n) HasKey(lang, key string) (ok bool) {
if !l.HasLang(lang) {
return
}
_, ok = l.data[lang].GetValue(key)
return
}
// translate from fallback language
func (l *I18n) transFromFallback(key string) string {
fl := l.FallbackLang
if !l.HasLang(fl) {
return ""
}
return l.data[fl].String(key)
}
func (l *I18n) renderMessage(msg string, args ...interface{}) string {
if l.TransMode == SprintfMode {
return fmt.Sprintf(msg, args...)
}
// if args[0] is []string
if ss, ok := args[0].([]string); ok {
return strings.NewReplacer(ss...).Replace(msg)
}
var ss []string
// if args is map[string]interface{}
if mp, ok := args[0].(map[string]interface{}); ok {
for k, v := range mp {
ss = append(ss, "{"+k+"}")
ss = append(ss, toString(v))
}
} else {
// if args is: {field1, value1, field2, value2, ...}, try convert all element to string.
for i, val := range args {
str := toString(val)
if i%2 == 0 {
str = "{" + str + "}"
}
ss = append(ss, str)
}
}
return strings.NewReplacer(ss...).Replace(msg)
}
// convert value to string
func toString(val interface{}) (str string) {
switch tVal := val.(type) {
case int:
str = strconv.Itoa(tVal)
case int8:
str = strconv.Itoa(int(tVal))
case int16:
str = strconv.Itoa(int(tVal))
case int32:
str = strconv.Itoa(int(tVal))
case int64:
str = strconv.Itoa(int(tVal))
case uint:
str = strconv.Itoa(int(tVal))
case uint8:
str = strconv.Itoa(int(tVal))
case uint16:
str = strconv.Itoa(int(tVal))
case uint32:
str = strconv.Itoa(int(tVal))
case uint64:
str = strconv.Itoa(int(tVal))
case float32:
str = fmt.Sprint(tVal)
case float64:
str = fmt.Sprint(tVal)
case string:
str = tVal
case nil:
str = ""
default:
str = "CANNOT-TO-STRING"
}
return
}
/************************************************************
* data manage
************************************************************/
// Init load add language files
func (l *I18n) Init() *I18n {
if l.LoadMode == FileMode {
l.loadSingleFiles()
} else if l.LoadMode == DirMode {
l.loadDirFiles()
} else {
panic("invalid load mode setting. only allow 0, 1")
}
return l
}
// load language files when LoadMode is 0
func (l *I18n) loadSingleFiles() {
pathSep := string(os.PathSeparator)
for lang := range l.languages {
lData := ini.New()
err := lData.LoadFiles(l.langDir + pathSep + lang + ".ini")
if err != nil {
panic("fail to load language: " + lang + ", error " + err.Error())
}
l.data[lang] = lData
}
}
// load language files when LoadMode is 1
func (l *I18n) loadDirFiles() {
pathSep := string(os.PathSeparator)
for lang := range l.languages {
dirPath := l.langDir + pathSep + lang
files, err := os.ReadDir(dirPath)
if err != nil {
panic("read dir fail: " + dirPath + ", error " + err.Error())
}
sl := l.data[lang]
for _, fi := range files {
// skip dir and filter the specified format
if fi.IsDir() || !strings.HasSuffix(fi.Name(), ".ini") {
continue
}
var err error
if sl != nil {
err = sl.LoadFiles(dirPath + pathSep + fi.Name())
} else { // create new language instance
sl = ini.New()
err = sl.LoadFiles(dirPath + pathSep + fi.Name())
l.data[lang] = sl
}
if err != nil {
panic("fail to load language file: " + lang + ", error " + err.Error())
}
}
}
}
// Add new language
func (l *I18n) Add(lang string, name string) {
l.NewLang(lang, name)
}
// NewLang create/add a new language
// Usage:
// i18n.NewLang("zh-CN", "简体中文")
func (l *I18n) NewLang(lang string, name string) {
// lang exist
if _, ok := l.languages[lang]; ok {
return
}
l.data[lang] = ini.New()
l.languages[lang] = name
}
// LoadFile append data to a exist language
// Usage:
// i18n.LoadFile("zh-CN", "path/to/zh-CN.ini")
func (l *I18n) LoadFile(lang string, file string) (err error) {
// append data
if ld, ok := l.data[lang]; ok {
err = ld.LoadFiles(file)
if err != nil {
return
}
} else {
err = errors.New("language" + lang + " not exist, please create it before load data")
}
return
}
// LoadString load language data form a string
// Usage:
// i18n.Set("zh-CN", "name = blog")
func (l *I18n) LoadString(lang string, data string) (err error) {
// append data
if ld, ok := l.data[lang]; ok {
err = ld.LoadStrings(data)
if err != nil {
return
}
} else {
err = errors.New("language" + lang + " not exist, please create it before load data")
}
return
}
// Lang get language data instance
func (l *I18n) Lang(lang string) *ini.Ini {
if _, ok := l.languages[lang]; ok {
return l.data[lang]
}
return nil
}
// Export a language data as INI string
func (l *I18n) Export(lang string) string {
if _, ok := l.languages[lang]; !ok {
return ""
}
var buf bytes.Buffer
_, err := l.data[lang].WriteTo(&buf)
if err != nil {
panic(err)
}
return buf.String()
}
// HasLang in the manager
func (l *I18n) HasLang(lang string) bool {
_, ok := l.languages[lang]
return ok
}
// DelLang from the i18n manager
func (l *I18n) DelLang(lang string) bool {
_, ok := l.languages[lang]
if ok {
delete(l.data, lang)
delete(l.languages, lang)
}
return ok
}
// Languages get all languages
func (l *I18n) Languages() map[string]string {
return l.languages
}