Commit 3712c373f976c51a8ad64e2d941de0298eda8d37

Authored by Marko Tikvić
1 parent de25e1deb6
Exists in master

better localization detection

Showing 1 changed file with 23 additions and 0 deletions   Show diff stats
... ... @@ -4,6 +4,8 @@ import (
4 4 "encoding/json"
5 5 "errors"
6 6 "io/ioutil"
  7 + "net/http"
  8 + "strings"
7 9 "sync"
8 10 )
9 11  
... ... @@ -69,3 +71,24 @@ func (d *Dictionary) SetDefaultLocale(loc string) error {
69 71 func (d *Dictionary) GetDefaultLocale() string {
70 72 return d.defaultLocale
71 73 }
  74 +
  75 +func (d *Dictionary) GetBestMatchLocale(req *http.Request) (best string) {
  76 + accepted := d.parseAcceptedLanguageHeader(req)
  77 + best = accepted[0]
  78 + return
  79 +}
  80 +
  81 +func (d *Dictionary) parseAcceptedLanguageHeader(req *http.Request) (langs []string) {
  82 + a := req.Header.Get("Accepted-Language")
  83 + if a == "" {
  84 + langs = append(langs, d.GetDefaultLocale())
  85 + return
  86 + }
  87 +
  88 + parts := strings.Split(a, ",")
  89 + for _, p := range parts {
  90 + langs = append(langs, p)
  91 + }
  92 +
  93 + return
  94 +}
... ...