This repository has been archived by the owner on Nov 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locale.go
62 lines (52 loc) · 1.45 KB
/
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
58
59
60
61
62
package clickcounteriframe
import (
"fmt"
"sort"
"strconv"
"strings"
)
type LocalePriority struct {
Locale string
Priority float64
}
func (p LocalePriority) String() string {
return fmt.Sprintf("%s: %f", p.Locale, p.Priority)
}
// ByPriority implements sort.Interface for []LocalePriority based on
// the Age field.
type ByPriority []LocalePriority
func (a ByPriority) Len() int { return len(a) }
func (a ByPriority) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByPriority) Less(i, j int) bool { return a[i].Priority < a[j].Priority }
// Parse the value in a Accept-Language Header
func GetClientLocales(acceptLanguage string) []string {
if len(acceptLanguage) == 0 {
locales := []string{"en"}
return locales
}
languages := strings.Split(acceptLanguage, ",")
prioLocales := make([]LocalePriority, len(languages))
for i, localeQPair := range languages {
localeQ := strings.Split(localeQPair, ";")
prioLocale := new(LocalePriority)
if localeQ[0] == localeQPair {
prioLocale.Locale = localeQ[0]
prioLocale.Priority = 1.0
} else {
qSplit := strings.Split(localeQ[1], "=")
q, err := strconv.ParseFloat(qSplit[1], 32)
if err != nil {
q = 0
}
prioLocale.Locale = localeQ[0]
prioLocale.Priority = q
}
prioLocales[i] = *prioLocale
}
sort.Sort(sort.Reverse(ByPriority(prioLocales)))
locales := make([]string, len(languages))
for i, l := range prioLocales {
locales[i] = l.Locale
}
return locales
}