-
Notifications
You must be signed in to change notification settings - Fork 0
/
language.go
142 lines (122 loc) · 2.76 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
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
package goaccepts
import (
"sort"
"strconv"
"strings"
)
// LanguageResult of the accepts header
type LanguageResult struct {
// prefix ...
Prefix string
// suffix ...
Suffix string
// weight ...
Weight float64
// i ...
i int
// full ...
Full string
}
func parseAcceptLanguage(accept string) []LanguageResult {
res := []LanguageResult{}
accepts := strings.Split(accept, ",")
j := 0
for i := 0; i < len(accepts); i++ {
language := parseLanguage(strings.Trim(accepts[i], " "), i)
if language != nil {
n := findL(res, language.Full)
if n == -1 {
res = append(res[:j], append([]LanguageResult{*language}, res[j:]...)...)
j = j + 1
}
}
}
return res
}
func parseLanguage(str string, i int) *LanguageResult {
if str == "*" {
return &LanguageResult{
Prefix: str,
Suffix: "",
Weight: 100.0,
i: i,
Full: str,
}
}
weight := 1.0
params := strings.Split(str, ";")
if len(params) > 1 {
p := strings.Split(params[1], "=")
if strings.ToLower(p[0]) == "q" {
weight, _ = strconv.ParseFloat(p[1], 64)
}
}
langs := strings.Split(params[0], "-")
prefix := langs[0]
suffix := ""
if len(langs) > 1 {
suffix = strings.Join(langs[1:], "-")
}
full := params[0]
return &LanguageResult{
Prefix: prefix,
Suffix: suffix,
Weight: weight,
i: i,
Full: full,
}
}
func parseLanguages(accept interface{}) []LanguageResult {
// no header = *
val := "*"
if accept != nil {
val = accept.(string)
}
accepts := parseAcceptLanguage(val)
// sorted list of all languages
filteredList := filterL(accepts, func(spec LanguageResult) bool {
return spec.Weight > 0
})
sort.SliceStable(filteredList, func(i, j int) bool {
return filteredList[i].Weight > filteredList[j].Weight
})
return filteredList
}
// Languages - Get the preferred languages from an Accept-Language header.
// https://tools.ietf.org/html/rfc2616#section-14.4
func Languages(accept interface{}) []string {
languages := parseLanguages(accept)
fullList := mapL(languages, func(spec LanguageResult) string {
return spec.Full
})
return fullList
}
// LanguagesDetails - Get the preferred languages from an Accept-Language header.
// https://tools.ietf.org/html/rfc2616#section-14.4
func LanguagesDetails(accept interface{}) []LanguageResult {
return parseLanguages(accept)
}
func filterL(vs []LanguageResult, f func(LanguageResult) bool) []LanguageResult {
vsf := make([]LanguageResult, 0)
for _, v := range vs {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}
func mapL(vs []LanguageResult, f func(LanguageResult) string) []string {
vsf := make([]string, 0)
for _, v := range vs {
vsf = append(vsf, f(v))
}
return vsf
}
func findL(a []LanguageResult, x string) int {
for i, n := range a {
if x == n.Full {
return i
}
}
return -1
}