Blame view

localization.go 2.13 KB
9933169c8   Marko Tikvić   localization support
1
2
3
4
  package webutility
  
  import (
  	"encoding/json"
a205e8f40   Marko Tikvić   changes
5
  	"errors"
0207726c4   Marko Tikvić   improved localiza...
6
  	"fmt"
9933169c8   Marko Tikvić   localization support
7
  	"io/ioutil"
e97375e4d   Marko Tikvić   strip file extension
8
  	"path"
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
  }
ad8e9dd2a   Marko Tikvić   added middleware ...
18
19
  func NewDictionary() *Dictionary {
  	return &Dictionary{
f84e7607d   Marko Tikvić   added dictionary;...
20
21
22
  		locales: map[string]map[string]string{},
  	}
  }
9933169c8   Marko Tikvić   localization support
23

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

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

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

e97375e4d   Marko Tikvić   strip file extension
41
  		break
3fffcb954   Marko Tikvić   removed old http API
42

0207726c4   Marko Tikvić   improved localiza...
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  		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
59
60
61
  
  	return nil
  }
0207726c4   Marko Tikvić   improved localiza...
62
63
64
65
66
  func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) {
  	accepted := d.parseAcceptedLanguageHeader(acceptedLanguages)
  	best = accepted[0]
  	return
  }
f84e7607d   Marko Tikvić   added dictionary;...
67
68
69
  func (d *Dictionary) Translate(loc, key string) string {
  	return d.locales[loc][key]
  }
0207726c4   Marko Tikvić   improved localiza...
70
  func (d *Dictionary) hasLocale(loc string) bool {
a205e8f40   Marko Tikvić   changes
71
72
  	for _, v := range d.supported {
  		if v == loc {
f84e7607d   Marko Tikvić   added dictionary;...
73
74
75
76
  			return true
  		}
  	}
  	return false
9933169c8   Marko Tikvić   localization support
77
  }
a205e8f40   Marko Tikvić   changes
78

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

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