Commit 5d654d249a5d096cfdddd35bd9c143c3f7447b12

Authored by Marko Tikvić
1 parent 3712c373f9
Exists in master

interface change

Showing 1 changed file with 5 additions and 7 deletions   Show diff stats
1 package webutility 1 package webutility
2 2
3 import ( 3 import (
4 "encoding/json" 4 "encoding/json"
5 "errors" 5 "errors"
6 "io/ioutil" 6 "io/ioutil"
7 "net/http"
8 "strings" 7 "strings"
9 "sync" 8 "sync"
10 ) 9 )
11 10
12 type Dictionary struct { 11 type Dictionary struct {
13 my sync.Mutex 12 my sync.Mutex
14 locales map[string]map[string]string 13 locales map[string]map[string]string
15 supported []string 14 supported []string
16 defaultLocale string 15 defaultLocale string
17 } 16 }
18 17
19 func NewDictionary() *Dictionary { 18 func NewDictionary() *Dictionary {
20 return &Dictionary{ 19 return &Dictionary{
21 locales: map[string]map[string]string{}, 20 locales: map[string]map[string]string{},
22 } 21 }
23 } 22 }
24 23
25 func (d *Dictionary) AddLocale(loc, filePath string) error { 24 func (d *Dictionary) AddLocale(loc, filePath string) error {
26 file, err := ioutil.ReadFile(filePath) 25 file, err := ioutil.ReadFile(filePath)
27 if err != nil { 26 if err != nil {
28 return err 27 return err
29 } 28 }
30 29
31 var data interface{} 30 var data interface{}
32 err = json.Unmarshal(file, &data) 31 err = json.Unmarshal(file, &data)
33 if err != nil { 32 if err != nil {
34 return err 33 return err
35 } 34 }
36 35
37 l := map[string]string{} 36 l := map[string]string{}
38 for k, v := range data.(map[string]interface{}) { 37 for k, v := range data.(map[string]interface{}) {
39 l[k] = v.(string) 38 l[k] = v.(string)
40 } 39 }
41 40
42 mu.Lock() 41 mu.Lock()
43 defer mu.Unlock() 42 defer mu.Unlock()
44 d.locales[loc] = l 43 d.locales[loc] = l
45 d.supported = append(d.supported, loc) 44 d.supported = append(d.supported, loc)
46 45
47 return nil 46 return nil
48 } 47 }
49 48
50 func (d *Dictionary) Translate(loc, key string) string { 49 func (d *Dictionary) Translate(loc, key string) string {
51 return d.locales[loc][key] 50 return d.locales[loc][key]
52 } 51 }
53 52
54 func (d *Dictionary) HasLocale(loc string) bool { 53 func (d *Dictionary) HasLocale(loc string) bool {
55 for _, v := range d.supported { 54 for _, v := range d.supported {
56 if v == loc { 55 if v == loc {
57 return true 56 return true
58 } 57 }
59 } 58 }
60 return false 59 return false
61 } 60 }
62 61
63 func (d *Dictionary) SetDefaultLocale(loc string) error { 62 func (d *Dictionary) SetDefaultLocale(loc string) error {
64 if !d.HasLocale(loc) { 63 if !d.HasLocale(loc) {
65 return errors.New("dictionary does not contain translations for " + loc) 64 return errors.New("dictionary does not contain translations for " + loc)
66 } 65 }
67 d.defaultLocale = loc 66 d.defaultLocale = loc
68 return nil 67 return nil
69 } 68 }
70 69
71 func (d *Dictionary) GetDefaultLocale() string { 70 func (d *Dictionary) GetDefaultLocale() string {
72 return d.defaultLocale 71 return d.defaultLocale
73 } 72 }
74 73
75 func (d *Dictionary) GetBestMatchLocale(req *http.Request) (best string) { 74 func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) {
76 accepted := d.parseAcceptedLanguageHeader(req) 75 accepted := d.parseAcceptedLanguageHeader(acceptedLanguages)
77 best = accepted[0] 76 best = accepted[0]
78 return 77 return
79 } 78 }
80 79
81 func (d *Dictionary) parseAcceptedLanguageHeader(req *http.Request) (langs []string) { 80 func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) {
82 a := req.Header.Get("Accepted-Language") 81 if acceptedLanguages == "" {
83 if a == "" {
84 langs = append(langs, d.GetDefaultLocale()) 82 langs = append(langs, d.GetDefaultLocale())
85 return 83 return
86 } 84 }
87 85
88 parts := strings.Split(a, ",") 86 parts := strings.Split(acceptedLanguages, ",")
89 for _, p := range parts { 87 for _, p := range parts {
90 langs = append(langs, p) 88 langs = append(langs, p)
91 } 89 }
92 90
93 return 91 return
94 } 92 }
95 93