-
Notifications
You must be signed in to change notification settings - Fork 4
/
loader.go
337 lines (287 loc) · 9.99 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
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
package spreak
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path"
"golang.org/x/text/language"
"github.com/vorlif/spreak/catalog"
"github.com/vorlif/spreak/internal/util"
)
const (
// Deprecated: Will be removed in a future version.
// Has only been used for tests so far.
UnknownFile = "unknown"
// PoFile is the file extension of Po files.
// Deprecated: Will be removed in a future version.
// The string should be kept in your own program code.
PoFile = ".po"
// MoFile is the file extension of Mo files.
// Deprecated: Will be removed in a future version.
// The string should be kept in your own program code.
MoFile = ".mo"
// JSONFile is the file extension of JSON files.
// Deprecated: Will be removed in a future version.
// The string should be kept in your own program code.
JSONFile = ".json"
)
// Catalog represents a collection of messages (translations) for a language and a domain.
// Normally it is a PO or MO file.
//
// Deprecated: Moved to catalog.Catalog. This alias will be removed in version 1.0.
type Catalog = catalog.Catalog
// A Decoder reads and decodes catalogs for a language and a domain from a byte array.
//
// Deprecated: Moved to catalog.Decoder and will be removed with v1.0.
type Decoder = catalog.Decoder
// Loader is responsible for loading Catalogs for a language and a domain.
// A bundle loads each domain through its own loader.
//
// If a loader cannot find a matching catalog for it must return error spreak.ErrNotFound,
// otherwise the bundle creation will be aborted with the returned error.
type Loader interface {
Load(lang language.Tag, domain string) (catalog.Catalog, error)
}
// A Resolver is used by the FilesystemLoader to resolve the appropriate path for a file.
// If a file was not found, os.ErrNotExist should be returned.
// All other errors cause the loaders search to stop.
//
// fsys represents the file system from which the FilesystemLoader wants to load the file.
// extensions is the file extension for which the file is to be resolved.
// Language and Domain indicate for which domain in which language the file is searched.
type Resolver interface {
Resolve(fsys fs.FS, extensions string, lang language.Tag, domain string) (string, error)
}
// FsOption is an option which can be used when creating the FilesystemLoader.
type FsOption func(l *FilesystemLoader) error
// ResolverOption is an option which can be used when creating the DefaultResolver.
type ResolverOption func(r *defaultResolver)
// FilesystemLoader is a Loader which loads and decodes files from a file system.
// A file system here means an implementation of fs.FS.
type FilesystemLoader struct {
fsys fs.FS
resolver Resolver
extensions []string
decoder []catalog.Decoder
}
var _ Loader = (*FilesystemLoader)(nil)
// NewFilesystemLoader creates a new FileSystemLoader.
// If no file system was stored during the creation, an error is returned.
// If no decoder has been stored, the Po and Mo decoders are automatically used.
// Otherwise, only the stored decoders are used.
func NewFilesystemLoader(opts ...FsOption) (*FilesystemLoader, error) {
l := &FilesystemLoader{
decoder: make([]catalog.Decoder, 0),
extensions: make([]string, 0),
}
for _, opt := range opts {
if opt == nil {
return nil, errors.New("spreak.Loader: try to create an FilesystemLoader with a nil option")
}
if err := opt(l); err != nil {
return nil, err
}
}
if len(l.decoder) == 0 {
l.addDecoder(".po", catalog.NewPoDecoder())
l.addDecoder(".mo", catalog.NewMoDecoder())
l.addDecoder(".json", catalog.NewJSONDecoder())
}
if l.fsys == nil {
return nil, errors.New("spreak.Loader: try to create an FilesystemLoader without an filesystem")
}
if l.resolver == nil {
resolver, err := NewDefaultResolver()
if err != nil {
return nil, err
}
l.resolver = resolver
}
return l, nil
}
func (l *FilesystemLoader) Load(lang language.Tag, domain string) (catalog.Catalog, error) {
for i, extension := range l.extensions {
resolvedPath, errP := l.resolver.Resolve(l.fsys, extension, lang, domain)
if errP != nil {
if errors.Is(errP, os.ErrNotExist) {
continue
}
return nil, errP
}
f, errF := l.fsys.Open(resolvedPath)
if errF != nil {
if f != nil {
_ = f.Close()
}
return nil, errF
}
defer f.Close()
data, errD := io.ReadAll(f)
if errD != nil {
return nil, errD
}
catl, errC := l.decoder[i].Decode(lang, domain, data)
if errC != nil {
return nil, fmt.Errorf("spreak: file %s could not be decoded: %w", resolvedPath, errC)
}
return catl, nil
}
return nil, NewErrNotFound(lang, "file", "domain=%q", domain)
}
func (l *FilesystemLoader) addDecoder(ext string, decoder catalog.Decoder) {
l.extensions = append(l.extensions, ext)
l.decoder = append(l.decoder, decoder)
}
// WithFs stores a fs.FS as filesystem.
// The file system can only be accessed with paths which are separated by slashes (Unix style).
// If a different behavior is desired, a separate resolver must be stored with WithResolver.
// Lets the creation of the FilesystemLoader fail, if a filesystem was already deposited.
func WithFs(fsys fs.FS) FsOption {
return func(l *FilesystemLoader) error {
if l.fsys != nil {
return errors.New("spreak.Loader: filesystem for FilesystemLoader already set")
}
l.fsys = fsys
return nil
}
}
// WithPath stores a path as filesystem.
// Lets the creation of the FilesystemLoader fail, if a filesystem was already deposited.
func WithPath(path string) FsOption {
return func(l *FilesystemLoader) error {
if l.fsys != nil {
return errors.New("spreak.Loader: filesystem for FilesystemLoader already set")
}
l.fsys = util.DirFS(path)
return nil
}
}
// WithSystemFs stores the root path as filesystem.
// Lets the creation of the FilesystemLoader fail, if a filesystem was already deposited.
//
// Shorthand for WithPath("").
func WithSystemFs() FsOption { return WithPath("") }
// WithResolver stores the resolver of a FilesystemLoader.
// Lets the creation of the FilesystemLoader fail, if a Resolver was already deposited.
func WithResolver(resolver Resolver) FsOption {
return func(l *FilesystemLoader) error {
if l.resolver != nil {
return errors.New("spreak.Loader: Resolver for FilesystemLoader already set")
}
l.resolver = resolver
return nil
}
}
// WithDecoder stores a decoder for a file extension.
//
// The file extension should begin with a dot. For example ".po" or ".json".
func WithDecoder(ext string, decoder catalog.Decoder) FsOption {
return func(r *FilesystemLoader) error {
r.addDecoder(ext, decoder)
return nil
}
}
// WithMoDecoder stores the mo file decoder.
//
// Shorthand for WithDecoder(".mo", catalog.NewMoDecoder()).
func WithMoDecoder() FsOption { return WithDecoder(".mo", catalog.NewMoDecoder()) }
// WithPoDecoder stores the mo file decoder.
//
// Shorthand for WithDecoder(".po", catalog.NewPoDecoder()).
func WithPoDecoder() FsOption { return WithDecoder(".po", catalog.NewPoDecoder()) }
// WithJSONDecoder stores the JSON file decoder.
//
// Shorthand for WithDecoder(".json", catalog.NewJSONDecoder()).
func WithJSONDecoder() FsOption { return WithDecoder(".json", catalog.NewJSONDecoder()) }
type defaultResolver struct {
search bool
category string
}
// NewDefaultResolver create a resolver which can be used for a FilesystemLoader.
// It is the Resolver that is used if no separate resolver has been set.
// He tries to find the files in different directories and returns the file that it found first.
//
// For example, if a Mo file is to be found, an attempt is made to resolve the following paths.
// - .../locale/category/domain.mo
// - .../locale/LC_MESSAGES/domain.mo
// - .../locale/domain.mo
// - .../domain/locale.mo
// - .../locale.mo
// - .../category/locale.mo
// - .../LC_MESSAGES/locale.mo
func NewDefaultResolver(opts ...ResolverOption) (Resolver, error) {
l := &defaultResolver{
search: true,
category: "",
}
for _, opt := range opts {
opt(l)
}
return l, nil
}
func WithDisabledSearch() ResolverOption { return func(r *defaultResolver) { r.search = false } }
// WithCategory defines an additional category which is included in the search.
// For Gettext files, LC_MESSAGES is often used for this.
func WithCategory(category string) ResolverOption {
return func(l *defaultResolver) { l.category = category }
}
func (r *defaultResolver) Resolve(fsys fs.FS, extension string, tag language.Tag, domain string) (string, error) {
for _, lang := range ExpandLanguage(tag) {
searchPath, err := r.searchFileForLanguageName(fsys, lang, domain, extension)
if errors.Is(err, os.ErrNotExist) {
continue
}
return searchPath, nil
}
return "", os.ErrNotExist
}
func (r *defaultResolver) searchFileForLanguageName(fsys fs.FS, locale, domain, ext string) (string, error) {
if domain != "" {
// .../locale/category/domain.mo
searchPath := path.Join(locale, r.category, domain+ext)
if _, err := fs.Stat(fsys, searchPath); err == nil {
return searchPath, nil
}
}
if r.search {
if domain != "" {
// .../locale/LC_MESSAGES/domain.mo
searchPath := path.Join(locale, "LC_MESSAGES", domain+ext)
if _, err := fs.Stat(fsys, searchPath); err == nil {
return searchPath, nil
}
// .../locale/domain.mo
searchPath = path.Join(locale, domain+ext)
if _, err := fs.Stat(fsys, searchPath); err == nil {
return searchPath, nil
}
// .../domain/locale.mo
searchPath = path.Join(domain, locale+ext)
if _, err := fs.Stat(fsys, searchPath); err == nil {
return searchPath, nil
}
}
// .../locale.mo
searchPath := path.Join(locale + ext)
if _, err := fs.Stat(fsys, searchPath); err == nil {
return searchPath, nil
}
if r.category != "" {
// .../category/locale.mo
searchPath = path.Join(r.category, locale+ext)
if _, err := fs.Stat(fsys, searchPath); err == nil {
return searchPath, nil
}
}
if r.category != "LC_MESSAGES" {
// .../LC_MESSAGES/locale.mo
searchPath = path.Join("LC_MESSAGES", locale+ext)
if _, err := fs.Stat(fsys, searchPath); err == nil {
return searchPath, nil
}
}
}
return "", os.ErrNotExist
}