-
Notifications
You must be signed in to change notification settings - Fork 5
/
i18n_test.go
66 lines (59 loc) · 1.46 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
package main
import (
"testing"
)
// 测试 NewI18n
func TestNewI18n(t *testing.T) {
// Test NewI18n
i18n, err := NewI18n("en")
if err != nil {
t.Fatalf("NewI18n failed: %v", err)
}
if i18n.defaultLang != "en" {
t.Errorf("Expected default language 'en', got '%s'", i18n.defaultLang)
}
if len(i18n.translations) != 2 {
t.Errorf("Expected 2 languages, got %d", len(i18n.translations))
}
}
// 测试 T 方法
func TestI18n_T(t *testing.T) {
i18n := &I18n{
translations: map[string]map[string]string{
"en": {
"hello": "Hello",
"world": "World",
"with_args": "Hello, %s!",
},
"es": {
"hello": "Hola",
"world": "Mundo",
"with_args": "¡Hola, %s!",
},
},
defaultLang: "en",
}
tests := []struct {
lang string
key string
args []interface{}
expected string
}{
{"en", "hello", nil, "Hello"},
{"es", "hello", nil, "Hola"},
{"en", "world", nil, "World"},
{"es", "world", nil, "Mundo"},
{"fr", "hello", nil, "Hello"}, // Fallback to default language
{"en", "unknown", nil, "unknown"}, // Key not found
{"en", "with_args", []interface{}{"John"}, "Hello, John!"},
{"es", "with_args", []interface{}{"Juan"}, "¡Hola, Juan!"},
}
for _, tt := range tests {
t.Run(tt.lang+"/"+tt.key, func(t *testing.T) {
result := i18n.T(tt.lang, tt.key, tt.args...)
if result != tt.expected {
t.Errorf("T(%q, %q, %v) = %q, want %q", tt.lang, tt.key, tt.args, result, tt.expected)
}
})
}
}