Blame view

localization.go 2.15 KB
9933169c8   Marko Tikvić   localization support
1
2
3
4
  package webutility
  
  import (
  	"encoding/json"
a205e8f40   Marko Tikvić   changes
5
  	"errors"
9933169c8   Marko Tikvić   localization support
6
  	"io/ioutil"
e97375e4d   Marko Tikvić   strip file extension
7
  	"path"
3712c373f   Marko Tikvić   better localizati...
8
  	"strings"
3fffcb954   Marko Tikvić   removed old http API
9
  	"sync"
9933169c8   Marko Tikvić   localization support
10
  )
f84e7607d   Marko Tikvić   added dictionary;...
11
  type Dictionary struct {
3fffcb954   Marko Tikvić   removed old http API
12
  	my            sync.Mutex
a205e8f40   Marko Tikvić   changes
13
14
15
  	locales       map[string]map[string]string
  	supported     []string
  	defaultLocale string
f84e7607d   Marko Tikvić   added dictionary;...
16
  }
ad8e9dd2a   Marko Tikvić   added middleware ...
17
18
  func NewDictionary() *Dictionary {
  	return &Dictionary{
f84e7607d   Marko Tikvić   added dictionary;...
19
20
21
  		locales: map[string]map[string]string{},
  	}
  }
9933169c8   Marko Tikvić   localization support
22

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

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

0207726c4   Marko Tikvić   improved localiza...
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  		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
54

63a06da0c   Marko Tikvić   automatic setting...
55
56
57
  	if len(d.supported) > 0 {
  		d.setDefaultLocale(d.supported[0])
  	}
9933169c8   Marko Tikvić   localization support
58
59
  	return nil
  }
0207726c4   Marko Tikvić   improved localiza...
60
61
62
63
64
  func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) {
  	accepted := d.parseAcceptedLanguageHeader(acceptedLanguages)
  	best = accepted[0]
  	return
  }
f84e7607d   Marko Tikvić   added dictionary;...
65
66
67
  func (d *Dictionary) Translate(loc, key string) string {
  	return d.locales[loc][key]
  }
0207726c4   Marko Tikvić   improved localiza...
68
  func (d *Dictionary) hasLocale(loc string) bool {
a205e8f40   Marko Tikvić   changes
69
70
  	for _, v := range d.supported {
  		if v == loc {
f84e7607d   Marko Tikvić   added dictionary;...
71
72
73
74
  			return true
  		}
  	}
  	return false
9933169c8   Marko Tikvić   localization support
75
  }
a205e8f40   Marko Tikvić   changes
76

0207726c4   Marko Tikvić   improved localiza...
77
78
  func (d *Dictionary) setDefaultLocale(loc string) error {
  	if !d.hasLocale(loc) {
a205e8f40   Marko Tikvić   changes
79
80
81
82
83
  		return errors.New("dictionary does not contain translations for " + loc)
  	}
  	d.defaultLocale = loc
  	return nil
  }
0207726c4   Marko Tikvić   improved localiza...
84
  func (d *Dictionary) getDefaultLocale() string {
a205e8f40   Marko Tikvić   changes
85
86
  	return d.defaultLocale
  }
3712c373f   Marko Tikvić   better localizati...
87

5d654d249   Marko Tikvić   interface change
88
89
  func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) {
  	if acceptedLanguages == "" {
0207726c4   Marko Tikvić   improved localiza...
90
  		langs = append(langs, d.getDefaultLocale())
3712c373f   Marko Tikvić   better localizati...
91
92
  		return
  	}
5d654d249   Marko Tikvić   interface change
93
  	parts := strings.Split(acceptedLanguages, ",")
3712c373f   Marko Tikvić   better localizati...
94
95
96
97
98
99
  	for _, p := range parts {
  		langs = append(langs, p)
  	}
  
  	return
  }
0207726c4   Marko Tikvić   improved localiza...
100
101
  
  func stripFileExtension(full string) (stripped string) {
e97375e4d   Marko Tikvić   strip file extension
102
103
104
  	extension := path.Ext(full)
  	stripped = strings.TrimSuffix(full, extension)
  	return stripped
0207726c4   Marko Tikvić   improved localiza...
105
  }