From ec477b5e7a3b897eabc28695cb26bcb9e6069568 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Mon, 25 Aug 2014 22:17:02 +0800 Subject: [PATCH] i18n --- conf/wide.go | 6 +---- i18n/locales.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ i18n/zh_CN.json | 4 ++++ main.go | 5 ++++- templates/index.html | 2 +- 5 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 i18n/locales.go create mode 100644 i18n/zh_CN.json diff --git a/conf/wide.go b/conf/wide.go index 3efe79d..908a6c6 100644 --- a/conf/wide.go +++ b/conf/wide.go @@ -2,7 +2,7 @@ package conf import ( "encoding/json" - "flag" + _ "github.com/b3log/wide/i18n" "github.com/b3log/wide/util" "github.com/golang/glog" "io/ioutil" @@ -28,10 +28,6 @@ type conf struct { var Wide conf func init() { - flag.Set("logtostderr", "true") - - flag.Parse() - bytes, _ := ioutil.ReadFile("conf/wide.json") err := json.Unmarshal(bytes, &Wide) diff --git a/i18n/locales.go b/i18n/locales.go new file mode 100644 index 0000000..e29f845 --- /dev/null +++ b/i18n/locales.go @@ -0,0 +1,53 @@ +package i18n + +import ( + "encoding/json" + "flag" + "github.com/golang/glog" + "io/ioutil" + "net/http" + "os" +) + +type locale struct { + Name string + Langs map[string]interface{} + TimeZone string +} + +// 所有的 locales. +var Locales = map[string]locale{} + +func init() { + flag.Set("logtostderr", "true") + + flag.Parse() + + // TODO: 加载所有语言配置 + bytes, _ := ioutil.ReadFile("i18n/zh_CN.json") + + zhCN := locale{Name: "zh_CN"} + + // TODO: 时区 + + err := json.Unmarshal(bytes, &zhCN.Langs) + if err != nil { + glog.Error(err) + + os.Exit(-1) + } + + Locales["zh_CN"] = zhCN + glog.Info("Loaded [zh_CN] locale configuration") +} + +func GetLangs(r *http.Request) map[string]interface{} { + locale := getLocale(r) + + return Locales[locale].Langs +} + +func getLocale(r *http.Request) string { + // TODO: 从请求中获取 locale + return "zh_CN" +} diff --git a/i18n/zh_CN.json b/i18n/zh_CN.json new file mode 100644 index 0000000..878f295 --- /dev/null +++ b/i18n/zh_CN.json @@ -0,0 +1,4 @@ +{ + "wide": "Wide" + +} \ No newline at end of file diff --git a/main.go b/main.go index 7c47b86..428fe20 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "github.com/b3log/wide/conf" "github.com/b3log/wide/editor" "github.com/b3log/wide/file" + "github.com/b3log/wide/i18n" "github.com/b3log/wide/output" "github.com/b3log/wide/session" "github.com/b3log/wide/shell" @@ -15,6 +16,8 @@ import ( ) func indexHandler(w http.ResponseWriter, r *http.Request) { + model := map[string]interface{}{"Wide": conf.Wide, "i18n": i18n.GetLangs(r)} + session, _ := session.Store.Get(r, "wide-session") if session.IsNew { @@ -32,7 +35,7 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { return } - t.Execute(w, map[string]interface{}{"Wide": conf.Wide}) + t.Execute(w, model) } func main() { diff --git a/templates/index.html b/templates/index.html index d8094c4..3bd3ebe 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,7 +2,7 @@ - Wide + {{.i18n.wide}}