From 715bb6ad842d3e2c326c7eb3cf469373b4699d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20Tikvi=C4=87?= Date: Tue, 17 Jul 2018 11:29:11 +0200 Subject: [PATCH] parsing of accepted languages header --- localization.go | 82 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 57 insertions(+), 25 deletions(-) diff --git a/localization.go b/localization.go index 4cf0df6..78f6955 100644 --- a/localization.go +++ b/localization.go @@ -2,10 +2,10 @@ package webutility import ( "encoding/json" - "errors" "fmt" "io/ioutil" "path" + "strconv" "strings" "sync" ) @@ -17,10 +17,12 @@ type Dictionary struct { defaultLocale string } -func NewDictionary() *Dictionary { - return &Dictionary{ +func NewDictionary(def string) *Dictionary { + d := Dictionary{ locales: map[string]map[string]string{}, } + d.defaultLocale = def + return &d } func (d *Dictionary) AddTranslations(directory string) error { @@ -60,13 +62,27 @@ func (d *Dictionary) AddTranslations(directory string) error { d.supported = append(d.supported, loc) } + if d.defaultLocale == "" { + if len(d.supported) > 0 { + d.defaultLocale = d.supported[0] + } + } + return nil } -func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) { - accepted := d.parseAcceptedLanguageHeader(acceptedLanguages) - best = accepted[0] - return +func (d *Dictionary) GetBestMatchLocale(langs string) (best string) { + accepted := d.parseAcceptedLanguages(langs) + + for i, _ := range accepted { + for j, _ := range d.supported { + if accepted[i].Code == d.supported[j] { + return d.supported[j] + } + } + } + + return d.defaultLocale } func (d *Dictionary) Translate(loc, key string) string { @@ -82,30 +98,46 @@ func (d *Dictionary) hasLocale(loc string) bool { return false } -func (d *Dictionary) setDefaultLocale(loc string) error { - if !d.hasLocale(loc) { - return errors.New("dictionary does not contain translations for " + loc) - } - d.defaultLocale = loc - return nil +type LangWeight struct { + Code string + Weight float64 } -func (d *Dictionary) getDefaultLocale() string { - return d.defaultLocale -} - -func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) { - if acceptedLanguages == "" { - langs = append(langs, d.getDefaultLocale()) - return +func (d *Dictionary) parseAcceptedLanguages(accepted string) (langs []LangWeight) { + if accepted == "" { + langs = append(langs, LangWeight{Code: d.defaultLocale, Weight: 1.0}) + return langs } - parts := strings.Split(acceptedLanguages, ",") - for _, p := range parts { - langs = append(langs, p) + parts := strings.Split(accepted, ",") + for i, _ := range parts { + parts[i] = strings.Trim(parts[i], "") + + var code string = "" + var weight float64 = 0.0 + + cw := strings.Split(parts[i], ";") + paramCount := len(cw) + + if paramCount == 1 { + // default weight of 1 + code = cw[0] + weight = 1.0 + } else if paramCount == 2 { + // parse weight + code = cw[0] + weight, _ = strconv.ParseFloat(cw[1], 64) + + } + + fmt.Println(parts[i], code, weight) + + langs = append(langs, LangWeight{Code: code, Weight: weight}) } - return + // TODO(marko): sort langs by weights + + return langs } func stripFileExtension(full string) (stripped string) { -- 1.8.1.2