Blame view
localization.go
2.6 KB
9933169c8 localization support |
1 2 3 4 |
package webutility import ( "encoding/json" |
0207726c4 improved localiza... |
5 |
"fmt" |
9933169c8 localization support |
6 |
"io/ioutil" |
e97375e4d strip file extension |
7 |
"path" |
715bb6ad8 parsing of accept... |
8 |
"strconv" |
3712c373f better localizati... |
9 |
"strings" |
3fffcb954 removed old http API |
10 |
"sync" |
9933169c8 localization support |
11 |
) |
f84e7607d added dictionary;... |
12 |
type Dictionary struct { |
3fffcb954 removed old http API |
13 |
my sync.Mutex |
a205e8f40 changes |
14 15 16 |
locales map[string]map[string]string supported []string defaultLocale string |
f84e7607d added dictionary;... |
17 |
} |
715bb6ad8 parsing of accept... |
18 19 |
func NewDictionary(def string) *Dictionary { d := Dictionary{ |
f84e7607d added dictionary;... |
20 21 |
locales: map[string]map[string]string{}, } |
715bb6ad8 parsing of accept... |
22 23 |
d.defaultLocale = def return &d |
f84e7607d added dictionary;... |
24 |
} |
9933169c8 localization support |
25 |
|
0207726c4 improved localiza... |
26 27 |
func (d *Dictionary) AddTranslations(directory string) error { files, err := ioutil.ReadDir(directory) |
9933169c8 localization support |
28 29 30 |
if err != nil { return err } |
0207726c4 improved localiza... |
31 32 |
for _, fileInfo := range files { fName := fileInfo.Name() |
5bba6f75f bug fix |
33 |
path := directory + "/" + fName |
0207726c4 improved localiza... |
34 35 36 37 |
file, err := ioutil.ReadFile(path) if err != nil { return err } |
9933169c8 localization support |
38 |
|
e97375e4d strip file extension |
39 |
loc := stripFileExtension(fName) |
0207726c4 improved localiza... |
40 |
|
0207726c4 improved localiza... |
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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 |
57 |
|
715bb6ad8 parsing of accept... |
58 59 60 61 62 |
if d.defaultLocale == "" { if len(d.supported) > 0 { d.defaultLocale = d.supported[0] } } |
9933169c8 localization support |
63 64 |
return nil } |
715bb6ad8 parsing of accept... |
65 66 67 68 69 70 71 72 73 74 75 76 |
func (d *Dictionary) GetBestMatchLocale(langs string) (best string) { accepted := d.parseAcceptedLanguages(langs) for i, _ := range accepted { for j, _ := range d.supported { if accepted[i].Code == d.supported[j] { return d.supported[j] } } } return d.defaultLocale |
0207726c4 improved localiza... |
77 |
} |
f84e7607d added dictionary;... |
78 79 80 |
func (d *Dictionary) Translate(loc, key string) string { return d.locales[loc][key] } |
0207726c4 improved localiza... |
81 |
func (d *Dictionary) hasLocale(loc string) bool { |
a205e8f40 changes |
82 83 |
for _, v := range d.supported { if v == loc { |
f84e7607d added dictionary;... |
84 85 86 87 |
return true } } return false |
9933169c8 localization support |
88 |
} |
a205e8f40 changes |
89 |
|
715bb6ad8 parsing of accept... |
90 91 92 |
type LangWeight struct { Code string Weight float64 |
a205e8f40 changes |
93 |
} |
715bb6ad8 parsing of accept... |
94 95 96 97 |
func (d *Dictionary) parseAcceptedLanguages(accepted string) (langs []LangWeight) { if accepted == "" { langs = append(langs, LangWeight{Code: d.defaultLocale, Weight: 1.0}) return langs |
3712c373f better localizati... |
98 |
} |
715bb6ad8 parsing of accept... |
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
parts := strings.Split(accepted, ",") for i, _ := range parts { parts[i] = strings.Trim(parts[i], "") var code string = "" var weight float64 = 0.0 cw := strings.Split(parts[i], ";") paramCount := len(cw) if paramCount == 1 { // default weight of 1 code = cw[0] weight = 1.0 } else if paramCount == 2 { // parse weight code = cw[0] weight, _ = strconv.ParseFloat(cw[1], 64) } fmt.Println(parts[i], code, weight) langs = append(langs, LangWeight{Code: code, Weight: weight}) |
3712c373f better localizati... |
123 |
} |
715bb6ad8 parsing of accept... |
124 125 126 |
// TODO(marko): sort langs by weights return langs |
3712c373f better localizati... |
127 |
} |
0207726c4 improved localiza... |
128 129 |
func stripFileExtension(full string) (stripped string) { |
e97375e4d strip file extension |
130 131 132 |
extension := path.Ext(full) stripped = strings.TrimSuffix(full, extension) return stripped |
0207726c4 improved localiza... |
133 |
} |