1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 14:10:54 +00:00

middleware: support i18n

This commit is contained in:
astaxie 2014-06-27 17:53:53 +08:00
parent 0f170a80da
commit 62e9c89010

View File

@ -10,85 +10,51 @@
package middleware package middleware
//import ( import (
// "github.com/astaxie/beego/config" "encoding/json"
// "os" "io/ioutil"
// "path" "os"
//) )
//type Translation struct { type Translation struct {
// filetype string filepath string
// CurrentLocal string CurrentLocal string
// Locales map[string]map[string]string Locales map[string]map[string]string
//} }
//func NewLocale(filetype string) *Translation { func NewLocale(filepath string, defaultlocal string) *Translation {
// return &Translation{ i18n := make(map[string]map[string]string)
// filetype: filetype, file, err := os.Open(filepath)
// CurrentLocal: "zh", if err != nil {
// Locales: make(map[string]map[string]string), panic("open " + filepath + " err :" + err.Error())
// } }
//} data, err := ioutil.ReadAll(file)
if err != nil {
panic("read " + filepath + " err :" + err.Error())
}
err = json.Unmarshal(data, &i18n)
if err != nil {
panic("json.Unmarshal " + filepath + " err :" + err.Error())
}
return &Translation{
filepath: filepath,
CurrentLocal: defaultlocal,
Locales: i18n,
}
}
//func (t *Translation) loadTranslations(dirPath string) error { func (t *Translation) SetLocale(local string) {
// dir, err := os.Open(dirPath) t.CurrentLocal = local
// if err != nil { }
// return err
// }
// defer dir.Close()
// names, err := dir.Readdirnames(-1) func (t *Translation) Translate(key string, local string) string {
// if err != nil { if local == "" {
// return err local = t.CurrentLocal
// } }
if ct, ok := t.Locales[key]; ok {
// for _, name := range names { if v, o := ct[local]; o {
// fullPath := path.Join(dirPath, name) return v
}
// fi, err := os.Stat(fullPath) }
// if err != nil { return key
// return err }
// }
// if fi.IsDir() {
// continue
// } else {
// if err := t.loadTranslation(fullPath, name); err != nil {
// return err
// }
// }
// }
// return nil
//}
//func (t *Translation) loadTranslation(fullPath, locale string) error {
// sourceKey2Trans, ok := t.Locales[locale]
// if !ok {
// sourceKey2Trans = make(map[string]string)
// t.Locales[locale] = sourceKey2Trans
// }
// for _, m := range trf.Messages {
// if m.Translation != "" {
// sourceKey2Trans[sourceKey(m.Source, m.Context)] = m.Translation
// }
// }
// return nil
//}
//func (t *Translation) SetLocale(local string) {
// t.CurrentLocal = local
//}
//func (t *Translation) Translate(key string) string {
// if ct, ok := t.Locales[t.CurrentLocal]; ok {
// if v, o := ct[key]; o {
// return v
// }
// }
// return key
//}