-
Notifications
You must be signed in to change notification settings - Fork 0
/
localizer_test.go
242 lines (201 loc) · 7.33 KB
/
localizer_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
package i18n
import (
"testing"
"github.com/stretchr/testify/assert"
)
var testTranslations = map[string]map[string]string{
"en": {
"{count, plural, =0 {None} one {1 Apple} other {# Apples}}": "{count, plural, =0 {None} one {1 Apple} other {# Apples}}",
},
"zh-Hans": {
// Token-based Translations
"test_message": "这是一则测试讯息。",
"test_template": "你好,{Name}!",
"test_plural": "{count, plural, =0 {没有} =1 {只有 1 个} other {有 # 个}}",
// Text-based Translations.
"Hello, world!": "你好,世界!",
"How are you, {Name}?": "过得如何,{Name}?",
"Post <verb>": "发表贴文",
"Post <noun>": "文章",
"{count, plural, =0 {None} one {1 Apple} other {# Apples}}": "{count, plural, =0 {没有苹果} =1 {1 颗苹果} other {有 # 颗苹果}}",
"{count, plural, =0 {No Post} one {1 Post} other {# Posts}} <noun>": "{count, plural, =0 {没有文章} =1 {1 篇文章} other {有 # 篇文章}}",
"{count, plural, =0 {No Post} one {1 Post} other {# Posts}} <verb>": "{count, plural, =0 {没有发表} =1 {1 篇发表} other {有 # 篇发表}}",
"Post": "THIS_SHOULD_NOT_BE_USED",
"{count, plural, =0 {No Post} one {1 Post} other {# Posts}}": "THIS_SHOULD_NOT_BE_USED",
},
"ja-JP": {
// Token-based Translations
"test_message": "これはテストメッセージです。",
"test_template": "こんにちは、{Name}!",
"test_plural": "{count, plural, =0 {なし} one {1 つだけ} other {# 个あります}}",
},
"ko-KR": {
// Token-based Translations
"test_message": "이것은 테스트 메시지입니다.",
"test_template": "안녕하세요, {Name} 님!",
"test_plural": "{count, plural, =0 {없음} one {1 개} other {# 개가 있음}}",
// Text-based Translations.
"Hello, world!": "안녕하세요, 세상!",
"How are you, {Name}?": "{Name} 님, 어떻게 지내세요?",
"Post <verb>": "메시지 게시",
"Post <noun>": "기사",
},
}
func newTestLocalizer() *Localizer {
bundle := NewBundle(
WithDefaultLocale("en"),
WithLocales("en", "zh-Hans", "ja-JP", "ko-KR"),
)
bundle.LoadMessages(testTranslations)
return bundle.NewLocalizer("zh-Hans")
}
func TestLocalizer(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("zh-Hans", localizer.Locale())
}
func TestTokenString(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("这是一则测试讯息。", localizer.Get("test_message"))
assert.Equal("not_exists_message", localizer.Get("not_exists_message"))
}
func TestTokenVars(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("你好,Yami!", localizer.Get("test_template", Vars{
"Name": "Yami",
}))
}
func TestTokenPlural(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("没有", localizer.Get("test_plural", Vars{
"count": 0,
}))
assert.Equal("只有 1 个", localizer.Get("test_plural", Vars{
"count": 1,
}))
assert.Equal("有 2 个", localizer.Get("test_plural", Vars{
"count": 2,
}))
}
func TestTextString(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("你好,世界!", localizer.Get("Hello, world!"))
}
func TestTextStringRaw(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("I'm fine thank you!", localizer.Get("I'm fine thank you!"))
}
func TestTextVars(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("过得如何,Yami?", localizer.Get("How are you, {Name}?", Vars{
"Name": "Yami",
}))
}
func TestTextVarsRaw(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("I'm fine, thanks to Yami!", localizer.Get("I'm fine, thanks to {Name}!", Vars{
"Name": "Yami",
}))
}
func TestTextPlural(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("没有苹果", localizer.Get("{count, plural, =0 {None} one {1 Apple} other {# Apples}}", Vars{
"count": 0,
}))
assert.Equal("1 颗苹果", localizer.Get("{count, plural, =0 {None} one {1 Apple} other {# Apples}}", Vars{
"count": 1,
}))
assert.Equal("有 2 颗苹果", localizer.Get("{count, plural, =0 {None} one {1 Apple} other {# Apples}}", Vars{
"count": 2,
}))
}
func TestTextStringContext(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("发表贴文", localizer.GetX("Post", "verb"))
assert.Equal("文章", localizer.GetX("Post", "noun"))
}
func TestTextPluralContext(t *testing.T) {
assert := assert.New(t)
localizer := newTestLocalizer()
assert.Equal("没有文章", localizer.GetX("{count, plural, =0 {No Post} one {1 Post} other {# Posts}}", "noun", Vars{
"count": 0,
}))
assert.Equal("1 篇文章", localizer.GetX("{count, plural, =0 {No Post} one {1 Post} other {# Posts}}", "noun", Vars{
"count": 1,
}))
assert.Equal("有 2 篇文章", localizer.GetX("{count, plural, =0 {No Post} one {1 Post} other {# Posts}}", "noun", Vars{
"count": 2,
}))
assert.Equal("没有发表", localizer.GetX("{count, plural, =0 {No Post} one {1 Post} other {# Posts}}", "verb", Vars{
"count": 0,
}))
assert.Equal("1 篇发表", localizer.GetX("{count, plural, =0 {No Post} one {1 Post} other {# Posts}}", "verb", Vars{
"count": 1,
}))
assert.Equal("有 2 篇发表", localizer.GetX("{count, plural, =0 {No Post} one {1 Post} other {# Posts}}", "verb", Vars{
"count": 2,
}))
}
func TestTextFallback(t *testing.T) {
assert := assert.New(t)
bundle := NewBundle(
WithDefaultLocale("zh-Hans"),
WithLocales("en", "zh-Hans", "ja-JP", "ko-KR"),
WithFallback(map[string][]string{
"ja-JP": {"ko-KR"},
}),
)
bundle.LoadMessages(testTranslations)
localizer := bundle.NewLocalizer("ja-JP")
// Test ja-JP
assert.Equal("これはテストメッセージです。", localizer.Get("test_message"))
assert.Equal("こんにちは、Yami!", localizer.Get("test_template", Vars{
"Name": "Yami",
}))
assert.Equal("なし", localizer.Get("test_plural", Vars{
"count": 0,
}))
// Test ja-JP -> ko-KR fallback
assert.Equal("안녕하세요, 세상!", localizer.Get("Hello, world!"))
assert.Equal("Yami 님, 어떻게 지내세요?", localizer.Get("How are you, {Name}?", Vars{
"Name": "Yami",
}))
assert.Equal("메시지 게시", localizer.GetX("Post", "verb"))
// Test ja-JP -> zh-CN fallback
assert.Equal("没有苹果", localizer.Get("{count, plural, =0 {None} one {1 Apple} other {# Apples}}", Vars{
"count": 0,
}))
assert.Equal("1 颗苹果", localizer.Get("{count, plural, =0 {None} one {1 Apple} other {# Apples}}", Vars{
"count": 1,
}))
assert.Equal("有 2 颗苹果", localizer.Get("{count, plural, =0 {None} one {1 Apple} other {# Apples}}", Vars{
"count": 2,
}))
// Test nil fallback
assert.Equal("Ni hao", localizer.Get("Ni hao"))
}
func TestTextFallbackResursive(t *testing.T) {
assert := assert.New(t)
bundle := NewBundle(
WithDefaultLocale("en"),
WithLocales("en", "zh-Hans", "ja-JP", "ko-KR"),
WithFallback(map[string][]string{
"ja-JP": {"ko-KR"},
"ko-KR": {"zh-Hans"},
}))
bundle.LoadMessages(testTranslations)
localizer := bundle.NewLocalizer("ja-JP")
// Test ja-JP -> ko-KR -> zh-CN fallback
assert.Equal("1 颗苹果", localizer.Get("{count, plural, =0 {None} one {1 Apple} other {# Apples}}", Vars{
"count": 1,
}))
}