Commit 5bba6f75fa2ef7cfd645ffef759e77cfa331c560
1 parent
0207726c47
Exists in
master
bug fix
Showing
1 changed file
with
1 additions
and
1 deletions
Show diff stats
localization.go
1 | package webutility | 1 | package webutility |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | 4 | "encoding/json" |
5 | "errors" | 5 | "errors" |
6 | "fmt" | 6 | "fmt" |
7 | "io/ioutil" | 7 | "io/ioutil" |
8 | "strings" | 8 | "strings" |
9 | "sync" | 9 | "sync" |
10 | ) | 10 | ) |
11 | 11 | ||
12 | type Dictionary struct { | 12 | type Dictionary struct { |
13 | my sync.Mutex | 13 | my sync.Mutex |
14 | locales map[string]map[string]string | 14 | locales map[string]map[string]string |
15 | supported []string | 15 | supported []string |
16 | defaultLocale string | 16 | defaultLocale string |
17 | } | 17 | } |
18 | 18 | ||
19 | func NewDictionary() *Dictionary { | 19 | func NewDictionary() *Dictionary { |
20 | return &Dictionary{ | 20 | return &Dictionary{ |
21 | locales: map[string]map[string]string{}, | 21 | locales: map[string]map[string]string{}, |
22 | } | 22 | } |
23 | } | 23 | } |
24 | 24 | ||
25 | func (d *Dictionary) AddTranslations(directory string) error { | 25 | func (d *Dictionary) AddTranslations(directory string) error { |
26 | files, err := ioutil.ReadDir(directory) | 26 | files, err := ioutil.ReadDir(directory) |
27 | if err != nil { | 27 | if err != nil { |
28 | return err | 28 | return err |
29 | } | 29 | } |
30 | 30 | ||
31 | for _, fileInfo := range files { | 31 | for _, fileInfo := range files { |
32 | fName := fileInfo.Name() | 32 | fName := fileInfo.Name() |
33 | path := directory + fName | 33 | path := directory + "/" + fName |
34 | file, err := ioutil.ReadFile(path) | 34 | file, err := ioutil.ReadFile(path) |
35 | if err != nil { | 35 | if err != nil { |
36 | return err | 36 | return err |
37 | } | 37 | } |
38 | 38 | ||
39 | fmt.Println(fName, path) | 39 | fmt.Println(fName, path) |
40 | 40 | ||
41 | break | 41 | break |
42 | 42 | ||
43 | loc := stripFileExtension(fName) | 43 | loc := stripFileExtension(fName) |
44 | 44 | ||
45 | var data interface{} | 45 | var data interface{} |
46 | err = json.Unmarshal(file, &data) | 46 | err = json.Unmarshal(file, &data) |
47 | if err != nil { | 47 | if err != nil { |
48 | return err | 48 | return err |
49 | } | 49 | } |
50 | 50 | ||
51 | l := map[string]string{} | 51 | l := map[string]string{} |
52 | for k, v := range data.(map[string]interface{}) { | 52 | for k, v := range data.(map[string]interface{}) { |
53 | l[k] = v.(string) | 53 | l[k] = v.(string) |
54 | } | 54 | } |
55 | 55 | ||
56 | mu.Lock() | 56 | mu.Lock() |
57 | defer mu.Unlock() | 57 | defer mu.Unlock() |
58 | d.locales[loc] = l | 58 | d.locales[loc] = l |
59 | d.supported = append(d.supported, loc) | 59 | d.supported = append(d.supported, loc) |
60 | } | 60 | } |
61 | 61 | ||
62 | return nil | 62 | return nil |
63 | } | 63 | } |
64 | 64 | ||
65 | func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) { | 65 | func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) { |
66 | accepted := d.parseAcceptedLanguageHeader(acceptedLanguages) | 66 | accepted := d.parseAcceptedLanguageHeader(acceptedLanguages) |
67 | best = accepted[0] | 67 | best = accepted[0] |
68 | return | 68 | return |
69 | } | 69 | } |
70 | 70 | ||
71 | func (d *Dictionary) Translate(loc, key string) string { | 71 | func (d *Dictionary) Translate(loc, key string) string { |
72 | return d.locales[loc][key] | 72 | return d.locales[loc][key] |
73 | } | 73 | } |
74 | 74 | ||
75 | func (d *Dictionary) hasLocale(loc string) bool { | 75 | func (d *Dictionary) hasLocale(loc string) bool { |
76 | for _, v := range d.supported { | 76 | for _, v := range d.supported { |
77 | if v == loc { | 77 | if v == loc { |
78 | return true | 78 | return true |
79 | } | 79 | } |
80 | } | 80 | } |
81 | return false | 81 | return false |
82 | } | 82 | } |
83 | 83 | ||
84 | func (d *Dictionary) setDefaultLocale(loc string) error { | 84 | func (d *Dictionary) setDefaultLocale(loc string) error { |
85 | if !d.hasLocale(loc) { | 85 | if !d.hasLocale(loc) { |
86 | return errors.New("dictionary does not contain translations for " + loc) | 86 | return errors.New("dictionary does not contain translations for " + loc) |
87 | } | 87 | } |
88 | d.defaultLocale = loc | 88 | d.defaultLocale = loc |
89 | return nil | 89 | return nil |
90 | } | 90 | } |
91 | 91 | ||
92 | func (d *Dictionary) getDefaultLocale() string { | 92 | func (d *Dictionary) getDefaultLocale() string { |
93 | return d.defaultLocale | 93 | return d.defaultLocale |
94 | } | 94 | } |
95 | 95 | ||
96 | func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) { | 96 | func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) { |
97 | if acceptedLanguages == "" { | 97 | if acceptedLanguages == "" { |
98 | langs = append(langs, d.getDefaultLocale()) | 98 | langs = append(langs, d.getDefaultLocale()) |
99 | return | 99 | return |
100 | } | 100 | } |
101 | 101 | ||
102 | parts := strings.Split(acceptedLanguages, ",") | 102 | parts := strings.Split(acceptedLanguages, ",") |
103 | for _, p := range parts { | 103 | for _, p := range parts { |
104 | langs = append(langs, p) | 104 | langs = append(langs, p) |
105 | } | 105 | } |
106 | 106 | ||
107 | return | 107 | return |
108 | } | 108 | } |
109 | 109 | ||
110 | func stripFileExtension(full string) (stripped string) { | 110 | func stripFileExtension(full string) (stripped string) { |
111 | return | 111 | return |
112 | } | 112 | } |
113 | 113 |