Blame view
localization.go
2.03 KB
9933169c8 localization support |
1 2 3 4 |
package webutility import ( "encoding/json" |
a205e8f40 changes |
5 |
"errors" |
0207726c4 improved localiza... |
6 |
"fmt" |
9933169c8 localization support |
7 |
"io/ioutil" |
3712c373f better localizati... |
8 |
"strings" |
3fffcb954 removed old http API |
9 |
"sync" |
9933169c8 localization support |
10 |
) |
f84e7607d added dictionary;... |
11 |
type Dictionary struct { |
3fffcb954 removed old http API |
12 |
my sync.Mutex |
a205e8f40 changes |
13 14 15 |
locales map[string]map[string]string supported []string defaultLocale string |
f84e7607d added dictionary;... |
16 |
} |
ad8e9dd2a added middleware ... |
17 18 |
func NewDictionary() *Dictionary { return &Dictionary{ |
f84e7607d added dictionary;... |
19 20 21 |
locales: map[string]map[string]string{}, } } |
9933169c8 localization support |
22 |
|
0207726c4 improved localiza... |
23 24 |
func (d *Dictionary) AddTranslations(directory string) error { files, err := ioutil.ReadDir(directory) |
9933169c8 localization support |
25 26 27 |
if err != nil { return err } |
0207726c4 improved localiza... |
28 29 30 31 32 33 34 |
for _, fileInfo := range files { fName := fileInfo.Name() path := directory + fName file, err := ioutil.ReadFile(path) if err != nil { return err } |
9933169c8 localization support |
35 |
|
0207726c4 improved localiza... |
36 37 38 39 40 |
fmt.Println(fName, path) break loc := stripFileExtension(fName) |
3fffcb954 removed old http API |
41 |
|
0207726c4 improved localiza... |
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
var data interface{} err = json.Unmarshal(file, &data) if err != nil { return err } l := map[string]string{} for k, v := range data.(map[string]interface{}) { l[k] = v.(string) } mu.Lock() defer mu.Unlock() d.locales[loc] = l d.supported = append(d.supported, loc) } |
9933169c8 localization support |
58 59 60 |
return nil } |
0207726c4 improved localiza... |
61 62 63 64 65 |
func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) { accepted := d.parseAcceptedLanguageHeader(acceptedLanguages) best = accepted[0] return } |
f84e7607d added dictionary;... |
66 67 68 |
func (d *Dictionary) Translate(loc, key string) string { return d.locales[loc][key] } |
0207726c4 improved localiza... |
69 |
func (d *Dictionary) hasLocale(loc string) bool { |
a205e8f40 changes |
70 71 |
for _, v := range d.supported { if v == loc { |
f84e7607d added dictionary;... |
72 73 74 75 |
return true } } return false |
9933169c8 localization support |
76 |
} |
a205e8f40 changes |
77 |
|
0207726c4 improved localiza... |
78 79 |
func (d *Dictionary) setDefaultLocale(loc string) error { if !d.hasLocale(loc) { |
a205e8f40 changes |
80 81 82 83 84 |
return errors.New("dictionary does not contain translations for " + loc) } d.defaultLocale = loc return nil } |
0207726c4 improved localiza... |
85 |
func (d *Dictionary) getDefaultLocale() string { |
a205e8f40 changes |
86 87 |
return d.defaultLocale } |
3712c373f better localizati... |
88 |
|
5d654d249 interface change |
89 90 |
func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) { if acceptedLanguages == "" { |
0207726c4 improved localiza... |
91 |
langs = append(langs, d.getDefaultLocale()) |
3712c373f better localizati... |
92 93 |
return } |
5d654d249 interface change |
94 |
parts := strings.Split(acceptedLanguages, ",") |
3712c373f better localizati... |
95 96 97 98 99 100 |
for _, p := range parts { langs = append(langs, p) } return } |
0207726c4 improved localiza... |
101 102 103 104 |
func stripFileExtension(full string) (stripped string) { return } |