diff --git a/localization.go b/localization.go index b71ae99..391d7a9 100644 --- a/localization.go +++ b/localization.go @@ -4,6 +4,8 @@ import ( "encoding/json" "errors" "io/ioutil" + "net/http" + "strings" "sync" ) @@ -69,3 +71,24 @@ func (d *Dictionary) SetDefaultLocale(loc string) error { func (d *Dictionary) GetDefaultLocale() string { return d.defaultLocale } + +func (d *Dictionary) GetBestMatchLocale(req *http.Request) (best string) { + accepted := d.parseAcceptedLanguageHeader(req) + best = accepted[0] + return +} + +func (d *Dictionary) parseAcceptedLanguageHeader(req *http.Request) (langs []string) { + a := req.Header.Get("Accepted-Language") + if a == "" { + langs = append(langs, d.GetDefaultLocale()) + return + } + + parts := strings.Split(a, ",") + for _, p := range parts { + langs = append(langs, p) + } + + return +}