Blame view

locale_utility.go 522 Bytes
9933169c8   Marko Tikvić   localization support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  package webutility
  
  import (
  	"encoding/json"
  	"io/ioutil"
  )
  
  var locales = map[string]map[string]string{}
  
  func LoadLocale(loc, filePath string) error {
  	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)
  	}
  	locales[loc] = l
  
  	return nil
  }
  
  func Translate(loc, key string) string {
  	return locales[loc][key]
  }