Commit 63a06da0c72025060ad0bbc2747ba7172a8d7611
1 parent
2a5220c528
Exists in
master
automatic settings for default locale
Showing
1 changed file
with
4 additions
and
0 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 | "io/ioutil" | 6 | "io/ioutil" |
7 | "path" | 7 | "path" |
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 | loc := stripFileExtension(fName) | 39 | loc := stripFileExtension(fName) |
40 | 40 | ||
41 | var data interface{} | 41 | var data interface{} |
42 | err = json.Unmarshal(file, &data) | 42 | err = json.Unmarshal(file, &data) |
43 | if err != nil { | 43 | if err != nil { |
44 | return err | 44 | return err |
45 | } | 45 | } |
46 | 46 | ||
47 | l := map[string]string{} | 47 | l := map[string]string{} |
48 | for k, v := range data.(map[string]interface{}) { | 48 | for k, v := range data.(map[string]interface{}) { |
49 | l[k] = v.(string) | 49 | l[k] = v.(string) |
50 | } | 50 | } |
51 | 51 | ||
52 | mu.Lock() | 52 | mu.Lock() |
53 | defer mu.Unlock() | 53 | defer mu.Unlock() |
54 | d.locales[loc] = l | 54 | d.locales[loc] = l |
55 | d.supported = append(d.supported, loc) | 55 | d.supported = append(d.supported, loc) |
56 | } | 56 | } |
57 | 57 | ||
58 | if len(d.supported) > 0 { | ||
59 | d.setDefaultLocale(d.supported[0]) | ||
60 | } | ||
61 | |||
58 | return nil | 62 | return nil |
59 | } | 63 | } |
60 | 64 | ||
61 | func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) { | 65 | func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) { |
62 | accepted := d.parseAcceptedLanguageHeader(acceptedLanguages) | 66 | accepted := d.parseAcceptedLanguageHeader(acceptedLanguages) |
63 | best = accepted[0] | 67 | best = accepted[0] |
64 | return | 68 | return |
65 | } | 69 | } |
66 | 70 | ||
67 | func (d *Dictionary) Translate(loc, key string) string { | 71 | func (d *Dictionary) Translate(loc, key string) string { |
68 | return d.locales[loc][key] | 72 | return d.locales[loc][key] |
69 | } | 73 | } |
70 | 74 | ||
71 | func (d *Dictionary) hasLocale(loc string) bool { | 75 | func (d *Dictionary) hasLocale(loc string) bool { |
72 | for _, v := range d.supported { | 76 | for _, v := range d.supported { |
73 | if v == loc { | 77 | if v == loc { |
74 | return true | 78 | return true |
75 | } | 79 | } |
76 | } | 80 | } |
77 | return false | 81 | return false |
78 | } | 82 | } |
79 | 83 | ||
80 | func (d *Dictionary) setDefaultLocale(loc string) error { | 84 | func (d *Dictionary) setDefaultLocale(loc string) error { |
81 | if !d.hasLocale(loc) { | 85 | if !d.hasLocale(loc) { |
82 | return errors.New("dictionary does not contain translations for " + loc) | 86 | return errors.New("dictionary does not contain translations for " + loc) |
83 | } | 87 | } |
84 | d.defaultLocale = loc | 88 | d.defaultLocale = loc |
85 | return nil | 89 | return nil |
86 | } | 90 | } |
87 | 91 | ||
88 | func (d *Dictionary) getDefaultLocale() string { | 92 | func (d *Dictionary) getDefaultLocale() string { |
89 | return d.defaultLocale | 93 | return d.defaultLocale |
90 | } | 94 | } |
91 | 95 | ||
92 | func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) { | 96 | func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) { |
93 | if acceptedLanguages == "" { | 97 | if acceptedLanguages == "" { |
94 | langs = append(langs, d.getDefaultLocale()) | 98 | langs = append(langs, d.getDefaultLocale()) |
95 | return | 99 | return |
96 | } | 100 | } |
97 | 101 | ||
98 | parts := strings.Split(acceptedLanguages, ",") | 102 | parts := strings.Split(acceptedLanguages, ",") |
99 | for _, p := range parts { | 103 | for _, p := range parts { |
100 | langs = append(langs, p) | 104 | langs = append(langs, p) |
101 | } | 105 | } |
102 | 106 | ||
103 | return | 107 | return |
104 | } | 108 | } |
105 | 109 | ||
106 | func stripFileExtension(full string) (stripped string) { | 110 | func stripFileExtension(full string) (stripped string) { |
107 | extension := path.Ext(full) | 111 | extension := path.Ext(full) |
108 | stripped = strings.TrimSuffix(full, extension) | 112 | stripped = strings.TrimSuffix(full, extension) |
109 | return stripped | 113 | return stripped |
110 | } | 114 | } |
111 | 115 |