-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.go
62 lines (51 loc) · 1.44 KB
/
i18n.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
package i18n
import (
"github.com/chrstphlbr/resource"
"sync"
)
const defaultDirectory = "/files"
var manager struct {
sync.Mutex
m Manager
}
func GetManager() Manager {
manager.Lock()
defer manager.Unlock()
if manager.m == nil {
repository := resource.NewFileRepository(defaultDirectory)
manager.m = NewDefaultManager(repository)
}
return manager.m
}
func SetManager(m Manager) {
manager.Lock()
defer manager.Unlock()
manager.m = m
}
type Manager interface {
ManagerMinimal
ManagerWithLanguage
ManagerWithLogging
}
type ManagerMinimal interface {
// Get searches in the imnternal repository for a value which matches the provided key and language and returns if found the value (value) or if not found
// an error (err). The parameter value can be a general string or can have the form for Accept-Language HTTP-Header.
Get(key string, language string) (value string, err error)
GetAll(key string) (values map[string]string, err error)
SetDefaultLanguage(language string)
GetDefaultLanguage() string
}
type ManagerWithLanguage interface {
GetByLanguage(key string) (value string, err error)
SetLanguage(language string)
GetLanguage() string
}
type ManagerWithLogging interface {
GetOrLog(key string, language string) (value string)
GetAllOrLog(key string) (values map[string]string)
}
// mapping types
// key map from keys to values
type keys map[string]values
// values map from laguage to actual values
type values map[string]string