-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranslations.go
47 lines (42 loc) · 1003 Bytes
/
translations.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
package interpol
import (
"fmt"
"os"
"strings"
)
type translations map[string]string
func (t translations) addMap(b string, content map[interface{}]interface{}) {
for key, value := range content {
if k, ok := key.(string); ok {
path := fmt.Sprintf("%s.%s", b, k)
switch v := value.(type) {
case []interface{}:
t.addList(path, v)
case map[interface{}]interface{}:
t.addMap(path, v)
default:
t.addOther(path, v)
}
}
}
}
func (t translations) addList(b string, lv []interface{}) {
for i, value := range lv {
path := fmt.Sprintf("%s.%d", b, i)
switch v := value.(type) {
case []interface{}:
t.addList(path, v)
case map[interface{}]interface{}:
t.addMap(path, v)
default:
t.addOther(path, v)
}
}
}
func (t translations) addOther(b string, v interface{}) {
if s, ok := v.(string); ok {
t[strings.Join(strings.Split(b, ".")[2:], ".")] = s
} else if os.Getenv("DEBUG") != "" {
fmt.Printf("failed to parse %T - %+v at %s\n", v, v, b)
}
}