i18n
This commit is contained in:
parent
eb14eac34e
commit
ec477b5e7a
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"wide": "Wide"
|
||||
|
||||
}
|
5
main.go
5
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() {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Wide</title>
|
||||
<title>{{.i18n.wide}}</title>
|
||||
<link rel="stylesheet" href="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/codemirror.css">
|
||||
<link rel="stylesheet" href="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/addon/hint/show-hint.css">
|
||||
<link rel="stylesheet" href="{{.Wide.StaticServer}}/static/js/lib/codemirror-4.4/theme/lesser-dark.css">
|
||||
|
|
Loading…
Reference in New Issue