-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaultManager.go
191 lines (172 loc) · 4.42 KB
/
defaultManager.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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package i18n
import (
"encoding/json"
"fmt"
"github.com/chrstphlbr/resource"
"log"
"sync"
"time"
)
type DefaultManager struct {
accessLock sync.RWMutex
// paths to directories where files are located
repository resource.Repository
mapping keys
mappingConstructed time.Time
language string
defaultLanguage string
}
func (m DefaultManager) GetByLanguage(key string) (value string, err error) {
m.accessLock.RLock()
defer m.accessLock.RUnlock()
if m.language == "" {
err = fmt.Errorf("language is not set")
return
}
value, err = m.getWithoutLock(key, m.language)
return
}
// Get implements the method of the interface ManagerMinimal
func (m DefaultManager) Get(key string, language string) (value string, err error) {
m.accessLock.RLock()
defer m.accessLock.RUnlock()
value, err = m.getWithoutLock(key, language)
return
}
func (m DefaultManager) getWithoutLock(key, language string) (value string, err error) {
values, err := m.getAllWithoutLock(key)
if err != nil {
return
}
gv := func() (value string, ok bool) {
if language == "" {
ok = false
return
}
// check for regular string
value, ok = values[language]
if ok {
return
}
// check for HTTP-Header Accept-Language
al, err := NewAcceptLanguage(language)
if err != nil {
ok = false
return
}
// get value for all languages and return when first match occurs
for lang := range al.AcceptedLanguages() {
value, ok = values[lang]
if ok {
return
}
}
return
}
// check for language
value, ok := gv()
if ok {
// found correct value
return
}
// did not find language
// check if default language specified
if m.defaultLanguage != "" {
// default language set
value, ok = values[m.defaultLanguage]
if !ok {
// did not find default language
err = fmt.Errorf("did not find value for key (%s) in language (%s) and default language (%s)", key, language, m.defaultLanguage)
}
} else {
// no default language set
err = fmt.Errorf("did not find value for key (%s) in language (%s). No default language set.", key, language)
}
return
}
func (m DefaultManager) GetAll(key string) (values map[string]string, err error) {
m.accessLock.RLock()
defer m.accessLock.RUnlock()
values, err = m.getAllWithoutLock(key)
return
}
func (m DefaultManager) getAllWithoutLock(key string) (values map[string]string, err error) {
values, ok := m.mapping[key]
if !ok {
err = fmt.Errorf("could not find mapping for key (%s)", key)
}
return
}
func (m DefaultManager) GetOrLog(key string, language string) (value string) {
value, err := m.Get(key, language)
if err != nil {
log.Println(err.Error())
}
return
}
func (m DefaultManager) GetAllOrLog(key string) (values map[string]string) {
values, err := m.GetAll(key)
if err != nil {
log.Println(err.Error())
}
return
}
func (m *DefaultManager) SetLanguage(language string) {
m.accessLock.Lock()
defer m.accessLock.Unlock()
m.language = language
}
func (m DefaultManager) GetLanguage() string {
m.accessLock.RLock()
defer m.accessLock.RUnlock()
return m.language
}
func (m *DefaultManager) SetDefaultLanguage(language string) {
m.accessLock.Lock()
defer m.accessLock.Unlock()
m.defaultLanguage = language
}
func (m DefaultManager) GetDefaultLanguage() string {
m.accessLock.RLock()
defer m.accessLock.RUnlock()
return m.defaultLanguage
}
func (m *DefaultManager) constructMapping() {
m.accessLock.Lock()
defer m.accessLock.Unlock()
m.mapping = make(keys)
var keys keys
// function that adds json decoded keys (currently stored in keys) to mapping
addKeysToMapping := func() {
for key, value := range keys {
m.mapping[key] = value
}
}
// update the repository
m.repository.Update()
for _, res := range m.repository.Resources() {
// get reader
jsonReader, err := res.Get()
// check for errors when getting reader to resource
if err != nil {
log.Printf("could not open resource: %v", err)
}
// create json decoder from reader
jsonDecoder := json.NewDecoder(jsonReader)
// decode json
err = jsonDecoder.Decode(&keys)
// check for decoding errors
if err != nil {
log.Printf("could not unmarshal json resource: %v\n", err)
continue
}
// decoding was successful
addKeysToMapping()
}
m.mappingConstructed = time.Now()
}
func NewDefaultManager(repository resource.Repository) *DefaultManager {
defaultManager := &DefaultManager{repository: repository}
defaultManager.constructMapping()
return defaultManager
}