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