Commit e1e658e7feb1d4f342d042de4015115aa41c3c89
Exists in
master
merge fix
Showing
1 changed file
Show diff stats
localization.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | 4 | "encoding/json" |
5 | "fmt" | 5 | "fmt" |
6 | "io/ioutil" | ||
7 | "path" | 6 | "io/ioutil" |
8 | "strconv" | 7 | "path" |
9 | "strings" | 8 | "strconv" |
10 | "sync" | 9 | "strings" |
11 | ) | 10 | "sync" |
12 | 11 | ) | |
13 | type Dictionary struct { | 12 | |
14 | my sync.Mutex | 13 | type Dictionary struct { |
15 | locales map[string]map[string]string | 14 | my sync.Mutex |
16 | supported []string | 15 | locales map[string]map[string]string |
17 | defaultLocale string | 16 | supported []string |
18 | } | 17 | defaultLocale string |
19 | 18 | } | |
20 | func NewDictionary(def string) *Dictionary { | 19 | |
21 | d := Dictionary{ | 20 | func NewDictionary(def string) *Dictionary { |
22 | locales: map[string]map[string]string{}, | 21 | d := Dictionary{ |
23 | } | 22 | locales: map[string]map[string]string{}, |
24 | d.defaultLocale = def | 23 | } |
25 | return &d | 24 | d.defaultLocale = def |
26 | } | 25 | return &d |
27 | 26 | } | |
28 | func (d *Dictionary) AddTranslations(directory string) error { | 27 | |
29 | files, err := ioutil.ReadDir(directory) | 28 | func (d *Dictionary) AddTranslations(directory string) error { |
30 | if err != nil { | 29 | files, err := ioutil.ReadDir(directory) |
31 | return err | 30 | if err != nil { |
32 | } | 31 | return err |
33 | 32 | } | |
34 | for _, fileInfo := range files { | 33 | |
35 | fName := fileInfo.Name() | 34 | for _, fileInfo := range files { |
36 | path := directory + "/" + fName | 35 | fName := fileInfo.Name() |
37 | file, err := ioutil.ReadFile(path) | 36 | path := directory + "/" + fName |
38 | if err != nil { | 37 | file, err := ioutil.ReadFile(path) |
39 | return err | 38 | if err != nil { |
40 | } | 39 | return err |
41 | 40 | } | |
42 | loc := stripFileExtension(fName) | ||
43 | |||
44 | fmt.Println(fName, path, loc) | ||
45 | |||
46 | break | 41 | |
47 | 42 | loc := stripFileExtension(fName) | |
48 | var data interface{} | 43 | |
49 | err = json.Unmarshal(file, &data) | 44 | var data interface{} |
50 | if err != nil { | 45 | err = json.Unmarshal(file, &data) |
51 | return err | 46 | if err != nil { |
52 | } | 47 | return err |
53 | 48 | } | |
54 | l := map[string]string{} | 49 | |
55 | for k, v := range data.(map[string]interface{}) { | 50 | l := map[string]string{} |
56 | l[k] = v.(string) | 51 | for k, v := range data.(map[string]interface{}) { |
57 | } | 52 | l[k] = v.(string) |
58 | 53 | } | |
59 | mu.Lock() | 54 | |
60 | defer mu.Unlock() | 55 | mu.Lock() |
61 | d.locales[loc] = l | 56 | defer mu.Unlock() |
62 | d.supported = append(d.supported, loc) | 57 | d.locales[loc] = l |
58 | d.supported = append(d.supported, loc) | ||
59 | } | ||
60 | |||
61 | if d.defaultLocale == "" { | ||
63 | } | 62 | if len(d.supported) > 0 { |
64 | 63 | d.defaultLocale = d.supported[0] | |
65 | if d.defaultLocale == "" { | 64 | } |
66 | if len(d.supported) > 0 { | 65 | } |
67 | d.defaultLocale = d.supported[0] | 66 | |
68 | } | 67 | return nil |
69 | } | 68 | } |
70 | 69 | ||
71 | return nil | 70 | func (d *Dictionary) GetBestMatchLocale(langs string) (best string) { |
72 | } | 71 | accepted := d.parseAcceptedLanguages(langs) |
73 | 72 | ||
74 | func (d *Dictionary) GetBestMatchLocale(langs string) (best string) { | 73 | for i, _ := range accepted { |
75 | accepted := d.parseAcceptedLanguages(langs) | 74 | for j, _ := range d.supported { |
76 | 75 | if accepted[i].Code == d.supported[j] { | |
77 | for i, _ := range accepted { | 76 | return d.supported[j] |
78 | for j, _ := range d.supported { | 77 | } |
79 | if accepted[i].Code == d.supported[j] { | 78 | } |
80 | return d.supported[j] | 79 | } |
81 | } | 80 | |
82 | } | 81 | return d.defaultLocale |
83 | } | 82 | } |
84 | 83 | ||
85 | return d.defaultLocale | 84 | func (d *Dictionary) Translate(loc, key string) string { |
86 | } | 85 | return d.locales[loc][key] |
87 | 86 | } | |
88 | func (d *Dictionary) Translate(loc, key string) string { | 87 | |
89 | return d.locales[loc][key] | 88 | func (d *Dictionary) hasLocale(loc string) bool { |
90 | } | 89 | for _, v := range d.supported { |
91 | 90 | if v == loc { | |
92 | func (d *Dictionary) hasLocale(loc string) bool { | 91 | return true |
93 | for _, v := range d.supported { | 92 | } |
94 | if v == loc { | 93 | } |
95 | return true | 94 | return false |
96 | } | 95 | } |
97 | } | 96 | |
98 | return false | 97 | type LangWeight struct { |
99 | } | 98 | Code string |
100 | 99 | Weight float64 | |
101 | type LangWeight struct { | 100 | } |
102 | Code string | 101 | |
103 | Weight float64 | 102 | func (d *Dictionary) parseAcceptedLanguages(accepted string) (langs []LangWeight) { |
104 | } | 103 | if accepted == "" { |
105 | 104 | langs = append(langs, LangWeight{Code: d.defaultLocale, Weight: 1.0}) | |
106 | func (d *Dictionary) parseAcceptedLanguages(accepted string) (langs []LangWeight) { | 105 | return langs |
107 | if accepted == "" { | 106 | } |
108 | langs = append(langs, LangWeight{Code: d.defaultLocale, Weight: 1.0}) | 107 | |
109 | return langs | 108 | parts := strings.Split(accepted, ",") |
110 | } | 109 | for i, _ := range parts { |
111 | 110 | parts[i] = strings.Trim(parts[i], "") | |
112 | parts := strings.Split(accepted, ",") | 111 | |
113 | for i, _ := range parts { | 112 | var code string = "" |
114 | parts[i] = strings.Trim(parts[i], "") | 113 | var weight float64 = 0.0 |
115 | 114 | ||
116 | var code string = "" | 115 | cw := strings.Split(parts[i], ";") |
117 | var weight float64 = 0.0 | 116 | paramCount := len(cw) |
118 | 117 | ||
119 | cw := strings.Split(parts[i], ";") | 118 | if paramCount == 1 { |
120 | paramCount := len(cw) | 119 | // default weight of 1 |
121 | 120 | code = cw[0] | |
122 | if paramCount == 1 { | 121 | weight = 1.0 |
123 | // default weight of 1 | 122 | } else if paramCount == 2 { |
124 | code = cw[0] | 123 | // parse weight |
125 | weight = 1.0 | 124 | code = cw[0] |
126 | } else if paramCount == 2 { | 125 | weight, _ = strconv.ParseFloat(cw[1], 64) |
127 | // parse weight | 126 | |
128 | code = cw[0] | 127 | } |
129 | weight, _ = strconv.ParseFloat(cw[1], 64) | 128 | |
130 | 129 | fmt.Println(parts[i], code, weight) | |
131 | } | 130 | |
132 | 131 | langs = append(langs, LangWeight{Code: code, Weight: weight}) | |
133 | fmt.Println(parts[i], code, weight) | 132 | } |
134 | 133 | ||
135 | langs = append(langs, LangWeight{Code: code, Weight: weight}) | 134 | // TODO(marko): sort langs by weights |
136 | } | 135 | |
137 | 136 | return langs | |
138 | // TODO(marko): sort langs by weights | 137 | } |
139 | 138 | ||
140 | return langs | 139 | func stripFileExtension(full string) (stripped string) { |
141 | } | 140 | extension := path.Ext(full) |
142 | 141 | stripped = strings.TrimSuffix(full, extension) | |
143 | func stripFileExtension(full string) (stripped string) { | 142 | return stripped |
144 | extension := path.Ext(full) | 143 | } |