This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translations.go
109 lines (90 loc) · 2.25 KB
/
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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"regexp"
"strings"
)
type Translations struct {
data map[string]map[string]interface{}
fallback string
}
func NewTranslations() Translations {
return Translations{
data: make(map[string]map[string]interface{}),
}
}
func (t *Translations) Load(language string, filename string) error {
file, err := os.Open(filename)
if err != nil {
return fmt.Errorf("Cannot open %s", filename)
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return fmt.Errorf("Cannot read %s", filename)
}
var lang_map map[string]interface{}
json.Unmarshal(bytes, &lang_map)
t.data[language] = lang_map
return nil
}
func (t *Translations) SetFallback(language string) error {
if _, ok := t.data[language]; ok {
t.fallback = language
return nil
}
// if language not in data, we cannot use it as a fallback
return fmt.Errorf("Language %s not in loaded languages", language)
}
func internalLookupKeyInNestedMap(id string, data interface{}) (string, bool) {
keys := strings.Split(id, ".")
// Traverse tree
for _, key := range keys {
mdat, ok := data.(map[string]interface{})
if !ok {
return "", false
}
data, ok = mdat[key]
if !ok {
return "", false
}
}
if output, ok := data.(string); ok {
return output, true
}
return "", false
}
func (t *Translations) Translate(language string, id string) string {
// Lookup in current language
if lang_map, ok := t.data[language]; ok {
output, ok := internalLookupKeyInNestedMap(id, lang_map)
if ok {
return output
}
}
// if not found, lookup in fallback language
if fallback_map, ok := t.data[t.fallback]; ok {
output, ok := internalLookupKeyInNestedMap(id, fallback_map)
if ok {
return output
}
}
// if not found, return id
return id
}
func (t *Translations) ParseAcceptLanguage(acceptLanguageHeader string) string {
// regexp to match language in accept language ignoring weights
re := regexp.MustCompile(`([\w-*]+)\s*(?:;\s*q\s*=\s*[0-9.]+)?`)
match := re.FindAllStringSubmatch(acceptLanguageHeader, -1)
for _, group := range match {
if _, ok := t.data[group[1]]; ok {
// if language in data, return it
return group[1]
}
}
// if language is not available, use default
return t.fallback
}