Commit 715bb6ad842d3e2c326c7eb3cf469373b4699d48
1 parent
e97375e4d5
Exists in
master
parsing of accepted languages header
Showing
1 changed file
with
57 additions
and
25 deletions
Show diff stats
localization.go
... | ... | @@ -2,10 +2,10 @@ package webutility |
2 | 2 | |
3 | 3 | import ( |
4 | 4 | "encoding/json" |
5 | - "errors" | |
6 | 5 | "fmt" |
7 | 6 | "io/ioutil" |
8 | 7 | "path" |
8 | + "strconv" | |
9 | 9 | "strings" |
10 | 10 | "sync" |
11 | 11 | ) |
... | ... | @@ -17,10 +17,12 @@ type Dictionary struct { |
17 | 17 | defaultLocale string |
18 | 18 | } |
19 | 19 | |
20 | -func NewDictionary() *Dictionary { | |
21 | - return &Dictionary{ | |
20 | +func NewDictionary(def string) *Dictionary { | |
21 | + d := Dictionary{ | |
22 | 22 | locales: map[string]map[string]string{}, |
23 | 23 | } |
24 | + d.defaultLocale = def | |
25 | + return &d | |
24 | 26 | } |
25 | 27 | |
26 | 28 | func (d *Dictionary) AddTranslations(directory string) error { |
... | ... | @@ -60,13 +62,27 @@ func (d *Dictionary) AddTranslations(directory string) error { |
60 | 62 | d.supported = append(d.supported, loc) |
61 | 63 | } |
62 | 64 | |
65 | + if d.defaultLocale == "" { | |
66 | + if len(d.supported) > 0 { | |
67 | + d.defaultLocale = d.supported[0] | |
68 | + } | |
69 | + } | |
70 | + | |
63 | 71 | return nil |
64 | 72 | } |
65 | 73 | |
66 | -func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) { | |
67 | - accepted := d.parseAcceptedLanguageHeader(acceptedLanguages) | |
68 | - best = accepted[0] | |
69 | - return | |
74 | +func (d *Dictionary) GetBestMatchLocale(langs string) (best string) { | |
75 | + accepted := d.parseAcceptedLanguages(langs) | |
76 | + | |
77 | + for i, _ := range accepted { | |
78 | + for j, _ := range d.supported { | |
79 | + if accepted[i].Code == d.supported[j] { | |
80 | + return d.supported[j] | |
81 | + } | |
82 | + } | |
83 | + } | |
84 | + | |
85 | + return d.defaultLocale | |
70 | 86 | } |
71 | 87 | |
72 | 88 | func (d *Dictionary) Translate(loc, key string) string { |
... | ... | @@ -82,30 +98,46 @@ func (d *Dictionary) hasLocale(loc string) bool { |
82 | 98 | return false |
83 | 99 | } |
84 | 100 | |
85 | -func (d *Dictionary) setDefaultLocale(loc string) error { | |
86 | - if !d.hasLocale(loc) { | |
87 | - return errors.New("dictionary does not contain translations for " + loc) | |
88 | - } | |
89 | - d.defaultLocale = loc | |
90 | - return nil | |
101 | +type LangWeight struct { | |
102 | + Code string | |
103 | + Weight float64 | |
91 | 104 | } |
92 | 105 | |
93 | -func (d *Dictionary) getDefaultLocale() string { | |
94 | - return d.defaultLocale | |
95 | -} | |
96 | - | |
97 | -func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) { | |
98 | - if acceptedLanguages == "" { | |
99 | - langs = append(langs, d.getDefaultLocale()) | |
100 | - return | |
106 | +func (d *Dictionary) parseAcceptedLanguages(accepted string) (langs []LangWeight) { | |
107 | + if accepted == "" { | |
108 | + langs = append(langs, LangWeight{Code: d.defaultLocale, Weight: 1.0}) | |
109 | + return langs | |
101 | 110 | } |
102 | 111 | |
103 | - parts := strings.Split(acceptedLanguages, ",") | |
104 | - for _, p := range parts { | |
105 | - langs = append(langs, p) | |
112 | + parts := strings.Split(accepted, ",") | |
113 | + for i, _ := range parts { | |
114 | + parts[i] = strings.Trim(parts[i], "") | |
115 | + | |
116 | + var code string = "" | |
117 | + var weight float64 = 0.0 | |
118 | + | |
119 | + cw := strings.Split(parts[i], ";") | |
120 | + paramCount := len(cw) | |
121 | + | |
122 | + if paramCount == 1 { | |
123 | + // default weight of 1 | |
124 | + code = cw[0] | |
125 | + weight = 1.0 | |
126 | + } else if paramCount == 2 { | |
127 | + // parse weight | |
128 | + code = cw[0] | |
129 | + weight, _ = strconv.ParseFloat(cw[1], 64) | |
130 | + | |
131 | + } | |
132 | + | |
133 | + fmt.Println(parts[i], code, weight) | |
134 | + | |
135 | + langs = append(langs, LangWeight{Code: code, Weight: weight}) | |
106 | 136 | } |
107 | 137 | |
108 | - return | |
138 | + // TODO(marko): sort langs by weights | |
139 | + | |
140 | + return langs | |
109 | 141 | } |
110 | 142 | |
111 | 143 | func stripFileExtension(full string) (stripped string) { | ... | ... |