-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathi18n_test.go
More file actions
292 lines (259 loc) · 8 KB
/
i18n_test.go
File metadata and controls
292 lines (259 loc) · 8 KB
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
/*
Copyright 2016 The Kubernetes Authors.
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
import (
"os"
"sync"
"testing"
"github.com/chai2010/gettext-go"
)
var knownTestLocale = "zh_CN.UTF-8"
func TestTranslation(t *testing.T) {
err := LoadTranslations("test", func() string { return defaultLanguage })
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
result := T("test_string")
if result != "foo" {
t.Errorf("expected: %s, saw: %s", "foo", result)
}
}
func TestTranslationPlural(t *testing.T) {
err := LoadTranslations("test", func() string { return defaultLanguage })
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
result := T("test_plural", 3)
if result != "there were 3 items" {
t.Errorf("expected: %s, saw: %s", "there were 3 items", result)
}
result = T("test_plural", 1)
if result != "there was 1 item" {
t.Errorf("expected: %s, saw: %s", "there was 1 item", result)
}
}
func TestTranslationUsingEnvVar(t *testing.T) {
// We must backup and restore env vars before setting test values in tests
// othervise we are risking to break other tests/test cases
// which rely on the same env vars
envVarsToBackup := []string{"LC_MESSAGES", "LANG", "LC_ALL"}
expectedStrEnUSLocale := "baz"
expectedStrFallback := "foo"
testCases := []struct {
name string
setenv map[string]string
expectedStr string
}{
{
name: "Only LC_ALL is set",
setenv: map[string]string{"LC_ALL": knownTestLocale},
expectedStr: expectedStrEnUSLocale,
},
{
name: "Only LC_MESSAGES is set",
setenv: map[string]string{"LC_MESSAGES": knownTestLocale},
expectedStr: expectedStrEnUSLocale,
},
{
name: "Only LANG",
setenv: map[string]string{"LANG": knownTestLocale},
expectedStr: expectedStrEnUSLocale,
},
{
name: "LC_MESSAGES overrides LANG",
setenv: map[string]string{
"LANG": "be_BY.UTF-8", // Unknown locale
"LC_MESSAGES": knownTestLocale,
},
expectedStr: expectedStrEnUSLocale,
},
{
name: "LC_ALL overrides LANG",
setenv: map[string]string{
"LANG": "be_BY.UTF-8", // Unknown locale
"LC_ALL": knownTestLocale,
},
expectedStr: expectedStrEnUSLocale,
},
{
name: "LC_ALL overrides LC_MESSAGES",
setenv: map[string]string{
"LC_MESSAGES": "be_BY.UTF-8", // Unknown locale
"LC_ALL": knownTestLocale,
},
expectedStr: expectedStrEnUSLocale,
},
{
name: "Unknown locale in LANG",
setenv: map[string]string{"LANG": "be_BY.UTF-8"},
expectedStr: expectedStrFallback,
},
{
name: "Unknown locale in LC_MESSAGES",
setenv: map[string]string{"LC_MESSAGES": "be_BY.UTF-8"},
expectedStr: expectedStrFallback,
},
{
name: "Unknown locale in LC_ALL",
setenv: map[string]string{"LC_ALL": "be_BY.UTF-8"},
expectedStr: expectedStrFallback,
},
{
name: "Invalid env var",
setenv: map[string]string{"LC_MESSAGES": "fake.locale.UTF-8"},
expectedStr: expectedStrFallback,
},
{
name: "No env vars",
expectedStr: expectedStrFallback,
},
}
for _, test := range testCases {
t.Run(test.name, func(t *testing.T) {
for _, envVar := range envVarsToBackup {
if envVarValue := os.Getenv(envVar); envVarValue != "" {
envVarValue, envVar := envVarValue, envVar
t.Cleanup(func() { os.Setenv(envVar, envVarValue) })
os.Unsetenv(envVar)
}
}
for envVar, envVarValue := range test.setenv {
t.Setenv(envVar, envVarValue)
}
err := LoadTranslations("test", nil)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
result := T("test_string")
if result != test.expectedStr {
t.Errorf("expected: %s, saw: %s", test.expectedStr, result)
}
})
}
}
// resetLazyLoading allows multiple tests to test translation lazy loading by resetting the state
func resetLazyLoading() {
translationsLoaded = false
lazyLoadTranslationsOnce = sync.Once{}
}
func TestLazyLoadTranslationFuncIsCalled(t *testing.T) {
resetLazyLoading()
timesCalled := 0
err := SetLoadTranslationsFunc(func() error {
timesCalled++
return LoadTranslations("test", func() string { return "zh_CN" })
})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if translationsLoaded {
t.Errorf("expected translationsLoaded to be false, but it was true")
}
// Translation should succeed and use the lazy loaded translations
result := T("test_string")
if result != "baz" {
t.Errorf("expected: %s, saw: %s", "baz", result)
}
if timesCalled != 1 {
t.Errorf("expected LoadTranslationsFunc to have been called 1 time, but it was called %d times", timesCalled)
}
if !translationsLoaded {
t.Errorf("expected translationsLoaded to be true, but it was false")
}
// Call T() again, and timesCalled should remain 1
T("test_string")
if timesCalled != 1 {
t.Errorf("expected LoadTranslationsFunc to have been called 1 time, but it was called %d times", timesCalled)
}
}
func TestLazyLoadTranslationFuncOnlyCalledIfTranslationsNotLoaded(t *testing.T) {
resetLazyLoading()
// Set a custom translations func
timesCalled := 0
err := SetLoadTranslationsFunc(func() error {
timesCalled++
return LoadTranslations("test", func() string { return defaultLanguage })
})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if translationsLoaded {
t.Errorf("expected translationsLoaded to be false, but it was true")
}
// Explicitly load translations before lazy loading can occur
err = LoadTranslations("test", func() string { return defaultLanguage })
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !translationsLoaded {
t.Errorf("expected translationsLoaded to be true, but it was false")
}
// Translation should succeed, and use the explicitly loaded translations, not the lazy loaded ones
result := T("test_string")
if result != "foo" {
t.Errorf("expected: %s, saw: %s", "foo", result)
}
if timesCalled != 0 {
t.Errorf("expected LoadTranslationsFunc to have not been called, but it was called %d times", timesCalled)
}
}
func TestSetCustomLoadTranslationsFunc(t *testing.T) {
resetLazyLoading()
// Set a custom translations func that loads translations from a directory
err := SetLoadTranslationsFunc(func() error {
gettext.BindLocale(gettext.New("cli", "./translations/test"))
gettext.SetDomain("cli")
gettext.SetLanguage("zh_CN")
return nil
})
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if translationsLoaded {
t.Errorf("expected translationsLoaded to be false, but it was true")
}
// Translation should succeed
result := T("test_string")
if result != "baz" {
t.Errorf("expected: %s, saw: %s", "baz", result)
}
if !translationsLoaded {
t.Errorf("expected translationsLoaded to be true, but it was false")
}
}
func TestSetCustomLoadTranslationsFuncAfterTranslationsLoadedShouldFail(t *testing.T) {
resetLazyLoading()
// Explicitly load translations
err := LoadTranslations("test", func() string { return defaultLanguage })
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if !translationsLoaded {
t.Errorf("expected translationsLoaded to be true, but it was false")
}
// This should fail because translations have already been loaded, and the custom function should not be called.
timesCalled := 0
err = SetLoadTranslationsFunc(func() error {
timesCalled++
return nil
})
if err == nil {
t.Errorf("expected error, but it did not occur")
}
if timesCalled != 0 {
t.Errorf("expected LoadTranslationsFunc to have not been called, but it was called %d times", timesCalled)
}
}