-
Notifications
You must be signed in to change notification settings - Fork 14
/
localizer.go
173 lines (150 loc) · 3.85 KB
/
localizer.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
package localizer
import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"sync"
)
const (
seperator = "|-|"
)
type Localizer interface {
Localize(bucket string, key string) (string, error)
LoadBucketFromFile(bucket string, path string, append bool)
LoadBucketFromMap(bucket string, pairs map[string]string, append bool)
// SaveToFile(path string)
SaveBucketToFile(bucket string, path string)
PushBucketToSource(bucket string) (err error)
PullBucketFromSource(bucket string, append bool) (err error)
}
type SimpleLocalizer struct {
lock sync.RWMutex
pairs map[string]string
source Source
}
func (l *SimpleLocalizer) Localize(bucket string, key string) (string, error) {
if bucket == "" {
return "", errors.New("localization bucket not provided")
}
l.lock.RLock()
defer l.lock.RUnlock()
if val, ok := l.pairs[l.genKey(bucket, key)]; ok {
return val, nil
}
return "", errors.New("translation not found")
}
func (l *SimpleLocalizer) LoadBucketFromMap(bucket string, pairs map[string]string, append bool) {
if bucket == "" {
return
}
l.lock.Lock()
defer l.lock.Unlock()
if l.pairs == nil {
l.pairs = map[string]string{}
}
if !append {
l.removeBucket(bucket)
}
for k, v := range pairs {
l.pairs[l.genKey(bucket, k)] = v
}
}
func (l *SimpleLocalizer) LoadBucketFromFile(bucket string, path string, append bool) {
jsonBytes, err := ioutil.ReadFile(path)
if err != nil {
panic("no such file or directory: " + path)
}
var tempParis map[string]string
err = json.Unmarshal(jsonBytes, &tempParis)
if err != nil {
log.Println("translation file not well formated json", err)
}
l.LoadBucketFromMap(bucket, tempParis, append)
}
// func (l *SimpleLocalizer) SaveToFile(path string) {
// //TODO: not implemented
// panic("not implemented")
// }
func (l *SimpleLocalizer) SaveBucketToFile(bucket string, path string) {
err := l.touchFile(path)
if err != nil {
panic(err)
}
tempPairs := l.getBucketPairsWithoutPrefix(bucket)
jsonBytes, _ := json.MarshalIndent(tempPairs, "", " ")
err = ioutil.WriteFile(path, jsonBytes, 0644)
if err != nil {
panic(err)
}
}
func (l *SimpleLocalizer) PushBucketToSource(bucket string) (err error) {
terms := l.getBucketTermsWithoutPrefix(bucket)
err = l.source.Push(terms)
if err != nil {
log.Fatal(err)
return
}
return
}
func (l *SimpleLocalizer) PullBucketFromSource(bucket string, append bool) (err error) {
terms, err := l.source.Pull()
if err != nil {
log.Fatal(err)
return
}
l.LoadBucketFromMap(bucket, terms, append)
return
}
func (l *SimpleLocalizer) removeBucket(bucket string) {
for k := range l.pairs {
if strings.HasPrefix(k, l.genKey(bucket, "")) {
delete(l.pairs, k)
}
}
}
func (l *SimpleLocalizer) getBucketTermsWithoutPrefix(bucket string) (terms []string) {
tempPairs := l.getBucketPairsWithoutPrefix(bucket)
for k := range tempPairs {
terms = append(terms, k)
}
return
}
func (l *SimpleLocalizer) getBucketPairsWithoutPrefix(bucket string) map[string]string {
tempPairs := map[string]string{}
for k, v := range l.pairs {
if !strings.HasPrefix(k, bucket) {
continue
}
tempPairs[l.removeKeyPrefix(bucket, k)] = v
}
return tempPairs
}
func (l *SimpleLocalizer) touchFile(path string) error {
directoryPath := filepath.Dir(path)
if _, err := os.Stat(directoryPath); os.IsNotExist(err) {
err := os.MkdirAll(directoryPath, 0755)
if err != nil {
return err
}
}
file, err := os.OpenFile(path, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
return file.Close()
}
func (l *SimpleLocalizer) genKey(bucket string, key string) string {
return bucket + seperator + key
}
func (l *SimpleLocalizer) removeKeyPrefix(bucket string, key string) string {
return strings.Replace(key, bucket+seperator, "", 1)
}
func NewSimpleLocalizer(source *PoeditorSource) *SimpleLocalizer {
return &SimpleLocalizer{
source: source,
}
}