/
file.go
50 lines (41 loc) · 999 Bytes
/
file.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
package i18n
import (
"path/filepath"
"github.com/pilu/config"
)
func loadFile(filePath string) error {
baseName := filepath.Base(filePath)
extension := filepath.Ext(baseName)
mainSectionName := baseName[:len(baseName) - len(extension)]
sections, err := config.ParseFile(filePath, mainSectionName)
if err != nil {
return err
}
for section, options := range sections {
if len(options) > 0 {
locale := GetLocale(section)
if locale == nil {
locale = NewLocale(section)
}
for key, value := range options {
locale.Add(key, value)
}
AddLocale(locale)
}
}
return nil
}
// Loads translations from one or more files passing globPatterns to filepath.Glob.
func Load(globPattern string) error {
paths, err := filepath.Glob(globPattern)
if err != nil {
return nil
}
for _, filePath := range paths {
err := loadFile(filePath)
if err != nil {
return err
}
}
return nil
}