localization.go
2.91 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package webutility
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path"
"strconv"
"strings"
"sync"
)
// Dictionary ...
type Dictionary struct {
my sync.Mutex
locales map[string]map[string]string
supported []string
defaultLocale string
}
// NewDictionary ...
func NewDictionary() *Dictionary {
return &Dictionary{
locales: map[string]map[string]string{},
}
}
// AddTranslations ...
func (d *Dictionary) AddTranslations(directory string) error {
files, err := ioutil.ReadDir(directory)
if err != nil {
return err
}
for _, fileInfo := range files {
fName := fileInfo.Name()
path := directory + "/" + fName
file, err := ioutil.ReadFile(path)
if err != nil {
return err
}
loc := stripFileExtension(fName)
var data interface{}
err = json.Unmarshal(file, &data)
if err != nil {
return err
}
l := map[string]string{}
for k, v := range data.(map[string]interface{}) {
l[k] = v.(string)
}
mu.Lock()
defer mu.Unlock()
d.locales[loc] = l
d.supported = append(d.supported, loc)
}
if d.defaultLocale == "" && len(d.supported) > 0 {
d.defaultLocale = d.supported[0]
}
return nil
}
// GetBestMatchLocale ...
func (d *Dictionary) GetBestMatchLocale(req *http.Request) (best string) {
accepted := d.parseAcceptedLanguages(req.Header.Get("Accept-Language"))
for i := range accepted {
if accepted[i].Code == "*" {
return d.defaultLocale
}
for j := range d.supported {
if accepted[i].Code == d.supported[j] {
return d.supported[j]
}
}
}
return d.defaultLocale
}
// Translate ...
func (d *Dictionary) Translate(loc, key string) string {
return d.locales[loc][key]
}
// SetDefaultLocale ...
func (d *Dictionary) SetDefaultLocale(loc string) error {
if !d.contains(loc) {
return fmt.Errorf("locale file not loaded: %s", loc)
}
d.defaultLocale = loc
return nil
}
func (d *Dictionary) contains(loc string) bool {
for _, v := range d.supported {
if v == loc {
return true
}
}
return false
}
// LangWeight ...
type LangWeight struct {
Code string
Weight float64
}
func (d *Dictionary) parseAcceptedLanguages(accepted string) (langs []LangWeight) {
if accepted == "" {
langs = append(langs, LangWeight{Code: d.defaultLocale, Weight: 1.0})
return langs
}
var code string
var weight float64
parts := strings.Split(accepted, ",")
for i := range parts {
parts[i] = strings.Trim(parts[i], " ")
cw := strings.Split(parts[i], ";")
paramCount := len(cw)
if paramCount == 1 {
// default weight of 1
code = cw[0]
weight = 1.0
} else if paramCount == 2 {
// parse weight
code = cw[0]
weight, _ = strconv.ParseFloat(cw[1][2:], 64)
}
langs = append(langs, LangWeight{Code: code, Weight: weight})
}
// TODO(marko): sort langs by weights?
return langs
}
func stripFileExtension(full string) (stripped string) {
extension := path.Ext(full)
stripped = strings.TrimSuffix(full, extension)
return stripped
}