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