-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale_test.go
54 lines (38 loc) · 1.29 KB
/
locale_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
package i18n
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseAcceptLanguage(t *testing.T) {
assert := assert.New(t)
bundle := NewBundle(
WithDefaultLocale("en"),
WithLocales("zh-Hans", "ja-JP", "ko-KR"),
)
bundle.LoadMessages(map[string]map[string]string{
"en": {
"hello_world": "Hello, world",
},
"zh-Hans": {
"hello_world": "你好,世界",
},
"ja-JP": {
"hello_world": "こんにちは世界",
},
"ko-KR": {
"hello_world": "안녕 세상",
},
})
localizer := bundle.NewLocalizer(bundle.MatchAvailableLocale("zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,ja;q=0.6"))
assert.Equal("zh-Hans", localizer.Locale())
assert.Equal("你好,世界", localizer.Get("hello_world"))
localizer = bundle.NewLocalizer(bundle.MatchAvailableLocale("en-us;q=0.7,en;q=0.3"))
assert.Equal("en", localizer.Locale())
assert.Equal("Hello, world", localizer.Get("hello_world"))
localizer = bundle.NewLocalizer(bundle.MatchAvailableLocale("ja-JP,ja;q=0.9,en;q=0.8"))
assert.Equal("ja-JP", localizer.Locale())
assert.Equal("こんにちは世界", localizer.Get("hello_world"))
localizer = bundle.NewLocalizer(bundle.MatchAvailableLocale("de;q=0.9,de-DE;q=0.8"))
assert.Equal("en", localizer.Locale())
assert.Equal("Hello, world", localizer.Get("hello_world"))
}