-
Notifications
You must be signed in to change notification settings - Fork 8
/
locale.go
46 lines (39 loc) · 843 Bytes
/
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
package locale
import (
"errors"
"golang.org/x/text/language"
)
// Detect will detect current env's language.
func Detect() (tag language.Tag, err error) {
tags, err := DetectAll()
if err != nil {
return language.Und, err
}
return tags[0], nil
}
// DetectAll will detect current env's all available language.
func DetectAll() (tags []language.Tag, err error) {
lang, err := detect()
if err != nil {
return
}
tags = make([]language.Tag, 0, len(lang))
for _, v := range lang {
tags = append(tags, language.Make(v))
}
return
}
type detector func() ([]string, error)
func detect() (lang []string, err error) {
for _, fn := range detectors {
lang, err = fn()
if err != nil && errors.Is(err, ErrNotDetected) {
continue
}
if err != nil {
return
}
return
}
return nil, &Error{"detect", ErrNotDetected}
}