62e9c890 by astaxie

middleware: support i18n

1 parent 0f170a80
...@@ -10,85 +10,51 @@ ...@@ -10,85 +10,51 @@
10 10
11 package middleware 11 package middleware
12 12
13 //import ( 13 import (
14 // "github.com/astaxie/beego/config" 14 "encoding/json"
15 // "os" 15 "io/ioutil"
16 // "path" 16 "os"
17 //) 17 )
18 18
19 //type Translation struct { 19 type Translation struct {
20 // filetype string 20 filepath string
21 // CurrentLocal string 21 CurrentLocal string
22 // Locales map[string]map[string]string 22 Locales map[string]map[string]string
23 //} 23 }
24 24
25 //func NewLocale(filetype string) *Translation { 25 func NewLocale(filepath string, defaultlocal string) *Translation {
26 // return &Translation{ 26 i18n := make(map[string]map[string]string)
27 // filetype: filetype, 27 file, err := os.Open(filepath)
28 // CurrentLocal: "zh", 28 if err != nil {
29 // Locales: make(map[string]map[string]string), 29 panic("open " + filepath + " err :" + err.Error())
30 // } 30 }
31 //} 31 data, err := ioutil.ReadAll(file)
32 32 if err != nil {
33 //func (t *Translation) loadTranslations(dirPath string) error { 33 panic("read " + filepath + " err :" + err.Error())
34 // dir, err := os.Open(dirPath) 34 }
35 // if err != nil { 35 err = json.Unmarshal(data, &i18n)
36 // return err 36 if err != nil {
37 // } 37 panic("json.Unmarshal " + filepath + " err :" + err.Error())
38 // defer dir.Close() 38 }
39 39 return &Translation{
40 // names, err := dir.Readdirnames(-1) 40 filepath: filepath,
41 // if err != nil { 41 CurrentLocal: defaultlocal,
42 // return err 42 Locales: i18n,
43 // } 43 }
44 44 }
45 // for _, name := range names { 45
46 // fullPath := path.Join(dirPath, name) 46 func (t *Translation) SetLocale(local string) {
47 47 t.CurrentLocal = local
48 // fi, err := os.Stat(fullPath) 48 }
49 // if err != nil { 49
50 // return err 50 func (t *Translation) Translate(key string, local string) string {
51 // } 51 if local == "" {
52 52 local = t.CurrentLocal
53 // if fi.IsDir() { 53 }
54 // continue 54 if ct, ok := t.Locales[key]; ok {
55 // } else { 55 if v, o := ct[local]; o {
56 // if err := t.loadTranslation(fullPath, name); err != nil { 56 return v
57 // return err 57 }
58 // } 58 }
59 // } 59 return key
60 // } 60 }
61
62 // return nil
63 //}
64
65 //func (t *Translation) loadTranslation(fullPath, locale string) error {
66
67 // sourceKey2Trans, ok := t.Locales[locale]
68 // if !ok {
69 // sourceKey2Trans = make(map[string]string)
70
71 // t.Locales[locale] = sourceKey2Trans
72 // }
73
74 // for _, m := range trf.Messages {
75 // if m.Translation != "" {
76 // sourceKey2Trans[sourceKey(m.Source, m.Context)] = m.Translation
77 // }
78 // }
79
80 // return nil
81 //}
82
83 //func (t *Translation) SetLocale(local string) {
84 // t.CurrentLocal = local
85 //}
86
87 //func (t *Translation) Translate(key string) string {
88 // if ct, ok := t.Locales[t.CurrentLocal]; ok {
89 // if v, o := ct[key]; o {
90 // return v
91 // }
92 // }
93 // return key
94 //}
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!