-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n.go
298 lines (251 loc) · 7.22 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2013 Unknwon
// Copyright 2017 Gerasimos (Makis) Maropoulos <https://github.com/@kataras>
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
// Package i18n is for app Internationalization and Localization.
package i18n
import (
"errors"
"fmt"
"reflect"
"strings"
"gopkg.in/ini.v1"
)
var (
// ErrLangAlreadyExist throwed when language is already exists.
ErrLangAlreadyExist = errors.New("lang already exists")
locales = &localeStore{store: make(map[string]*locale)}
)
// add support for multi language file per language.
// ini has already implement a *ini.File#Append
// BUT IT DOESN'T F WORKING, SO:
type localeFiles struct {
files []*ini.File
}
// Get returns a the value from the "keyName" on the "sectionName"
// by searching all loc.files.
func (loc *localeFiles) GetKey(sectionName, keyName string) (*ini.Key, error) {
for _, f := range loc.files {
// returns the first available.
// section is the same for both files if key exists.
if sec, serr := f.GetSection(sectionName); serr == nil && sec != nil {
if k, err := sec.GetKey(keyName); err == nil && k != nil {
return k, err
}
}
}
return nil, fmt.Errorf("not found")
}
// Reload reloads and parses all data sources.
func (loc *localeFiles) Reload() error {
for _, f := range loc.files {
if err := f.Reload(); err != nil {
return err
}
}
return nil
}
func (loc *localeFiles) addFile(file *ini.File) error {
loc.files = append(loc.files, file)
return loc.Reload()
}
type locale struct {
id int
lang string
langDesc string
message *localeFiles
}
type localeStore struct {
langs []string
langDescs []string
store map[string]*locale
defaultLang string
}
// Get target language string
func (d *localeStore) Get(lang, section, format string) (string, bool) {
if locale, ok := d.store[lang]; ok {
// println(lang + " language found, let's see keys")
if key, err := locale.message.GetKey(section, format); err == nil && key != nil {
// println("value for section= " + section + "and key=" + format + " found")
return key.Value(), true
}
}
if len(d.defaultLang) > 0 && lang != d.defaultLang {
// println("use the default lang: " + d.defaultLang)
return d.Get(d.defaultLang, section, format)
}
return "", false
}
func (d *localeStore) Add(lang, langDesc string, source interface{}, others ...interface{}) error {
file, err := ini.Load(source, others...)
if err != nil {
return err
}
file.BlockMode = false
// if already exists add the file on this language.
lc, ok := d.store[lang]
if !ok {
// println("add lang and init message: " + lang)
// create a new one if doesn't exist.
lc = new(locale)
lc.message = new(localeFiles)
lc.lang = lang
lc.langDesc = langDesc
lc.id = len(d.langs)
// add the first language if not exists.
d.langs = append(d.langs, lang)
d.langDescs = append(d.langDescs, langDesc)
d.store[lang] = lc
}
// println("append a file for language: " + lang)
return lc.message.addFile(file)
}
func (d *localeStore) Reload(langs ...string) (err error) {
if len(langs) == 0 {
for _, lc := range d.store {
if err = lc.message.Reload(); err != nil {
return err
}
}
} else {
for _, lang := range langs {
if lc, ok := d.store[lang]; ok {
if err = lc.message.Reload(); err != nil {
return err
}
}
}
}
return nil
}
// SetDefaultLang sets default language which is a indicator that
// when target language is not found, try find in default language again.
func SetDefaultLang(lang string) {
locales.defaultLang = lang
}
// ReloadLangs reloads locale files.
func ReloadLangs(langs ...string) error {
return locales.Reload(langs...)
}
// Count returns number of languages that are registered.
func Count() int {
return len(locales.langs)
}
// ListLangs returns list of all locale languages.
func ListLangs() []string {
langs := make([]string, len(locales.langs))
copy(langs, locales.langs)
return langs
}
func ListLangDescs() []string {
langDescs := make([]string, len(locales.langDescs))
copy(langDescs, locales.langDescs)
return langDescs
}
// IsExist returns true if given language locale exists.
func IsExist(lang string) bool {
_, ok := locales.store[lang]
return ok
}
// IsExistSimilar returns true if the language, or something similar
// exists (e.g. en-US maps to en).
// it returns the found name and whether it was able to match something.
// - PATCH by @j-lenoch.
func IsExistSimilar(lang string) (string, bool) {
_, ok := locales.store[lang]
if ok {
return lang, true
}
// remove the internationalization element from the IETF code
code := strings.Split(lang, "-")[0]
for _, lc := range locales.store {
if strings.Contains(lc.lang, code) {
return lc.lang, true
}
}
return "", false
}
// IndexLang returns index of language locale,
// it returns -1 if locale not exists.
func IndexLang(lang string) int {
if lc, ok := locales.store[lang]; ok {
return lc.id
}
return -1
}
// GetLangByIndex return language by given index.
func GetLangByIndex(index int) string {
if index < 0 || index >= len(locales.langs) {
return ""
}
return locales.langs[index]
}
func GetDescriptionByIndex(index int) string {
if index < 0 || index >= len(locales.langDescs) {
return ""
}
return locales.langDescs[index]
}
func GetDescriptionByLang(lang string) string {
return GetDescriptionByIndex(IndexLang(lang))
}
func SetMessageWithDesc(lang, langDesc string, localeFile interface{}, otherLocaleFiles ...interface{}) error {
return locales.Add(lang, langDesc, localeFile, otherLocaleFiles...)
}
// SetMessage sets the message file for localization.
func SetMessage(lang string, localeFile interface{}, otherLocaleFiles ...interface{}) error {
return SetMessageWithDesc(lang, lang, localeFile, otherLocaleFiles...)
}
// Locale represents the information of localization.
type Locale struct {
Lang string
}
// Tr translates content to target language.
func (l Locale) Tr(format string, args ...interface{}) string {
return Tr(l.Lang, format, args...)
}
// Index returns lang index of LangStore.
func (l Locale) Index() int {
return IndexLang(l.Lang)
}
// Tr translates content to target language.
func Tr(lang, format string, args ...interface{}) string {
var section string
idx := strings.IndexByte(format, '.')
if idx > 0 {
section = format[:idx]
format = format[idx+1:]
}
value, ok := locales.Get(lang, section, format)
if ok {
format = value
}
if len(args) > 0 {
params := make([]interface{}, 0, len(args))
for _, arg := range args {
if arg == nil {
continue
}
val := reflect.ValueOf(arg)
if val.Kind() == reflect.Slice {
for i := 0; i < val.Len(); i++ {
params = append(params, val.Index(i).Interface())
}
} else {
params = append(params, arg)
}
}
return fmt.Sprintf(format, params...)
}
return format
}