forked from jeandeaual/go-locale
-
Notifications
You must be signed in to change notification settings - Fork 1
/
locale_unix_test.go
190 lines (143 loc) · 4.89 KB
/
locale_unix_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
//go:build !windows && !darwin && !js && !android
// +build !windows,!darwin,!js,!android
package locale
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func resetEnv(t *testing.T) {
t.Log("Resetting LANGUAGE, LC_ALL, LC_MESSAGES and LANG")
for _, env := range [...]string{"LANGUAGE", "LC_ALL", "LC_MESSAGES", "LANG"} {
err := os.Setenv(env, "")
assert.Equal(t, nil, err)
}
}
func TestMultipleLocales(t *testing.T) {
resetEnv(t)
err := os.Setenv("LANGUAGE", "en_US:fr:ja")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LC_ALL", "")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LC_MESSAGES", "")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LANG", "en_US.UTF-8")
assert.Equal(t, nil, err, "err should be nil")
locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, []string{"en-US", "fr", "ja"}, locales)
locale, err := GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "en-US", locale)
lang, err := GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "en", lang)
region, err := GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "US", region)
// If "C" is set, we should ignore LANGUAGE
err = os.Setenv("LC_ALL", "C")
assert.Equal(t, nil, err, "err should be nil")
var nilStringSlice []string
locales, err = GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, nilStringSlice, locales)
locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", locale)
lang, err = GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", lang)
region, err = GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", region)
}
func TestSingleLocale(t *testing.T) {
resetEnv(t)
err := os.Setenv("LANGUAGE", "ja_JP")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LC_ALL", "en_US")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LC_MESSAGES", "ko_KR")
assert.Equal(t, nil, err, "err should be nil")
err = os.Setenv("LANG", "fr_FR")
assert.Equal(t, nil, err, "err should be nil")
locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, []string{"ja-JP"}, locales)
locale, err := GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "ja-JP", locale)
lang, err := GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "ja", lang)
region, err := GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "JP", region)
err = os.Setenv("LANGUAGE", "")
assert.Equal(t, nil, err, "err should be nil")
locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "en-US", locale)
lang, err = GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "en", lang)
region, err = GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "US", region)
err = os.Setenv("LC_ALL", "")
assert.Equal(t, nil, err, "err should be nil")
locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "ko-KR", locale)
lang, err = GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "ko", lang)
region, err = GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "KR", region)
err = os.Setenv("LC_MESSAGES", "")
assert.Equal(t, nil, err, "err should be nil")
locale, err = GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "fr-FR", locale)
lang, err = GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "fr", lang)
region, err = GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "FR", region)
}
func TestLocaleNoRegion(t *testing.T) {
resetEnv(t)
err := os.Setenv("LANG", "fr")
assert.Equal(t, nil, err, "err should be nil")
locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, []string{"fr"}, locales)
locale, err := GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "fr", locale)
lang, err := GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "fr", lang)
region, err := GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", region)
}
func TestNoLocale(t *testing.T) {
resetEnv(t)
var nilStringSlice []string
locales, err := GetLocales()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, nilStringSlice, locales)
locale, err := GetLocale()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", locale)
lang, err := GetLanguage()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", lang)
region, err := GetRegion()
assert.Equal(t, nil, err, "err should be nil")
assert.Equal(t, "", region)
}