-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathfile_loader.go
44 lines (38 loc) · 916 Bytes
/
file_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
package translation
import (
"os"
"path/filepath"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/support/file"
)
type FileLoader struct {
paths []string
json foundation.Json
}
func NewFileLoader(paths []string, json foundation.Json) *FileLoader {
return &FileLoader{
paths: paths,
json: json,
}
}
func (f *FileLoader) Load(locale string, group string) (map[string]any, error) {
for _, path := range f.paths {
var val map[string]any
fullPath := filepath.Join(path, locale, group+".json")
if group == "*" {
fullPath = filepath.Join(path, locale+".json")
}
if file.Exists(fullPath) {
data, err := os.ReadFile(fullPath)
if err != nil {
return nil, err
}
if err = f.json.Unmarshal(data, &val); err != nil {
return nil, err
}
return val, nil
}
}
return nil, errors.LangFileNotExist
}