Commit 89f45d7aa83a4bb8347c80404b5856e648e14731

Authored by Marko Tikvić
1 parent e1e658e7fe
Exists in master

GetBestMatchLocale takes in *http.Request for simplicity

Showing 1 changed file with 9 additions and 8 deletions   Show diff stats
... ... @@ -2,8 +2,8 @@ package webutility
2 2  
3 3 import (
4 4 "encoding/json"
5   - "fmt"
6 5 "io/ioutil"
  6 + "net/http"
7 7 "path"
8 8 "strconv"
9 9 "strings"
... ... @@ -67,10 +67,13 @@ func (d *Dictionary) AddTranslations(directory string) error {
67 67 return nil
68 68 }
69 69  
70   -func (d *Dictionary) GetBestMatchLocale(langs string) (best string) {
71   - accepted := d.parseAcceptedLanguages(langs)
  70 +func (d *Dictionary) GetBestMatchLocale(req *http.Request) (best string) {
  71 + accepted := d.parseAcceptedLanguages(req.Header.Get("Accept-Language"))
72 72  
73 73 for i, _ := range accepted {
  74 + if accepted[i].Code == "*" {
  75 + return d.defaultLocale
  76 + }
74 77 for j, _ := range d.supported {
75 78 if accepted[i].Code == d.supported[j] {
76 79 return d.supported[j]
... ... @@ -107,7 +110,7 @@ func (d *Dictionary) parseAcceptedLanguages(accepted string) (langs []LangWeight
107 110  
108 111 parts := strings.Split(accepted, ",")
109 112 for i, _ := range parts {
110   - parts[i] = strings.Trim(parts[i], "")
  113 + parts[i] = strings.Trim(parts[i], " ")
111 114  
112 115 var code string = ""
113 116 var weight float64 = 0.0
... ... @@ -122,16 +125,14 @@ func (d *Dictionary) parseAcceptedLanguages(accepted string) (langs []LangWeight
122 125 } else if paramCount == 2 {
123 126 // parse weight
124 127 code = cw[0]
125   - weight, _ = strconv.ParseFloat(cw[1], 64)
  128 + weight, _ = strconv.ParseFloat(cw[1][2:], 64)
126 129  
127 130 }
128 131  
129   - fmt.Println(parts[i], code, weight)
130   -
131 132 langs = append(langs, LangWeight{Code: code, Weight: weight})
132 133 }
133 134  
134   - // TODO(marko): sort langs by weights
  135 + // TODO(marko): sort langs by weights?
135 136  
136 137 return langs
137 138 }
... ...