-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlanguage.go
76 lines (61 loc) · 1.47 KB
/
language.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
package main
import (
"bytes"
_ "embed"
"encoding/json"
"encoding/xml"
"io/ioutil"
"net/http"
)
type formatters struct {
XMLName xml.Name `xml:"formatters"`
Objects []object `xml:"object"`
}
type object struct {
XMLName xml.Name `xml:"object"`
Name string `xml:"name,attr"`
Format string `xml:"format,attr"`
}
func loadDict(hdgEndpoint string, language string) (map[string]string, error) {
val, err := get(hdgEndpoint + "/data/dictionaries/" + language + ".json")
if err != nil {
return nil, err
}
result := make(map[string]string)
if err := json.Unmarshal(trimBom(val), &result); err != nil {
return nil, err
}
return result, nil
}
func loadFormats(hdgEndpoint string, language string) (map[string]string, error) {
val, err := get(hdgEndpoint + "/data/dictionaries/" + language + "_formatters.xml")
if err != nil {
return nil, err
}
var formatters formatters
if err := xml.Unmarshal(trimBom(val), &formatters); err != nil {
return nil, err
}
result := make(map[string]string)
for _, object := range formatters.Objects {
result[object.Name] = object.Format
}
return result, nil
}
func trimBom(fileBytes []byte) []byte {
return bytes.Trim(fileBytes, "\xef\xbb\xbf")
}
func get(url string) ([]byte, error) {
res, reqErr := http.Get(url)
if reqErr != nil {
return nil, reqErr
}
if res.Body != nil {
defer res.Body.Close()
}
resBody, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
return nil, readErr
}
return resBody, nil
}