Commit 2b62f61cc84cc1e486f68f7e8d8f26a1fab71e2b

Authored by Marko Tikvić
1 parent 89f45d7aa8
Exists in master

minor changes

Showing 1 changed file with 16 additions and 9 deletions   Show diff stats
... ... @@ -2,6 +2,7 @@ package webutility
2 2  
3 3 import (
4 4 "encoding/json"
  5 + "fmt"
5 6 "io/ioutil"
6 7 "net/http"
7 8 "path"
... ... @@ -17,12 +18,10 @@ type Dictionary struct {
17 18 defaultLocale string
18 19 }
19 20  
20   -func NewDictionary(def string) *Dictionary {
21   - d := Dictionary{
  21 +func NewDictionary() *Dictionary {
  22 + return &Dictionary{
22 23 locales: map[string]map[string]string{},
23 24 }
24   - d.defaultLocale = def
25   - return &d
26 25 }
27 26  
28 27 func (d *Dictionary) AddTranslations(directory string) error {
... ... @@ -58,10 +57,8 @@ func (d *Dictionary) AddTranslations(directory string) error {
58 57 d.supported = append(d.supported, loc)
59 58 }
60 59  
61   - if d.defaultLocale == "" {
62   - if len(d.supported) > 0 {
63   - d.defaultLocale = d.supported[0]
64   - }
  60 + if d.defaultLocale == "" && len(d.supported) > 0 {
  61 + d.defaultLocale = d.supported[0]
65 62 }
66 63  
67 64 return nil
... ... @@ -88,7 +85,17 @@ func (d *Dictionary) Translate(loc, key string) string {
88 85 return d.locales[loc][key]
89 86 }
90 87  
91   -func (d *Dictionary) hasLocale(loc string) bool {
  88 +func (d *Dictionary) SetDefaultLocale(loc string) error {
  89 + if !d.contains(loc) {
  90 + return fmt.Errorf("locale file not loaded: %s", loc)
  91 + }
  92 +
  93 + d.defaultLocale = loc
  94 +
  95 + return nil
  96 +}
  97 +
  98 +func (d *Dictionary) contains(loc string) bool {
92 99 for _, v := range d.supported {
93 100 if v == loc {
94 101 return true
... ...