/
yaml.go
179 lines (158 loc) · 4.5 KB
/
yaml.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
package yaml
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/conku/i18n"
"gopkg.in/yaml.v2"
)
var _ i18n.Backend = &Backend{}
// New new YAML backend for I18n
func New(paths ...string) *Backend {
backend := &Backend{}
var files []string
for _, p := range paths {
if file, err := os.Open(p); err == nil {
defer file.Close()
if fileInfo, err := file.Stat(); err == nil {
if fileInfo.IsDir() {
yamlFiles, _ := filepath.Glob(filepath.Join(p, "*.yaml"))
files = append(files, yamlFiles...)
ymlFiles, _ := filepath.Glob(filepath.Join(p, "*.yml"))
files = append(files, ymlFiles...)
} else if fileInfo.Mode().IsRegular() {
files = append(files, p)
}
}
}
}
for _, file := range files {
if content, err := ioutil.ReadFile(file); err == nil {
backend.contents = append(backend.contents, content)
}
}
return backend
}
// NewWithWalk has the same functionality as New but uses filepath.Walk to find all the translation files recursively.
func NewWithWalk(paths ...string) i18n.Backend {
backend := &Backend{}
var files []string
for _, p := range paths {
filepath.Walk(p, func(path string, fileInfo os.FileInfo, err error) error {
if isYamlFile(fileInfo) {
files = append(files, path)
}
return nil
})
}
for _, file := range files {
if content, err := ioutil.ReadFile(file); err == nil {
backend.contents = append(backend.contents, content)
}
}
return backend
}
func isYamlFile(fileInfo os.FileInfo) bool {
if fileInfo == nil {
return false
}
return fileInfo.Mode().IsRegular() && (strings.HasSuffix(fileInfo.Name(), ".yml") || strings.HasSuffix(fileInfo.Name(), ".yaml"))
}
func walkFilesystem(fs http.FileSystem, entry http.File, prefix string) [][]byte {
var (
contents [][]byte
err error
isRoot bool
)
if entry == nil {
if entry, err = fs.Open("/"); err != nil {
return nil
}
isRoot = true
defer entry.Close()
}
fileInfo, err := entry.Stat()
if err != nil {
return nil
}
if !isRoot {
prefix = prefix + fileInfo.Name() + "/"
}
if fileInfo.IsDir() {
if entries, err := entry.Readdir(-1); err == nil {
for _, e := range entries {
if file, err := fs.Open(prefix + e.Name()); err == nil {
defer file.Close()
contents = append(contents, walkFilesystem(fs, file, prefix)...)
}
}
}
} else if isYamlFile(fileInfo) {
if content, err := ioutil.ReadAll(entry); err == nil {
contents = append(contents, content)
}
}
return contents
}
// NewWithFilesystem initializes a backend that reads translation files from an http.FileSystem.
func NewWithFilesystem(fss ...http.FileSystem) i18n.Backend {
backend := &Backend{}
for _, fs := range fss {
backend.contents = append(backend.contents, walkFilesystem(fs, nil, "/")...)
}
return backend
}
// Backend YAML backend
type Backend struct {
contents [][]byte
}
func loadTranslationsFromYaml(locale string, value interface{}, scopes []string) (translations []*i18n.Translation) {
switch v := value.(type) {
case yaml.MapSlice:
for _, s := range v {
results := loadTranslationsFromYaml(locale, s.Value, append(scopes, fmt.Sprint(s.Key)))
translations = append(translations, results...)
}
default:
var translation = &i18n.Translation{
Locale: locale,
Key: strings.Join(scopes, "."),
Value: fmt.Sprint(v),
}
translations = append(translations, translation)
}
return
}
// LoadYAMLContent load YAML content
func (backend *Backend) LoadYAMLContent(content []byte) (translations []*i18n.Translation, err error) {
var slice yaml.MapSlice
if err = yaml.Unmarshal(content, &slice); err == nil {
for _, item := range slice {
translations = append(translations, loadTranslationsFromYaml(item.Key.(string) /* locale */, item.Value, []string{})...)
}
}
return translations, err
}
// LoadTranslations load translations from YAML backend
func (backend *Backend) LoadTranslations() (translations []*i18n.Translation) {
for _, content := range backend.contents {
if results, err := backend.LoadYAMLContent(content); err == nil {
translations = append(translations, results...)
} else {
panic(err)
}
}
return translations
}
// SaveTranslation save translation into YAML backend, not implemented
func (backend *Backend) SaveTranslation(t *i18n.Translation) error {
return errors.New("not implemented")
}
// DeleteTranslation delete translation into YAML backend, not implemented
func (backend *Backend) DeleteTranslation(t *i18n.Translation) error {
return errors.New("not implemented")
}