-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18n_test.go
371 lines (303 loc) Β· 11.2 KB
/
i18n_test.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package i18n
import (
"embed"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
)
//go:embed test/*.json
var testTranslationFS embed.FS
var testTranslations = map[string]map[string]string{
"en-us": map[string]string{
"None | 1 Apple | {{ .Count }} Apples": "None | 1 Apple | {{ .Count }} Apples",
},
"zh-tw": map[string]string{
// Token-based Translations
"test_message": "ιζ―δΈε測試θ¨ζ―γ",
"test_template": "δ½ ε₯½οΌ{{ .Name }}οΌ",
"test_plural": "ζ²ζ | εͺζ 1 ε | ζ {{.Count}} ε",
// Text-based Translations.
"Hello, world!": "δ½ ε₯½οΌδΈηοΌ",
"How are you, {{ .Name }}?": "ιεΎε¦δ½οΌ{{ .Name }}οΌ",
"Post <verb>": "ηΌθ‘¨θ²Όζ",
"Post <noun>": "ζη« ",
"None | 1 Apple | {{ .Count }} Apples": "ζ²ζθζ | 1 ι‘θζ | ζ {{.Count}} ι‘θζ",
"No Post | 1 Post | {{ .Count }} Posts <noun>": "ζ²ζζη« | 1 η―ζη« | ζ {{.Count}} η―ζη« ",
"No Post | 1 Post | {{ .Count }} Posts <verb>": "ζ²ζηΌθ‘¨ | 1 η―ηΌθ‘¨ | ζ {{.Count}} η―ηΌθ‘¨",
"Post": "THIS_SHOULD_NOT_BE_USED",
"No Post | 1 Post | {{ .Count }} Posts": "THIS_SHOULD_NOT_BE_USED | THIS_SHOULD_NOT_BE_USED | THIS_SHOULD_NOT_BE_USED",
},
"ja-jp": map[string]string{
// Token-based Translations
"test_message": "γγγ―γγΉγγ‘γγ»γΌγΈγ§γγ",
"test_template": "γγγ«γ‘γ―γ{{ .Name }}οΌ",
"test_plural": "γͺγ | 1 γ€γ γ | {{.Count}} εγγγΎγ",
},
"ko-kr": map[string]string{
// Token-based Translations
"test_message": "μ΄κ²μ ν
μ€νΈ λ©μμ§μ
λλ€.",
"test_template": "μλ
νμΈμ, {{ .Name }} λ!",
"test_plural": "μμ | 1 κ° | {{.Count}} κ°κ° μμ",
// Text-based Translations.
"Hello, world!": "μλ
νμΈμ, μΈμ!",
"How are you, {{ .Name }}?": "{{ .Name }} λ, μ΄λ»κ² μ§λ΄μΈμ?",
"Post <verb>": "λ©μμ§ κ²μ",
"Post <noun>": "κΈ°μ¬",
},
}
func newTestLocale() *Locale {
i := New("zh-tw")
i.LoadMap(testTranslations)
return i.NewLocale("zh-tw")
}
func TestLoadMap(t *testing.T) {
assert := assert.New(t)
i := New("zh-tw")
i.LoadMap(testTranslations)
l := i.NewLocale("zh-tw")
assert.Equal("ιζ―δΈε測試θ¨ζ―γ", l.String("test_message"))
assert.Equal("not_exists_message", l.String("not_exists_message"))
}
func TestLoadFiles(t *testing.T) {
assert := assert.New(t)
i := New("zh-tw")
assert.NoError(i.LoadFiles("test/zh-tw.json", "test/zh_TW.json", "test/zh_tw.hello.json"))
l := i.NewLocale("zh-tw")
assert.Equal("θ¨ζ― A", l.String("message_a"))
assert.Equal("θ¨ζ― B", l.String("message_b"))
assert.Equal("θ¨ζ― C", l.String("message_c"))
}
func TestLoadGlob(t *testing.T) {
assert := assert.New(t)
i := New("zh-tw")
assert.NoError(i.LoadGlob("test/*.json"))
l := i.NewLocale("zh-tw")
assert.Equal("θ¨ζ― A", l.String("message_a"))
assert.Equal("θ¨ζ― B", l.String("message_b"))
assert.Equal("θ¨ζ― C", l.String("message_c"))
}
func TestLoadFS(t *testing.T) {
assert := assert.New(t)
i := New("zh-tw")
assert.NoError(i.LoadFS(testTranslationFS, "test/*.json"))
l := i.NewLocale("zh-tw")
assert.Equal("θ¨ζ― A", l.String("message_a"))
assert.Equal("θ¨ζ― B", l.String("message_b"))
assert.Equal("θ¨ζ― C", l.String("message_c"))
}
func TestPluralizor(t *testing.T) {
assert := assert.New(t)
i := New("ru", WithPluralizor(map[string]Pluralizor{
"ru": func(number, choices int) int {
if number == 0 {
return 0
}
teen := number > 10 && number < 20
endsWithOne := number%10 == 1
if choices < 4 {
if !teen && endsWithOne {
return 1
} else {
return 2
}
}
if !teen && endsWithOne {
return 1
}
if !teen && number%10 >= 2 && number%10 <= 4 {
return 2
}
if choices < 4 {
return 2
}
return 3
},
}))
l := i.NewLocale("ru")
assert.Equal("0 ΠΌΠ°ΡΠΈΠ½", l.Number("0 ΠΌΠ°ΡΠΈΠ½ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Π° | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Ρ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½", 0, map[string]any{
"Count": 0,
}))
assert.Equal("1 ΠΌΠ°ΡΠΈΠ½Π°", l.Number("0 ΠΌΠ°ΡΠΈΠ½ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Π° | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Ρ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½", 1, map[string]any{
"Count": 1,
}))
assert.Equal("2 ΠΌΠ°ΡΠΈΠ½Ρ", l.Number("0 ΠΌΠ°ΡΠΈΠ½ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Π° | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Ρ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½", 2, map[string]any{
"Count": 2,
}))
assert.Equal("12 ΠΌΠ°ΡΠΈΠ½", l.Number("0 ΠΌΠ°ΡΠΈΠ½ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Π° | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Ρ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½", 12, map[string]any{
"Count": 12,
}))
assert.Equal("21 ΠΌΠ°ΡΠΈΠ½Π°", l.Number("0 ΠΌΠ°ΡΠΈΠ½ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Π° | {{ .Count }} ΠΌΠ°ΡΠΈΠ½Ρ | {{ .Count }} ΠΌΠ°ΡΠΈΠ½", 21, map[string]any{
"Count": 21,
}))
}
func TestUnmarshaler(t *testing.T) {
assert := assert.New(t)
i := New("zh-tw", WithUnmarshaler(yaml.Unmarshal))
assert.NoError(i.LoadFiles("test/zh_tW.yml"))
l := i.NewLocale("zh-tw")
assert.Equal("θ¨ζ― A", l.String("message_a"))
}
func TestLocale(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("zh-tw", l.Locale())
}
func TestTokenString(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("ιζ―δΈε測試θ¨ζ―γ", l.String("test_message"))
assert.Equal("not_exists_message", l.String("not_exists_message"))
}
func TestTokenTmpl(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("δ½ ε₯½οΌYamiοΌ", l.String("test_template", map[string]string{
"Name": "Yami",
}))
}
func TestTokenPlural(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("ζ²ζ", l.Number("test_plural", 0))
assert.Equal("εͺζ 1 ε", l.Number("test_plural", 1))
assert.Equal("ζ 2 ε", l.Number("test_plural", 2, map[string]int{
"Count": 2,
}))
// Lazy template
assert.Equal("ζ²ζ", l.Number("test_plural", 0, map[string]int{
"Count": 2,
}))
assert.Equal("εͺζ 1 ε", l.Number("test_plural", 1, map[string]int{
"Count": 2,
}))
assert.Equal("ζ 2 ε", l.Number("test_plural", 2, map[string]int{
"Count": 2,
}))
}
func TestTextString(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("δ½ ε₯½οΌδΈηοΌ", l.String("Hello, world!"))
}
func TestTextStringRaw(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("I'm fine thank you!", l.String("I'm fine thank you!"))
}
func TestTextTmpl(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("ιεΎε¦δ½οΌYamiοΌ", l.String("How are you, {{ .Name }}?", map[string]string{
"Name": "Yami",
}))
}
func TestTextTmplRaw(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("I'm fine, thanks to Yami!", l.String("I'm fine, thanks to {{ .Name }}!", map[string]string{
"Name": "Yami",
}))
}
func TestTextPlural(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("ζ²ζθζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 0))
assert.Equal("1 ι‘θζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 1))
assert.Equal("ζ 2 ι‘θζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 2, map[string]int{
"Count": 2,
}))
// Lazy template
assert.Equal("ζ²ζθζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 0, map[string]int{
"Count": 2,
}))
assert.Equal("1 ι‘θζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 1, map[string]int{
"Count": 2,
}))
assert.Equal("ζ 2 ι‘θζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 2, map[string]int{
"Count": 2,
}))
}
func TestTextPluralRaw(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("Zero", l.Number("Zero | 1 Thing | {{ .Count }} Things", 0))
assert.Equal("1 Thing", l.Number("Zero | 1 Thing | {{ .Count }} Things", 1))
assert.Equal("2 Things", l.Number("Zero | 1 Thing | {{ .Count }} Things", 2, map[string]int{
"Count": 2,
}))
}
func TestTextStringContext(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("ηΌθ‘¨θ²Όζ", l.StringX("Post", "verb"))
assert.Equal("ζη« ", l.StringX("Post", "noun"))
}
func TestTextPluralContext(t *testing.T) {
assert := assert.New(t)
l := newTestLocale()
assert.Equal("ζ²ζζη« ", l.NumberX("No Post | 1 Post | {{ .Count }} Posts", "noun", 0))
assert.Equal("1 η―ζη« ", l.NumberX("No Post | 1 Post | {{ .Count }} Posts", "noun", 1))
assert.Equal("ζ 2 η―ζη« ", l.NumberX("No Post | 1 Post | {{ .Count }} Posts", "noun", 2, map[string]int{
"Count": 2,
}))
// Lazy template
assert.Equal("ζ²ζζη« ", l.NumberX("No Post | 1 Post | {{ .Count }} Posts", "noun", 0, map[string]int{
"Count": 2,
}))
assert.Equal("1 η―ζη« ", l.NumberX("No Post | 1 Post | {{ .Count }} Posts", "noun", 1, map[string]int{
"Count": 2,
}))
assert.Equal("ζ 2 η―ζη« ", l.NumberX("No Post | 1 Post | {{ .Count }} Posts", "noun", 2, map[string]int{
"Count": 2,
}))
//
assert.Equal("ζ²ζηΌθ‘¨", l.NumberX("No Post | 1 Post | {{ .Count }} Posts", "verb", 0))
assert.Equal("1 η―ηΌθ‘¨", l.NumberX("No Post | 1 Post | {{ .Count }} Posts", "verb", 1))
assert.Equal("ζ 2 η―ηΌθ‘¨", l.NumberX("No Post | 1 Post | {{ .Count }} Posts", "verb", 2, map[string]int{
"Count": 2,
}))
}
func TestTextFallback(t *testing.T) {
assert := assert.New(t)
i := New("zh-tw", WithFallback(map[string][]string{
"ja-jp": []string{"ko-kr"},
}))
i.LoadMap(testTranslations)
l := i.NewLocale("ja-jp")
// Test ja-jp
assert.Equal("γγγ―γγΉγγ‘γγ»γΌγΈγ§γγ", l.String("test_message"))
assert.Equal("γγγ«γ‘γ―γYamiοΌ", l.String("test_template", map[string]string{
"Name": "Yami",
}))
assert.Equal("γͺγ", l.Number("test_plural", 0))
// Test ja-jp -> ko-kr fallback
assert.Equal("μλ
νμΈμ, μΈμ!", l.String("Hello, world!"))
assert.Equal("Yami λ, μ΄λ»κ² μ§λ΄μΈμ?", l.String("How are you, {{ .Name }}?", map[string]string{
"Name": "Yami",
}))
assert.Equal("λ©μμ§ κ²μ", l.StringX("Post", "verb"))
// Test ja-jp -> zh-tw fallback
assert.Equal("ζ²ζθζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 0))
assert.Equal("1 ι‘θζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 1))
assert.Equal("ζ 2 ι‘θζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 2, map[string]int{
"Count": 2,
}))
// Test nil fallback
assert.Equal("Ni hao", l.String("Ni hao"))
}
func TestTextFallbackResursive(t *testing.T) {
assert := assert.New(t)
i := New("en-us", WithFallback(map[string][]string{
"ja-jp": []string{"ko-kr"},
"ko-kr": []string{"zh-tw"},
}))
i.LoadMap(testTranslations)
l := i.NewLocale("ja-jp")
// Test ja-jp -> ko-kr -> zh-tw fallback
assert.Equal("1 ι‘θζ", l.Number("None | 1 Apple | {{ .Count }} Apples", 1))
}
func TestParseAcceptLanguage(t *testing.T) {
assert := assert.New(t)
assert.Equal("[zh-tw zh en-us en ja]", fmt.Sprintf("%+v", ParseAcceptLanguage("zh-TW,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6")))
}