...
1
2
3
4
5
6
7
8
9 package htmlindex
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import (
25 "errors"
26 "strings"
27 "sync"
28
29 "golang.org/x/text/encoding"
30 "golang.org/x/text/encoding/internal/identifier"
31 "golang.org/x/text/language"
32 )
33
34 var (
35 errInvalidName = errors.New("htmlindex: invalid encoding name")
36 errUnknown = errors.New("htmlindex: unknown Encoding")
37 errUnsupported = errors.New("htmlindex: this encoding is not supported")
38 )
39
40 var (
41 matcherOnce sync.Once
42 matcher language.Matcher
43 )
44
45
46
47 func LanguageDefault(tag language.Tag) string {
48 matcherOnce.Do(func() {
49 tags := []language.Tag{}
50 for _, t := range strings.Split(locales, " ") {
51 tags = append(tags, language.MustParse(t))
52 }
53 matcher = language.NewMatcher(tags, language.PreferSameScript(true))
54 })
55 _, i, _ := matcher.Match(tag)
56 return canonical[localeMap[i]]
57 }
58
59
60
61
62 func Get(name string) (encoding.Encoding, error) {
63 x, ok := nameMap[strings.ToLower(strings.TrimSpace(name))]
64 if !ok {
65 return nil, errInvalidName
66 }
67 return encodings[x], nil
68 }
69
70
71
72 func Name(e encoding.Encoding) (string, error) {
73 id, ok := e.(identifier.Interface)
74 if !ok {
75 return "", errUnknown
76 }
77 mib, _ := id.ID()
78 if mib == 0 {
79 return "", errUnknown
80 }
81 v, ok := mibMap[mib]
82 if !ok {
83 return "", errUnsupported
84 }
85 return canonical[v], nil
86 }
87
View as plain text