-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.go
206 lines (163 loc) · 4.35 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package i18n
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/alobaton/i18n/extensions"
"github.com/alobaton/i18n/langs"
)
// Translate handles directories, default locale and messages.
type Translate struct {
path []string
mainLocale string
locales []string
languages map[string]interface{}
initialized bool
}
// NewTranslate create a new Config with default value.
func NewTranslate() *Translate {
return new(Translate)
}
// BindPath store the config file json path value.
func (t *Translate) BindPath(path string) *Translate {
t.path = append(t.path, path)
return t
}
// BindMainLocale store the default language code value.
func (t *Translate) BindMainLocale(mainLocale string) *Translate {
t.mainLocale = mainLocale
return t.BindLocale(mainLocale)
}
// BindLocale store a locale code value.
func (t *Translate) BindLocale(locale string) *Translate {
t.locales = append(t.locales, locale)
return t
}
// Init store the message to map variable.
func (t *Translate) Init() (*Translate, error) {
if !langs.IsValid(t.mainLocale) {
return nil, fmt.Errorf("invalid language %s. supported languages : %v", t.mainLocale, langs.All)
}
for _, l := range t.locales {
if !langs.IsValid(l) {
return nil, fmt.Errorf("invalid language %s. supported languages : %v", l, langs.All)
}
}
if t.languages == nil {
t.languages = make(map[string]interface{})
}
for _, p := range t.path {
for _, l := range t.locales {
for _, ext := range extensions.All {
path := fmt.Sprintf("%s/%s%s", p, l, ext)
f, err := os.Open(path)
if err != nil {
if os.IsNotExist(err) {
continue
}
return nil, err
}
defer f.Close()
content, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
messages := make(map[string]interface{})
err = json.Unmarshal(content, &messages)
if err != nil {
return nil, err
}
t.languages[l] = messages
}
}
}
if t.IsLoaded() {
return nil, errors.New("no languages loaded")
}
t.initialized = true
return t, nil
}
// IsLoaded check if at least one language was loaded.
func (t *Translate) IsLoaded() bool {
for _, v := range t.languages {
if v != nil {
m, ok := v.(map[string]interface{})
if ok {
if len(m) > 0 {
return false
}
}
}
}
return true
}
// Exists return true and nil if locale exists on Translate struct,
// otherwise return false and an error if applies.
func (t *Translate) Exists(locale string) (bool, error) {
if !langs.IsValid(locale) {
return false, fmt.Errorf("invalid language %s. supported languages : %v", locale, langs.All)
}
if t.languages[locale] == nil {
return false, fmt.Errorf("no messages detected for language : %v", locale)
}
if len(t.languages[locale].(map[string]interface{})) < 1 {
return false, nil
}
return true, nil
}
// Lookup the destination message based on a message key.
func (t *Translate) Lookup(key string, args ...interface{}) (string, error) {
if !t.initialized {
return key, errors.New("Translate instance not initialized")
}
locale := t.mainLocale
return t.LookupWithLocale(locale, key, args...)
}
// LookupWithLocale lookup the destination message based
// on language code and message key.
func (t *Translate) LookupWithLocale(locale, key string, args ...interface{}) (string, error) {
if !t.initialized {
return key, errors.New("Translate instance not initialized")
}
exists, err := t.Exists(locale)
if err != nil {
return key, err
}
if !exists {
return key, fmt.Errorf("locales soen't exists %s. supported languages : %v", locale, langs.All)
}
keys := strings.Split(key, ".")
message, found := lookup(keys[0], keys[1:], t.languages[locale].(map[string]interface{}), args)
if !found {
return key, fmt.Errorf("message not found for key : %v", key)
}
return message, nil
}
// lookup for a key k in a struct m. Replace args if founded
func lookup(key string, keys []string, m map[string]interface{}, args []interface{}) (string, bool) {
v := m[key]
strc, ok := v.(string)
if ok {
if len(keys) < 1 {
if len(args) < 1 {
return strc, true
}
return fmt.Sprintf(strc, args...), true
}
return "", false
}
mapc, ok := v.(map[string]interface{})
if !ok {
return "", false
}
if mapc == nil {
return "", false
}
if len(mapc) < 1 {
return "", false
}
return lookup(keys[0], keys[1:], mapc, args)
}