Commit 0207726c47afcb7f65aff0e2c145a33cd3deb27f

Authored by Marko Tikvić
1 parent 5d654d249a
Exists in master

improved localization interfaces

Showing 1 changed file with 46 additions and 26 deletions   Show diff stats
... ... @@ -3,6 +3,7 @@ package webutility
3 3 import (
4 4 "encoding/json"
5 5 "errors"
  6 + "fmt"
6 7 "io/ioutil"
7 8 "strings"
8 9 "sync"
... ... @@ -21,36 +22,57 @@ func NewDictionary() *Dictionary {
21 22 }
22 23 }
23 24  
24   -func (d *Dictionary) AddLocale(loc, filePath string) error {
25   - file, err := ioutil.ReadFile(filePath)
  25 +func (d *Dictionary) AddTranslations(directory string) error {
  26 + files, err := ioutil.ReadDir(directory)
26 27 if err != nil {
27 28 return err
28 29 }
29 30  
30   - var data interface{}
31   - err = json.Unmarshal(file, &data)
32   - if err != nil {
33   - return err
34   - }
  31 + for _, fileInfo := range files {
  32 + fName := fileInfo.Name()
  33 + path := directory + fName
  34 + file, err := ioutil.ReadFile(path)
  35 + if err != nil {
  36 + return err
  37 + }
35 38  
36   - l := map[string]string{}
37   - for k, v := range data.(map[string]interface{}) {
38   - l[k] = v.(string)
39   - }
  39 + fmt.Println(fName, path)
  40 +
  41 + break
  42 +
  43 + loc := stripFileExtension(fName)
40 44  
41   - mu.Lock()
42   - defer mu.Unlock()
43   - d.locales[loc] = l
44   - d.supported = append(d.supported, loc)
  45 + var data interface{}
  46 + err = json.Unmarshal(file, &data)
  47 + if err != nil {
  48 + return err
  49 + }
  50 +
  51 + l := map[string]string{}
  52 + for k, v := range data.(map[string]interface{}) {
  53 + l[k] = v.(string)
  54 + }
  55 +
  56 + mu.Lock()
  57 + defer mu.Unlock()
  58 + d.locales[loc] = l
  59 + d.supported = append(d.supported, loc)
  60 + }
45 61  
46 62 return nil
47 63 }
48 64  
  65 +func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) {
  66 + accepted := d.parseAcceptedLanguageHeader(acceptedLanguages)
  67 + best = accepted[0]
  68 + return
  69 +}
  70 +
49 71 func (d *Dictionary) Translate(loc, key string) string {
50 72 return d.locales[loc][key]
51 73 }
52 74  
53   -func (d *Dictionary) HasLocale(loc string) bool {
  75 +func (d *Dictionary) hasLocale(loc string) bool {
54 76 for _, v := range d.supported {
55 77 if v == loc {
56 78 return true
... ... @@ -59,27 +81,21 @@ func (d *Dictionary) HasLocale(loc string) bool {
59 81 return false
60 82 }
61 83  
62   -func (d *Dictionary) SetDefaultLocale(loc string) error {
63   - if !d.HasLocale(loc) {
  84 +func (d *Dictionary) setDefaultLocale(loc string) error {
  85 + if !d.hasLocale(loc) {
64 86 return errors.New("dictionary does not contain translations for " + loc)
65 87 }
66 88 d.defaultLocale = loc
67 89 return nil
68 90 }
69 91  
70   -func (d *Dictionary) GetDefaultLocale() string {
  92 +func (d *Dictionary) getDefaultLocale() string {
71 93 return d.defaultLocale
72 94 }
73 95  
74   -func (d *Dictionary) GetBestMatchLocale(acceptedLanguages string) (best string) {
75   - accepted := d.parseAcceptedLanguageHeader(acceptedLanguages)
76   - best = accepted[0]
77   - return
78   -}
79   -
80 96 func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (langs []string) {
81 97 if acceptedLanguages == "" {
82   - langs = append(langs, d.GetDefaultLocale())
  98 + langs = append(langs, d.getDefaultLocale())
83 99 return
84 100 }
85 101  
... ... @@ -90,3 +106,7 @@ func (d *Dictionary) parseAcceptedLanguageHeader(acceptedLanguages string) (lang
90 106  
91 107 return
92 108 }
  109 +
  110 +func stripFileExtension(full string) (stripped string) {
  111 + return
  112 +}
... ...