-
Notifications
You must be signed in to change notification settings - Fork 1
/
i18n.go
51 lines (45 loc) · 1.02 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
package datagen
import (
"embed"
"encoding/json"
"strings"
)
//go:embed locales/*.json
var i18nDataDir embed.FS
var localesDatas map[string]*locale
const defaultLocal = "en"
type locale struct {
Text *i18nText `json:"text,omitempty"`
People *i18nPeople `json:"people,omitempty"`
Address *i18nAddress `json:"address,omitempty"`
}
func init() {
ls, err := i18nDataDir.ReadDir("locales")
if err != nil {
panic(err)
}
localesDatas = make(map[string]*locale)
for _, v := range ls {
if v.IsDir() {
continue
}
raw, err := i18nDataDir.ReadFile("locales/" + v.Name())
if err != nil {
panic(err)
}
var data locale
if err := json.Unmarshal(raw, &data); err != nil {
panic(err)
}
data.Text._charRuns = []rune(data.Text.Chars)
localesDatas[strings.Split(v.Name(), ".")[0]] = &data
}
}
func GetLocale(lang ...string) *locale {
if len(lang) > 0 {
if v, ok := localesDatas[pick(lang)]; ok {
return v
}
}
return localesDatas[defaultLocal]
}