-
Notifications
You must be signed in to change notification settings - Fork 0
/
i18ngo_test.go
249 lines (210 loc) · 6 KB
/
i18ngo_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
243
244
245
246
247
248
249
package i18ngo
import (
"github.com/stretchr/testify/assert"
"testing"
)
var enData = []byte(`{
"pages": {
"login": {
"buttons": {
"login": "Login"
}
}
},
"common": {
"greetings": {
"hello": "Hello"
}
}
}`)
var trData = []byte(`{
"pages": {
"login": {
"buttons": {
"login": "Giriş"
}
}
},
"common": {
"greetings": {
"hello": "Merhaba"
}
}
}`)
func TestNewI18nInitialization(t *testing.T) {
i18n, err := New(Options{
DefaultLocale: "en",
Debug: false,
Locales: []LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})
assert.NoError(t, err, "Expected no error during initialization")
ctx := i18n.(*Context)
assert.Equal(t, "en", ctx.currentLocale.locale, "Expected default locale to be 'en'")
assert.Len(t, ctx.locales, 2, "Expected two locales to be loaded")
assert.Contains(t, ctx.availableLocales, "en", "Expected availableLocales to include 'en'")
assert.Contains(t, ctx.availableLocales, "tr", "Expected availableLocales to include 'tr'")
}
func TestTranslate(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: false,
Locales: []LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})
assert.Equal(t, "Login", i18n.T("pages.login.buttons.login"), "Expected 'Login' in English locale")
_ = i18n.ChangeLocale("tr")
assert.Equal(t, "Giriş", i18n.T("pages.login.buttons.login"), "Expected 'Giriş' in Turkish locale")
}
func TestMissingTranslation(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: true,
Locales: []LocaleOptions{
{Locale: "en", File: enData},
},
})
missingTranslation := i18n.T("pages.login.buttons.logout")
expectedMessage := "missing translation for path 'en.pages.login.buttons.logout'"
assert.Equal(t, expectedMessage, missingTranslation, "Expected missing translation message")
}
func TestTranslateWithParams(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: false,
Locales: []LocaleOptions{
{Locale: "en", File: []byte(`{
"pages": {
"login": {
"welcome": "Welcome, {{name}}!"
}
}
}`)},
},
})
params := map[string]interface{}{
"name": "John",
}
translated := i18n.T("pages.login.welcome", ¶ms)
assert.Equal(t, "Welcome, John!", translated, "Expected 'Welcome, John!' with parameter replacement")
}
func TestTranslateWithScope(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: false,
Locales: []LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})
params := map[string]interface{}{
"scope": "common.greetings",
}
translated := i18n.T("hello", ¶ms)
assert.Equal(t, "Hello", translated, "Expected 'Hello' from the 'common.greetings' scope in English")
_ = i18n.ChangeLocale("tr")
translated = i18n.T("hello", ¶ms)
assert.Equal(t, "Merhaba", translated, "Expected 'Merhaba' from the 'common.greetings' scope in Turkish")
}
func TestTranslateWithLocaleParam(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: false,
Locales: []LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})
params := map[string]interface{}{
"locale": "tr",
}
translated := i18n.T("pages.login.buttons.login", ¶ms)
assert.Equal(t, "Giriş", translated, "Expected 'Giriş' translation when locale param is 'tr'")
params["locale"] = "en"
translated = i18n.T("pages.login.buttons.login", ¶ms)
assert.Equal(t, "Login", translated, "Expected 'Login' translation when locale param is 'en'")
}
func TestTranslateWithScopeAndLocale(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: false,
Locales: []LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})
params := map[string]interface{}{
"scope": "common.greetings",
"locale": "tr",
}
translated := i18n.T("hello", ¶ms)
assert.Equal(t, "Merhaba", translated, "Expected 'Merhaba' from the 'common.greetings' scope in Turkish")
params["locale"] = "en"
translated = i18n.T("hello", ¶ms)
assert.Equal(t, "Hello", translated, "Expected 'Hello' from the 'common.greetings' scope in English")
}
func TestChangeLocale(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: false,
Locales: []LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})
ctx := i18n.(*Context)
assert.Equal(t, "en", ctx.currentLocale.locale, "Expected current locale to be 'en'")
err := i18n.ChangeLocale("tr")
assert.NoError(t, err, "Expected no error when changing locale to 'tr'")
assert.Equal(t, "tr", ctx.currentLocale.locale, "Expected current locale to be 'tr'")
}
func TestInvalidLocaleChange(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: false,
Locales: []LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})
err := i18n.ChangeLocale("fr")
assert.Error(t, err, "Expected error when switching to an undefined locale")
assert.EqualError(t, err, "locale 'fr' is not available", "Expected specific error message for undefined locale")
}
func TestSeparatorHandling(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: false,
Separator: "/",
Locales: []LocaleOptions{
{Locale: "en", File: []byte(`{
"pages": {
"login": {
"buttons": {
"login": "Login"
}
}
}
}`)},
},
})
assert.Equal(t, "Login", i18n.T("pages/login/buttons/login"), "Expected translation with custom separator")
}
func TestCurrentLocale(t *testing.T) {
i18n, _ := New(Options{
DefaultLocale: "en",
Debug: false,
Locales: []LocaleOptions{
{Locale: "en", File: enData},
{Locale: "tr", File: trData},
},
})
ctx := i18n.(*Context)
assert.Equal(t, "en", ctx.CurrentLocale(), "Expected initial locale to be 'en'")
_ = i18n.ChangeLocale("tr")
assert.Equal(t, "tr", ctx.CurrentLocale(), "Expected current locale to be 'tr'")
}