-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathi18n_test.go
209 lines (186 loc) · 4.01 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
package i18n
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestStore_AddLocale(t *testing.T) {
s := NewStore()
_, err := s.AddLocale("en-US", "English", []byte(``))
assert.Nil(t, err)
t.Run("duplicated locales", func(t *testing.T) {
_, err := s.AddLocale("en-US", "English", []byte(``))
got := fmt.Sprintf("%v", err)
want := `duplicated locales for "en-US"`
assert.Equal(t, want, got)
})
t.Run("bad index", func(t *testing.T) {
_, err := s.AddLocale("en-US", "English", []byte(`
[messages]
test1 = I have %[1]d ${cat, 0}
`))
got := fmt.Sprintf("%v", err)
want := `new locale: the smallest index is 1 but got 0 for "${cat, 0}"`
assert.Equal(t, want, got)
})
}
func TestStore_Locale(t *testing.T) {
s := NewStore()
want, err := s.AddLocale("en-US", "English", []byte(``))
assert.Nil(t, err)
got, err := s.Locale("en-US")
assert.Nil(t, err)
assert.Equal(t, want, got)
t.Run("non-existent locale", func(t *testing.T) {
_, err := s.Locale("zh-CN")
got := fmt.Sprintf("%v", err)
want := "locale not found"
assert.Equal(t, want, got)
})
}
var sampleSource = []byte(`
[plurals]
file.one = file
file.other = files
dog.zero = dog
dog.one = %(dog.zero)s
dog.two = dogs
dog.few = %(dog.two)s
dog.other = %(dog.two)s
[messages]
test1 = I have %[1]d changed ${file, 1} and deleted %[2]d ${file, 2}
test2 = I bought %[1]d ${cat, 1} and sold %[2]d ${dog, 2}
test3 = I have %[1]d ${dog, 10}
test4 = I have a dream
test5 = My name is %s
test6 = I have %[1]d ${dog, 1}
`)
func TestLocale_Translate(t *testing.T) {
l, err := NewStore().AddLocale(
"en-US",
"English",
sampleSource,
)
assert.Nil(t, err)
tests := []struct {
name string
key string
args []interface{}
want string
}{
{
name: "good",
key: "messages::test1",
args: []interface{}{1, 2},
want: `I have 1 changed file and deleted 2 files`,
},
{
name: "no such plural",
key: "messages::test2",
args: []interface{}{1, 2},
want: `I bought 1 <no such plural: cat> and sold 2 dogs`,
},
{
name: "no arg for index",
key: "messages::test3",
args: []interface{}{1},
want: `I have 1 <no arg for index 10>`,
},
{
name: "no plural",
key: "messages::test4",
args: nil,
want: `I have a dream`,
},
{
name: "no such key",
key: "messages::404",
args: nil,
want: `<no such key: messages::404>`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := l.Translate(test.key, test.args...)
assert.Equal(t, test.want, got)
})
}
}
func TestLocale_TranslateWithFallback(t *testing.T) {
s := NewStore()
l1, err := s.AddLocale(
"en-US",
"English",
sampleSource,
)
assert.Nil(t, err)
l2, err := s.AddLocale(
"zh-CN",
"简体中文",
[]byte(`
[plurals]
file.other = 文件
[messages]
test1 = 我变更了 %[1]d 个${file, 1}并删除了 %[2]d 个${file, 2}
`),
)
assert.Nil(t, err)
tests := []struct {
name string
key string
args []interface{}
want string
}{
{
name: "good",
key: "messages::test1",
args: []interface{}{1, 2},
want: `我变更了 1 个文件并删除了 2 个文件`,
},
{
name: "fallback",
key: "messages::test4",
args: nil,
want: `I have a dream`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := l2.TranslateWithFallback(l1, test.key, test.args...)
assert.Equal(t, test.want, got)
})
}
}
func BenchmarkLocale_Translate(b *testing.B) {
l, err := NewStore().AddLocale(
"en-US",
"English",
sampleSource,
)
assert.Nil(b, err)
for i := 0; i < b.N; i++ {
l.Translate("messages::test4")
}
}
func BenchmarkLocale_Translate_Format(b *testing.B) {
l, err := NewStore().AddLocale(
"en-US",
"English",
sampleSource,
)
assert.Nil(b, err)
for i := 0; i < b.N; i++ {
l.Translate("messages::test5", "Joe")
}
}
func BenchmarkLocale_Translate_Plural(b *testing.B) {
l, err := NewStore().AddLocale(
"en-US",
"English",
sampleSource,
)
assert.Nil(b, err)
for i := 0; i < b.N; i++ {
l.Translate("messages::test6", 1)
}
}