-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocale_test.go
More file actions
108 lines (100 loc) · 2.7 KB
/
Copy pathlocale_test.go
File metadata and controls
108 lines (100 loc) · 2.7 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
package i18n
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMatchAvailableLocale(t *testing.T) {
t.Parallel()
bundle := newTestBundle(t, "en",
WithLocales("zh-Hans", "ja-JP", "ko-KR"),
)
require.NoError(t, bundle.LoadMessages(map[string]map[string]string{
"en": {"hello_world": "Hello, world"},
"zh-Hans": {"hello_world": "你好,世界"},
"ja-JP": {"hello_world": "こんにちは世界"},
"ko-KR": {"hello_world": "안녕 세상"},
}))
tests := []struct {
name string
accepts []string
wantLocale string
wantText string
}{
{
name: "Chinese simplified via zh-CN",
accepts: []string{"zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6"},
wantLocale: "zh-Hans",
wantText: "你好,世界",
},
{
name: "English via en-us",
accepts: []string{"en-us;q=0.7,en;q=0.3"},
wantLocale: "en",
wantText: "Hello, world",
},
{
name: "Japanese via ja-JP",
accepts: []string{"ja-JP,ja;q=0.9,en;q=0.8"},
wantLocale: "ja-JP",
wantText: "こんにちは世界",
},
{
name: "unsupported language falls back to default",
accepts: []string{"de;q=0.9,de-DE;q=0.8"},
wantLocale: "en",
wantText: "Hello, world",
},
{
name: "invalid header falls back to default",
accepts: []string{"not-a-valid-header!!!"},
wantLocale: "en",
wantText: "Hello, world",
},
{
name: "empty header falls back to default",
accepts: []string{""},
wantLocale: "en",
wantText: "Hello, world",
},
{
name: "multiple headers picks best match",
accepts: []string{"de;q=0.9", "ja-JP;q=0.8"},
wantLocale: "ja-JP",
wantText: "こんにちは世界",
},
{
name: "multiple headers use global quality order",
accepts: []string{"zh;q=0.1", "ja;q=0.9"},
wantLocale: "ja-JP",
wantText: "こんにちは世界",
},
{
name: "zero quality entry is ignored across headers",
accepts: []string{"zh;q=0", "ja;q=0.5"},
wantLocale: "ja-JP",
wantText: "こんにちは世界",
},
{
name: "equal quality preserves first header order",
accepts: []string{"zh;q=0.8", "ja;q=0.8"},
wantLocale: "zh-Hans",
wantText: "你好,世界",
},
{
name: "no arguments falls back to default",
accepts: nil,
wantLocale: "en",
wantText: "Hello, world",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
locale := bundle.MatchAvailableLocale(tt.accepts...)
loc := bundle.NewLocalizer(locale)
assert.Equal(t, tt.wantLocale, loc.Locale())
assert.Equal(t, tt.wantText, loc.Get("hello_world"))
})
}
}