-
Notifications
You must be signed in to change notification settings - Fork 4
/
module.go
72 lines (62 loc) · 1.47 KB
/
module.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
package langkit
import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"sync"
)
type Module struct { // TODO maybe other name than domain
sync.RWMutex
translations map[string]Translations
}
func NewModule(path string, lazyLoad bool) (*Module, error) {
d := &Module{translations: make(map[string]Translations)}
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, fmt.Errorf("Could not read translation files at path: %v, err: %w", path, err)
}
for _, f := range files {
ext := filepath.Ext(f.Name())
if ext == ".po" {
locale := strings.TrimSuffix(f.Name(), ext)
fullpath := filepath.Join(path, f.Name())
translations, err := ReadPoFile(fullpath, lazyLoad)
if err != nil {
return nil, fmt.Errorf("Could not read translations at path: %v, err: %w", fullpath, err)
}
d.translations[locale] = translations
}
}
return d, nil
}
var noop = &NoTranslations{formatters: newFormatters()}
func (d *Module) FindTranslations(locale string) Translations {
d.RLock()
t, found := d.translations[locale]
d.RUnlock()
if !found {
d.Lock()
defer d.Unlock()
t = noop
l := getLang(locale)
for k, tx := range d.translations {
if getLang(k) == l {
t = tx
break
}
}
d.translations[locale] = t
}
return t
}
func getLang(locale string) string {
parts := strings.Split(locale, "_")
if len(parts) != 2 {
parts = strings.Split(locale, "-")
}
if len(parts) == 2 {
return strings.ToLower(parts[0])
}
return strings.ToLower(locale)
}