-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.go
105 lines (92 loc) · 2.34 KB
/
loader.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
package i18n
import (
"io/fs"
"os"
"path/filepath"
)
// LoadMessages loads the translations from the map.
func (bundle *I18n) LoadMessages(languages map[string]map[string]string) error {
for locale, translations := range languages {
locale = bundle.getExactSupportedLocale(locale)
if locale != "" {
if _, ok := bundle.parsedTranslations[locale]; !ok {
bundle.parsedTranslations[locale] = make(map[string]*parsedTranslation)
}
for name, text := range translations {
trans, err := bundle.parseTranslation(locale, name, text)
if err != nil {
return err
}
bundle.parsedTranslations[locale][name] = trans
}
}
}
bundle.formatFallbacks()
return nil
}
// LoadFiles loads the translations from the files.
func (bundle *I18n) LoadFiles(files ...string) error {
data := make(map[string]map[string]string)
for _, file := range files {
b, err := os.ReadFile(file) //nolint:gosec
if err != nil {
return err
}
var trans map[string]string
if err := bundle.unmarshaler(b, &trans); err != nil {
return err
}
locale := nameInsenstive(file)
_, ok := data[locale]
if !ok {
data[locale] = make(map[string]string)
}
for name, text := range trans {
data[locale][name] = text
}
}
return bundle.LoadMessages(data)
}
// LoadGlob loads the translations from the files that matches specified patterns.
func (bundle *I18n) LoadGlob(pattern ...string) error {
var files []string
for _, pattern := range pattern {
v, err := filepath.Glob(pattern)
if err != nil {
return err
}
files = append(files, v...)
}
return bundle.LoadFiles(files...)
}
// LoadFS loads the translation from a `fs.FS`, useful for `go:embed`.
func (bundle *I18n) LoadFS(fsys fs.FS, patterns ...string) error {
var files []string
data := make(map[string]map[string]string)
for _, pattern := range patterns {
v, err := fs.Glob(fsys, pattern)
if err != nil {
return err
}
files = append(files, v...)
}
for _, file := range files {
b, err := fs.ReadFile(fsys, file)
if err != nil {
return err
}
trans := make(map[string]string)
if err := bundle.unmarshaler(b, &trans); err != nil {
return err
}
locale := nameInsenstive(file)
_, ok := data[locale]
if !ok {
data[locale] = make(map[string]string)
}
for name, text := range trans {
data[locale][name] = text
}
}
return bundle.LoadMessages(data)
}