-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils_locale.go
57 lines (49 loc) · 1.3 KB
/
utils_locale.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
package go_android_utils
import (
"errors"
"strings"
)
var (
ErrLocaleFormatUnsupported = errors.New("the supplied locale had an unsupported format")
ErrLocaleUnsupported = errors.New("the supplied locale is unsupported")
)
func (locale *Locale) ToLocale(separator string, iso bool) string {
result := locale.Language + separator + locale.GetCountry(iso)
return result
}
func (locale *Locale) GetCountry(iso bool) string {
if iso {
return strings.ToUpper(locale.CountryISO)
} else {
return strings.ToLower(locale.CountryISO)
}
}
func fromSlice(parts []string) (*Locale, error) {
result := new(Locale)
if len(parts) == 2 {
_, ok := stringsAreNotNull(parts...)
if ok {
result.Language = strings.ToLower(parts[0])
result.CountryISO = strings.ToUpper(parts[1])
} else {
return result, ErrLocaleFormatUnsupported
}
} else {
return result, ErrLocaleFormatUnsupported
}
return result, nil
}
func LocaleFromLocaleString(localeStr string) (*Locale, error) {
// Support en_US and en-US
var err error
if strings.Contains(localeStr, "-") {
parts := strings.Split(localeStr, "-")
return fromSlice(parts)
} else if strings.Contains(localeStr, "_") {
parts := strings.Split(localeStr, "_")
return fromSlice(parts)
} else {
err = ErrLocaleFormatUnsupported
}
return nil, err
}