-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n.go
301 lines (266 loc) Β· 7.06 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
299
300
301
package i18n
import (
"encoding/json"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"text/template"
)
// Pluralizor decides which translation string to use by the returned index.
type Pluralizor func(number, choices int) int
// Unmarshaler unmarshals the translation files, can be `json.Unmarshal` or `yaml.Unmarshal`.
type Unmarshaler func(data []byte, v any) error
// I18n is the main internationalization core.
type I18n struct {
defaultLocale string
pluralizors map[string]Pluralizor
unmarshaler Unmarshaler
fallbacks map[string][]string
translations map[string]map[string]string
runtimeCompiledTranslations map[string]*compiledTranslation
compiledTranslations map[string]map[string]*compiledTranslation
}
// WithUnmarshaler replaces the default translation file unmarshaler.
func WithUnmarshaler(u Unmarshaler) func(*I18n) {
return func(i *I18n) {
i.unmarshaler = u
}
}
// WithFallback changes fallback settings.
func WithFallback(f map[string][]string) func(*I18n) {
return func(i *I18n) {
i.fallbacks = f
}
}
// WithPluralizor changes pluralizors.
func WithPluralizor(p map[string]Pluralizor) func(*I18n) {
return func(i *I18n) {
i.pluralizors = p
}
}
// New creates a new internationalization.
func New(defaultLocale string, options ...func(*I18n)) *I18n {
i := &I18n{
defaultLocale: nameInsenstive(defaultLocale),
unmarshaler: json.Unmarshal,
pluralizors: make(map[string]Pluralizor),
fallbacks: make(map[string][]string),
translations: make(map[string]map[string]string),
runtimeCompiledTranslations: make(map[string]*compiledTranslation),
compiledTranslations: make(map[string]map[string]*compiledTranslation),
}
for _, o := range options {
o(i)
}
return i
}
// LoadMap loads the translations from the map.
func (i *I18n) LoadMap(languages map[string]map[string]string) error {
for locale, translations := range languages {
locale = nameInsenstive(locale)
i.compiledTranslations[locale] = make(map[string]*compiledTranslation)
for name, text := range translations {
trans := i.compileTranslation(locale, name, text)
i.compiledTranslations[locale][name] = trans
}
}
i.compileFallbacks()
return nil
}
// LoadFiles loads the translations from the files.
func (i *I18n) LoadFiles(filenames ...string) error {
data := make(map[string]map[string]string)
for _, v := range filenames {
b, err := os.ReadFile(v)
if err != nil {
return err
}
var trans map[string]string
if err := i.unmarshaler(b, &trans); err != nil {
return err
}
locale := nameInsenstive(v)
_, ok := data[locale]
if !ok {
data[locale] = make(map[string]string)
}
for name, text := range trans {
data[locale][name] = text
}
}
return i.LoadMap(data)
}
// LoadGlob loads the translations from the files that matches specified patterns.
func (i *I18n) LoadGlob(pattern ...string) error {
var files []string
for _, pattern := range pattern {
v, err := filepath.Glob(pattern)
if err != nil {
return err
}
files = append(files, v...)
}
return i.LoadFiles(files...)
}
// LoadFS loads the translation from a `fs.FS`, useful for `go:embed`.
func (i *I18n) LoadFS(fsys fs.FS, patterns ...string) error {
var files []string
data := make(map[string]map[string]string)
for _, pattern := range patterns {
v, err := fs.Glob(fsys, pattern)
if err != nil {
return err
}
files = append(files, v...)
}
for _, v := range files {
b, err := fs.ReadFile(fsys, v)
if err != nil {
return err
}
var trans map[string]string
if err := i.unmarshaler(b, &trans); err != nil {
return err
}
locale := nameInsenstive(v)
_, ok := data[locale]
if !ok {
data[locale] = make(map[string]string)
}
for name, text := range trans {
data[locale][name] = text
}
}
return i.LoadMap(data)
}
// NewLocale reads a locale from the internationalization core.
func (i *I18n) NewLocale(locales ...string) *Locale {
selectedLocale := i.defaultLocale
for _, v := range locales {
v = nameInsenstive(v)
if _, ok := i.compiledTranslations[v]; ok {
selectedLocale = v
break
}
}
return &Locale{
parent: i,
locale: selectedLocale,
}
}
var contextRegExp = regexp.MustCompile("<(.*?)>$")
// compiledTranslation
type compiledTranslation struct {
locale string
name string
pluralizor Pluralizor
texts []*compiledText
}
// compiledText
type compiledText struct {
text string
tmpl *template.Template
}
// defaultPluralizor
func defaultPluralizor(number, choices int) int {
switch choices {
case 2:
switch number {
case 0, 1:
return 0
default:
return 1
}
default:
switch number {
case 0:
return 0
case 1:
return 1
default:
return 2
}
}
}
// pluralizor
func (i *I18n) pluralizor(lang string) Pluralizor {
v, ok := i.pluralizors[lang]
if !ok {
return defaultPluralizor
}
return v
}
// trimContext
func trimContext(v string) string {
return contextRegExp.ReplaceAllString(v, "")
}
// compileTranslation
func (i *I18n) compileTranslation(locale, name, text string) *compiledTranslation {
compTrans := &compiledTranslation{
name: name,
}
compTrans.locale = locale
compTrans.pluralizor = i.pluralizor(locale)
compTrans.texts = compileText(text)
return compTrans
}
// compileText
func compileText(text string) (compTexts []*compiledText) {
texts := strings.Split(text, " | ")
for _, v := range texts {
compText := &compiledText{}
if strings.Contains(v, "{{") {
t, _ := template.New("").Parse(v)
compText.tmpl = t
} else {
compText.text = v
}
compTexts = append(compTexts, compText)
}
return
}
// nameInsenstive converts `zh_TW.music.json`, `zh_TW` and `zh-TW` to `zh-tw`.
func nameInsenstive(v string) string {
v = filepath.Base(v)
v = strings.Split(v, ".")[0]
v = strings.ToLower(v)
v = strings.ReplaceAll(v, "_", "-")
return v
}
// compileFallbacks
func (i *I18n) compileFallbacks() {
for _, grandTrans := range i.compiledTranslations[i.defaultLocale] {
for locale, trans := range i.compiledTranslations {
//
if locale == i.defaultLocale {
continue
}
//
if _, ok := trans[grandTrans.name]; !ok {
if bestfit := i.lookupBestFallback(locale, grandTrans.name); bestfit != nil {
i.compiledTranslations[locale][grandTrans.name] = bestfit
}
}
}
}
}
// lookupBestFallback
func (i *I18n) lookupBestFallback(locale, name string) *compiledTranslation {
fallbacks, ok := i.fallbacks[locale]
if !ok {
if v, ok := i.compiledTranslations[i.defaultLocale][name]; ok {
return v
}
}
for _, fallback := range fallbacks {
if v, ok := i.compiledTranslations[fallback][name]; ok {
return v
}
if j := i.lookupBestFallback(fallback, name); j != nil {
return j
}
}
return nil
}