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