Blame view

locale_utility.go 821 Bytes
9933169c8   Marko Tikvić   localization support
1
2
3
4
5
6
  package webutility
  
  import (
  	"encoding/json"
  	"io/ioutil"
  )
f84e7607d   Marko Tikvić   added dictionary;...
7
8
9
10
11
12
13
14
15
  type Dictionary struct {
  	locales map[string]map[string]string
  }
  
  func NewDictionary() Dictionary {
  	return Dictionary{
  		locales: map[string]map[string]string{},
  	}
  }
9933169c8   Marko Tikvić   localization support
16

f84e7607d   Marko Tikvić   added dictionary;...
17
  func (d *Dictionary) AddLocale(loc, filePath string) error {
9933169c8   Marko Tikvić   localization support
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  	file, err := ioutil.ReadFile(filePath)
  	if err != nil {
  		return err
  	}
  
  	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)
  	}
f84e7607d   Marko Tikvić   added dictionary;...
33
  	d.locales[loc] = l
9933169c8   Marko Tikvić   localization support
34
35
36
  
  	return nil
  }
f84e7607d   Marko Tikvić   added dictionary;...
37
38
39
40
41
42
43
44
45
46
47
  func (d *Dictionary) Translate(loc, key string) string {
  	return d.locales[loc][key]
  }
  
  func (d *Dictionary) SupportsLocale(loc string) bool {
  	for k, _ := range d.locales {
  		if k == loc {
  			return true
  		}
  	}
  	return false
9933169c8   Marko Tikvić   localization support
48
  }