-
Notifications
You must be signed in to change notification settings - Fork 2
/
i18n_test.go
348 lines (306 loc) · 10.8 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
package valix
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"net/http"
"strings"
"testing"
)
func TestFallbackI18nContext(t *testing.T) {
i18ctx := DefaultI18nProvider.DefaultContext()
require.Equal(t, fallbackI18nContext, i18ctx)
}
func TestEnsureNonNilI18nContext(t *testing.T) {
defer func() {
DefaultI18nProvider = fallbackI18nProvider
}()
i18ctx := obtainI18nContext(nil)
require.NotNil(t, i18ctx)
DefaultI18nProvider = nil
i18ctx = obtainI18nContext(nil)
require.NotNil(t, i18ctx)
}
func TestCanTranslateToken(t *testing.T) {
tcx := &defaultI18nContext{lang: "en"}
s := tcx.TranslateToken("xxxx")
require.Equal(t, "xxxx", s)
s = tcx.TranslateToken("century")
require.Equal(t, "century", s)
s = tcx.TranslateToken("century...")
require.Equal(t, "centuries", s)
}
func TestCanTranslateMessage(t *testing.T) {
tcx := &defaultI18nContext{lang: "en"}
s := tcx.TranslateMessage("xxxx")
require.Equal(t, "xxxx", s)
s = tcx.TranslateMessage(msgUnableToDecode)
require.Equal(t, msgUnableToDecode, s)
}
func TestCanTranslateFormat(t *testing.T) {
tcx := &defaultI18nContext{lang: "en"}
s := tcx.TranslateFormat("xxxx %d", 1)
require.Equal(t, "xxxx 1", s)
s = tcx.TranslateFormat(fmtMsgValueExpectedType, "foo")
require.Equal(t, fmt.Sprintf(fmtMsgValueExpectedType, "foo"), s)
}
func TestCanMarshalDefaultI18nContext(t *testing.T) {
_, err := json.Marshal(&defaultI18nContext{lang: "en"})
require.NoError(t, err)
}
func TestAllTokensTranslated(t *testing.T) {
require.Equal(t, len(internalTokens), len(defaultInternalTranslator.Tokens))
for k := range internalTokens {
_, ok := defaultInternalTranslator.Tokens[k]
require.True(t, ok)
}
}
func TestAllMessagesTranslated(t *testing.T) {
require.Equal(t, len(internalMessages), len(defaultInternalTranslator.Messages))
for k := range internalMessages {
_, ok := defaultInternalTranslator.Messages[k]
require.True(t, ok)
}
}
func TestAllFormatMessagesTranslated(t *testing.T) {
require.Equal(t, len(internalFormats), len(defaultInternalTranslator.Formats))
for k := range internalFormats {
_, ok := defaultInternalTranslator.Formats[k]
require.True(t, ok)
}
}
func TestDefaultLanguage(t *testing.T) {
defer func() {
DefaultLanguage = "en"
}()
lang := defaultLanguage("")
require.Equal(t, "en", lang)
lang = defaultLanguage("EN")
require.Equal(t, "en", lang)
lang = defaultLanguage("fr")
require.Equal(t, "fr", lang)
lang = defaultLanguage("xx")
require.Equal(t, "en", lang)
DefaultLanguage = "xx"
lang = defaultLanguage("xx")
require.Equal(t, "en", lang)
lang = defaultLanguage("en")
require.Equal(t, "en", lang)
lang = defaultLanguage("")
require.Equal(t, "en", lang)
lang = defaultLanguage("fr")
require.Equal(t, "fr", lang)
}
func TestDefaultFallbackLanguage(t *testing.T) {
defer func() {
DefaultFallbackLanguages = map[string]string{}
}()
i18ctx := newDefaultI18nContext("mt", "")
require.Equal(t, "en", i18ctx.Language())
DefaultFallbackLanguages["mt"] = "IT"
i18ctx = newDefaultI18nContext("mt", "")
require.Equal(t, "it", i18ctx.Language())
}
func TestRegionTranslations(t *testing.T) {
tcx := &defaultI18nContext{
lang: "en",
region: "US",
}
msg := tcx.TranslateMessage(msgPresetRgb)
require.Equal(t, "Value must be a valid rgb() color string", msg)
msg = tcx.TranslateMessage(msgPresetPublication)
require.Equal(t, msgPresetPublication, msg)
tcx.region = ""
msg = tcx.TranslateMessage(msgPresetRgb)
require.Equal(t, msgPresetRgb, msg)
tcx.region = "US"
DefaultTranslator.AddMessageLanguageTranslation("en", "test", "TEST ENGLISH",
RegionalVariantTranslation{"US", "TEST ENGLISH US"})
msg = tcx.TranslateMessage("test")
require.Equal(t, "TEST ENGLISH US", msg)
tcx.region = "GB"
msg = tcx.TranslateMessage("test")
require.Equal(t, "TEST ENGLISH", msg)
DefaultTranslator.AddMessageRegionTranslation("en", "GB", "test", "TEST ENGLISH GB")
msg = tcx.TranslateMessage("test")
require.Equal(t, "TEST ENGLISH GB", msg)
tcx.region = "US"
DefaultTranslator.AddTokenLanguageTranslation("en", "test", "TEST ENGLISH",
RegionalVariantTranslation{"US", "TEST ENGLISH US"})
msg = tcx.TranslateToken("test")
require.Equal(t, "TEST ENGLISH US", msg)
tcx.region = "GB"
msg = tcx.TranslateToken("test")
require.Equal(t, "TEST ENGLISH", msg)
DefaultTranslator.AddTokenRegionTranslation("en", "GB", "test", "TEST ENGLISH GB")
msg = tcx.TranslateToken("test")
require.Equal(t, "TEST ENGLISH GB", msg)
tcx.region = "US"
DefaultTranslator.AddFormatLanguageTranslation("en", "TEST %d", "TEST ENGLISH %d",
RegionalVariantTranslation{"US", "%d TEST ENGLISH US"})
msg = tcx.TranslateFormat("TEST %d", 1)
require.Equal(t, "1 TEST ENGLISH US", msg)
tcx.region = "GB"
msg = tcx.TranslateFormat("TEST %d", 2)
require.Equal(t, "TEST ENGLISH 2", msg)
DefaultTranslator.AddFormatRegionTranslation("en", "GB", "TEST %d", "TEST ENGLISH GB %d")
msg = tcx.TranslateFormat("TEST %d", 3)
require.Equal(t, "TEST ENGLISH GB 3", msg)
}
func TestTranslatorTokenAdditions(t *testing.T) {
const testToken = "TEST_TEST"
msg := DefaultTranslator.TranslateToken("en", "", testToken)
require.Equal(t, testToken, msg)
l := len(defaultInternalTranslator.Tokens)
DefaultTranslator.AddTokenLanguageTranslation("en", testToken, "TEST IN ENGLISH", RegionalVariantTranslation{"gb", ""})
require.Equal(t, l+1, len(defaultInternalTranslator.Tokens))
msg = DefaultTranslator.TranslateToken("en", "", testToken)
require.Equal(t, "TEST IN ENGLISH", msg)
msg = DefaultTranslator.TranslateToken("en", "GB", testToken)
require.Equal(t, "TEST IN ENGLISH", msg)
const testToken2 = "TEST_TEST_2"
DefaultTranslator.AddTokenRegionTranslation("en", "GB", testToken2, "ANOTHER TEST IN ENGLISH")
require.Equal(t, l+2, len(defaultInternalTranslator.Tokens))
msg = DefaultTranslator.TranslateToken("en", "", testToken2)
require.Equal(t, "ANOTHER TEST IN ENGLISH", msg)
msg = DefaultTranslator.TranslateToken("en", "GB", testToken2)
require.Equal(t, "ANOTHER TEST IN ENGLISH", msg)
}
func TestTranslatorMessageAdditions(t *testing.T) {
const testMessage = "TEST_TEST"
msg := DefaultTranslator.TranslateMessage("en", "", testMessage)
require.Equal(t, testMessage, msg)
l := len(defaultInternalTranslator.Messages)
DefaultTranslator.AddMessageLanguageTranslation("en", testMessage, "TEST IN ENGLISH", RegionalVariantTranslation{"gb", ""})
require.Equal(t, l+1, len(defaultInternalTranslator.Messages))
msg = DefaultTranslator.TranslateMessage("en", "", testMessage)
require.Equal(t, "TEST IN ENGLISH", msg)
msg = DefaultTranslator.TranslateMessage("en", "GB", testMessage)
require.Equal(t, "TEST IN ENGLISH", msg)
const testMessage2 = "TEST_TEST_2"
DefaultTranslator.AddMessageRegionTranslation("en", "GB", testMessage2, "ANOTHER TEST IN ENGLISH")
require.Equal(t, l+2, len(defaultInternalTranslator.Messages))
msg = DefaultTranslator.TranslateMessage("en", "", testMessage2)
require.Equal(t, "ANOTHER TEST IN ENGLISH", msg)
msg = DefaultTranslator.TranslateMessage("en", "GB", testMessage2)
require.Equal(t, "ANOTHER TEST IN ENGLISH", msg)
}
func TestTranslatorFormatAdditions(t *testing.T) {
const testFormat = "TEST_TEST %d"
msg := DefaultTranslator.TranslateFormat("en", "", testFormat, 10)
require.Equal(t, fmt.Sprintf(testFormat, 10), msg)
l := len(defaultInternalTranslator.Formats)
DefaultTranslator.AddFormatLanguageTranslation("en", testFormat, "TEST IN ENGLISH %d", RegionalVariantTranslation{"gb", ""})
require.Equal(t, l+1, len(defaultInternalTranslator.Formats))
msg = DefaultTranslator.TranslateFormat("en", "", testFormat, 11)
require.Equal(t, "TEST IN ENGLISH 11", msg)
msg = DefaultTranslator.TranslateFormat("en", "GB", testFormat, 12)
require.Equal(t, "TEST IN ENGLISH 12", msg)
const testFormat2 = "TEST_TEST_2 %d"
DefaultTranslator.AddFormatRegionTranslation("en", "GB", testFormat2, "ANOTHER TEST IN ENGLISH %d")
require.Equal(t, l+2, len(defaultInternalTranslator.Formats))
msg = DefaultTranslator.TranslateFormat("en", "", testFormat2, 13)
require.Equal(t, "ANOTHER TEST IN ENGLISH 13", msg)
msg = DefaultTranslator.TranslateFormat("en", "GB", testFormat2, 14)
require.Equal(t, "ANOTHER TEST IN ENGLISH 14", msg)
}
func TestRequestLanguage(t *testing.T) {
testCases := []struct {
header string
lang string
region string
}{
{
"nn;q=0.3, en;q=0.8, en-US,",
"en",
"US",
},
{
"en-gb, en-US, en,",
"en",
"GB",
},
{
"fr, it, es, de, en,",
"fr",
"",
},
{
"fr;q=0.5, fr-CA, it, es, de, en,",
"fr",
"CA",
},
{
"gsw, en;q=0.7, en-US;q=0.8",
"en",
"US",
},
{
"gsw, nl, da",
"en",
"",
},
{
"invalid",
"en",
"",
},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("[%d]Accept-Language=\"%s\"", i+1, tc.header), func(t *testing.T) {
r, _ := http.NewRequest("GET", "example.com", strings.NewReader("Hello"))
r.Header.Set("Accept-Language", tc.header)
tcx := DefaultI18nProvider.ContextFromRequest(r)
require.NotNil(t, tcx)
require.Equal(t, tc.lang, tcx.Language())
require.Equal(t, tc.region, tcx.Region())
})
}
}
func TestValidationMessagesTranslated(t *testing.T) {
foundLang := ""
foundRegion := ""
validator := &Validator{
Constraints: Constraints{
NewCustomConstraint(func(value interface{}, vcx *ValidatorContext, this *CustomConstraint) (passed bool, message string) {
foundLang = vcx.Language()
foundRegion = vcx.Region()
return true, ""
}, ""),
},
Properties: Properties{
"foo": {
NotNull: true,
Constraints: Constraints{
&StringPresetPattern{Preset: PresetRgb},
},
},
},
}
r, _ := http.NewRequest("GET", "example.com", strings.NewReader(`{"foo": null}`))
r.Header.Set("Accept-Language", "fr")
ok, violations, _ := validator.RequestValidate(r)
require.False(t, ok)
require.Equal(t, 1, len(violations))
require.Equal(t, "La valeur ne peut pas être nulle", violations[0].Message)
require.Equal(t, "fr", foundLang)
require.Equal(t, "", foundRegion)
r, _ = http.NewRequest("GET", "example.com", strings.NewReader(`null`))
r.Header.Set("Accept-Language", "es")
ok, violations, _ = validator.RequestValidate(r)
require.False(t, ok)
require.Equal(t, 1, len(violations))
require.Equal(t, "El cuerpo de la solicitud no debe ser JSON nulo", violations[0].Message)
r, _ = http.NewRequest("GET", "example.com", strings.NewReader(`{"foo": "not an rgb string!"}`))
r.Header.Set("Accept-Language", "en")
ok, violations, _ = validator.RequestValidate(r)
require.False(t, ok)
require.Equal(t, 1, len(violations))
require.Equal(t, "Value must be a valid rgb() colour string", violations[0].Message)
r, _ = http.NewRequest("GET", "example.com", strings.NewReader(`{"foo": "not an rgb string!"}`))
r.Header.Set("Accept-Language", "en-us")
ok, violations, _ = validator.RequestValidate(r)
require.False(t, ok)
require.Equal(t, 1, len(violations))
require.Equal(t, "Value must be a valid rgb() color string", violations[0].Message)
}