Blame view

localization.go 2.64 KB
9933169c8   Marko Tikvić   localization support
1
2
3
4
  package webutility
  
  import (
  	"encoding/json"
0207726c4   Marko Tikvić   improved localiza...
5
  	"fmt"
9933169c8   Marko Tikvić   localization support
6
  	"io/ioutil"
e97375e4d   Marko Tikvić   strip file extension
7
  	"path"
715bb6ad8   Marko Tikvić   parsing of accept...
8
  	"strconv"
3712c373f   Marko Tikvić   better localizati...
9
  	"strings"
3fffcb954   Marko Tikvić   removed old http API
10
  	"sync"
9933169c8   Marko Tikvić   localization support
11
  )
f84e7607d   Marko Tikvić   added dictionary;...
12
  type Dictionary struct {
3fffcb954   Marko Tikvić   removed old http API
13
  	my            sync.Mutex
a205e8f40   Marko Tikvić   changes
14
15
16
  	locales       map[string]map[string]string
  	supported     []string
  	defaultLocale string
f84e7607d   Marko Tikvić   added dictionary;...
17
  }
715bb6ad8   Marko Tikvić   parsing of accept...
18
19
  func NewDictionary(def string) *Dictionary {
  	d := Dictionary{
f84e7607d   Marko Tikvić   added dictionary;...
20
21
  		locales: map[string]map[string]string{},
  	}
715bb6ad8   Marko Tikvić   parsing of accept...
22
23
  	d.defaultLocale = def
  	return &d
f84e7607d   Marko Tikvić   added dictionary;...
24
  }
9933169c8   Marko Tikvić   localization support
25

0207726c4   Marko Tikvić   improved localiza...
26
27
  func (d *Dictionary) AddTranslations(directory string) error {
  	files, err := ioutil.ReadDir(directory)
9933169c8   Marko Tikvić   localization support
28
29
30
  	if err != nil {
  		return err
  	}
0207726c4   Marko Tikvić   improved localiza...
31
32
  	for _, fileInfo := range files {
  		fName := fileInfo.Name()
5bba6f75f   Marko Tikvić   bug fix
33
  		path := directory + "/" + fName
0207726c4   Marko Tikvić   improved localiza...
34
35
36
37
  		file, err := ioutil.ReadFile(path)
  		if err != nil {
  			return err
  		}
9933169c8   Marko Tikvić   localization support
38

e97375e4d   Marko Tikvić   strip file extension
39
  		loc := stripFileExtension(fName)
0207726c4   Marko Tikvić   improved localiza...
40

e97375e4d   Marko Tikvić   strip file extension
41
  		fmt.Println(fName, path, loc)
0207726c4   Marko Tikvić   improved localiza...
42

e97375e4d   Marko Tikvić   strip file extension
43
  		break
3fffcb954   Marko Tikvić   removed old http API
44

0207726c4   Marko Tikvić   improved localiza...
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  		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   Marko Tikvić   localization support
61

715bb6ad8   Marko Tikvić   parsing of accept...
62
63
64
65
66
  	if d.defaultLocale == "" {
  		if len(d.supported) > 0 {
  			d.defaultLocale = d.supported[0]
  		}
  	}
9933169c8   Marko Tikvić   localization support
67
68
  	return nil
  }
715bb6ad8   Marko Tikvić   parsing of accept...
69
70
71
72
73
74
75
76
77
78
79
80
  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   Marko Tikvić   improved localiza...
81
  }
f84e7607d   Marko Tikvić   added dictionary;...
82
83
84
  func (d *Dictionary) Translate(loc, key string) string {
  	return d.locales[loc][key]
  }
0207726c4   Marko Tikvić   improved localiza...
85
  func (d *Dictionary) hasLocale(loc string) bool {
a205e8f40   Marko Tikvić   changes
86
87
  	for _, v := range d.supported {
  		if v == loc {
f84e7607d   Marko Tikvić   added dictionary;...
88
89
90
91
  			return true
  		}
  	}
  	return false
9933169c8   Marko Tikvić   localization support
92
  }
a205e8f40   Marko Tikvić   changes
93

715bb6ad8   Marko Tikvić   parsing of accept...
94
95
96
  type LangWeight struct {
  	Code   string
  	Weight float64
a205e8f40   Marko Tikvić   changes
97
  }
715bb6ad8   Marko Tikvić   parsing of accept...
98
99
100
101
  func (d *Dictionary) parseAcceptedLanguages(accepted string) (langs []LangWeight) {
  	if accepted == "" {
  		langs = append(langs, LangWeight{Code: d.defaultLocale, Weight: 1.0})
  		return langs
3712c373f   Marko Tikvić   better localizati...
102
  	}
715bb6ad8   Marko Tikvić   parsing of accept...
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  	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   Marko Tikvić   better localizati...
127
  	}
715bb6ad8   Marko Tikvić   parsing of accept...
128
129
130
  	// TODO(marko): sort langs by weights
  
  	return langs
3712c373f   Marko Tikvić   better localizati...
131
  }
0207726c4   Marko Tikvić   improved localiza...
132
133
  
  func stripFileExtension(full string) (stripped string) {
e97375e4d   Marko Tikvić   strip file extension
134
135
136
  	extension := path.Ext(full)
  	stripped = strings.TrimSuffix(full, extension)
  	return stripped
0207726c4   Marko Tikvić   improved localiza...
137
  }