-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate_test.go
More file actions
371 lines (309 loc) · 8.63 KB
/
translate_test.go
File metadata and controls
371 lines (309 loc) · 8.63 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
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 (
"fmt"
"testing"
)
func setupTestDictionaries() {
// Clean up existing dictionaries
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
// Set up English dictionary
enDict := NewDictionary("en")
enDict.AddAll(map[string]string{
"hello-0": "Hello {0}",
"welcome": "Welcome",
"dashboard": "Dashboard",
"goodbye": "Goodbye",
"hello-0-world": "Hello {0} World",
"item-count": "{count, plural, one {# item} other {# items}}",
"message-count": "{count, plural, zero {no messages} one {# message} other {# messages}}",
})
Register(enDict)
// Set up French dictionary
frDict := NewDictionary("fr")
frDict.AddAll(map[string]string{
"hello-0": "Bonjour {0}",
"welcome": "Bienvenue",
"dashboard": "Tableau de bord",
"hello-0-world": "Bonjour {0} Monde",
"item-count": "{count, plural, one {# élément} other {# éléments}}",
})
Register(frDict)
SetDefaultLanguage("en")
}
func TestT_BasicTranslation(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
fn := T("hello-0", "John")
result := fn("en")
if result != "Hello John" {
t.Errorf("Expected 'Hello John', got '%s'", result)
}
result = fn("fr")
if result != "Bonjour John" {
t.Errorf("Expected 'Bonjour John', got '%s'", result)
}
}
func TestT_FallbackToDefault(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
fn := T("goodbye")
// French doesn't have "goodbye", should fallback to English
result := fn("fr")
if result != "Goodbye" {
t.Errorf("Expected 'Goodbye' (fallback), got '%s'", result)
}
}
func TestT_NoTranslation(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
fn := T("nonexistent-key", "arg")
result := fn("en")
if result != "nonexistent-key" {
t.Errorf("Expected 'nonexistent-key', got '%s'", result)
}
}
func TestF_BasicFormat(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
fn := F("Hello %s World", "Beautiful")
result := fn("en")
if result != "Hello Beautiful World" {
t.Errorf("Expected 'Hello Beautiful World', got '%s'", result)
}
result = fn("fr")
if result != "Bonjour Beautiful Monde" {
t.Errorf("Expected 'Bonjour Beautiful Monde', got '%s'", result)
}
}
func TestF_NoTranslation(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
fn := F("Unknown %s format", "test")
// Should use normalized template since no translation exists
result := fn("en")
if result != "Unknown test format" {
t.Errorf("Expected 'Unknown test format', got '%s'", result)
}
}
func TestS_StaticText(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
fn := S("Dashboard")
result := fn("en")
if result != "Dashboard" {
t.Errorf("Expected 'Dashboard', got '%s'", result)
}
result = fn("fr")
if result != "Tableau de bord" {
t.Errorf("Expected 'Tableau de bord', got '%s'", result)
}
}
func TestS_FallbackToOriginal(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
fn := S("Unknown Text")
result := fn("en")
if result != "Unknown Text" {
t.Errorf("Expected 'Unknown Text', got '%s'", result)
}
result = fn("fr")
if result != "Unknown Text" {
t.Errorf("Expected 'Unknown Text', got '%s'", result)
}
}
func TestP_Pluralization(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
// Add more comprehensive plural templates
enDict := GetDictionary("en")
enDict.Add("advanced-count", "{count, plural, zero {no items} one {# item} other {# items}}")
ruDict := NewDictionary("ru")
ruDict.Add("advanced-count", "{count, plural, zero {нет элементов} one {# элемент} few {# элемента} many {# элементов}}")
Register(ruDict)
arDict := NewDictionary("ar")
arDict.Add("advanced-count", "{count, plural, zero {لا عناصر} one {عنصر واحد} two {عنصران} few {# عناصر} many {# عنصر} other {# عنصر}}")
Register(arDict)
tests := []struct {
locale string
count int
expected string
}{
// English tests
{"en", 0, "no items"},
{"en", 1, "1 item"},
{"en", 2, "2 items"},
{"en", 5, "5 items"},
// Russian tests (complex Slavic rules)
{"ru", 0, "нет элементов"},
{"ru", 1, "1 элемент"},
{"ru", 2, "2 элемента"},
{"ru", 3, "3 элемента"},
{"ru", 4, "4 элемента"},
{"ru", 5, "5 элементов"},
{"ru", 10, "10 элементов"},
// Arabic tests (even more complex)
{"ar", 0, "لا عناصر"},
{"ar", 1, "عنصر واحد"},
{"ar", 2, "عنصران"},
{"ar", 3, "3 عناصر"},
{"ar", 5, "5 عناصر"},
{"ar", 10, "10 عناصر"},
{"ar", 11, "11 عنصر"},
{"ar", 50, "50 عنصر"},
{"ar", 100, "100 عنصر"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%s_%d", tt.locale, tt.count), func(t *testing.T) {
fn := P("advanced-count", tt.count)
result := fn(tt.locale)
if result != tt.expected {
t.Errorf("P('advanced-count', %d)(%q) = %q, expected %q",
tt.count, tt.locale, result, tt.expected)
}
})
}
// Test original simple cases for backwards compatibility
fn := P("item-count", 1)
result := fn("en")
if result != "1 item" {
t.Errorf("Expected '1 item', got '%s'", result)
}
result = fn("fr")
if result != "1 élément" {
t.Errorf("Expected '1 élément', got '%s'", result)
}
fn = P("item-count", 5)
result = fn("en")
if result != "5 items" {
t.Errorf("Expected '5 items', got '%s'", result)
}
result = fn("fr")
if result != "5 éléments" {
t.Errorf("Expected '5 éléments', got '%s'", result)
}
}
func TestP_FallbackToSimpleSubstitution(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
// Add a simple template without ICU plural syntax
enDict := GetDictionary("en")
enDict.Add("simple-count", "{count} things")
fn := P("simple-count", 3)
result := fn("en")
if result != "3 things" {
t.Errorf("Expected '3 things', got '%s'", result)
}
}
func TestR_DirectTranslation(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
result := R("en", "Dashboard")
if result != "Dashboard" {
t.Errorf("Expected 'Dashboard', got '%s'", result)
}
result = R("fr", "Dashboard")
if result != "Tableau de bord" {
t.Errorf("Expected 'Tableau de bord', got '%s'", result)
}
result = R("fr", "Unknown Text")
if result != "Unknown Text" {
t.Errorf("Expected 'Unknown Text', got '%s'", result)
}
}
func TestMultipleArgs(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
// Add templates with multiple placeholders
enDict := GetDictionary("en")
enDict.Add("multi-args", "Hello {0}, you have {1} messages and {2} notifications")
frDict := GetDictionary("fr")
frDict.Add("multi-args", "Bonjour {0}, vous avez {1} messages et {2} notifications")
fn := T("multi-args", "John", 5, 3)
result := fn("en")
expected := "Hello John, you have 5 messages and 3 notifications"
if result != expected {
t.Errorf("Expected '%s', got '%s'", expected, result)
}
result = fn("fr")
expected = "Bonjour John, vous avez 5 messages et 3 notifications"
if result != expected {
t.Errorf("Expected '%s', got '%s'", expected, result)
}
}
func TestEmptyArgs(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
fn := T("welcome")
result := fn("en")
if result != "Welcome" {
t.Errorf("Expected 'Welcome', got '%s'", result)
}
result = fn("fr")
if result != "Bienvenue" {
t.Errorf("Expected 'Bienvenue', got '%s'", result)
}
}
func TestNonExistentLocale(t *testing.T) {
setupTestDictionaries()
defer func() {
muDicts.Lock()
dictionaries = make(map[string]*Dictionary)
muDicts.Unlock()
}()
fn := T("welcome")
// Should fallback to default language (en) for unknown locale
result := fn("de")
if result != "Welcome" {
t.Errorf("Expected 'Welcome' (fallback), got '%s'", result)
}
}