diff --git a/conf/wide.go b/conf/wide.go
index 1983d54..9c735b5 100644
--- a/conf/wide.go
+++ b/conf/wide.go
@@ -35,6 +35,7 @@ type User struct {
Name string
Password string
Workspace string // 该用户的工作空间 GOPATH 路径
+ Locale string
LatestSessionContent *LatestSessionContent
}
@@ -52,6 +53,7 @@ type conf struct {
RuntimeMode string // 运行模式
Pwd string // 工作目录
Workspace string // 主工作空间 GOPATH 路径
+ Locale string // 默认的区域
Users []*User // 用户集
}
diff --git a/conf/wide.json b/conf/wide.json
index bc97fbb..ee59936 100644
--- a/conf/wide.json
+++ b/conf/wide.json
@@ -11,11 +11,13 @@
"RuntimeMode": "dev",
"Pwd": "{pwd}",
"Workspace": "{pwd}/data/workspace",
+ "Locale": "zh_CN",
"Users": [
{
"Name": "admin",
"Password": "admin",
"Workspace": "{pwd}/data/user_workspaces/admin",
+ "Locale": "zh_CN",
"LatestSessionContent": {
"FileTree": [],
"Files": [],
diff --git a/i18n/locales.go b/i18n/locales.go
index af41a89..c80b33e 100644
--- a/i18n/locales.go
+++ b/i18n/locales.go
@@ -4,7 +4,6 @@ package i18n
import (
"encoding/json"
"io/ioutil"
- "net/http"
"os"
"github.com/golang/glog"
@@ -22,39 +21,38 @@ var Locales = map[string]locale{}
// 加载国际化配置.
func Load() {
// TODO: 加载所有语言配置
- bytes, _ := ioutil.ReadFile("i18n/zh_CN.json")
+ load("zh_CN")
- zhCN := locale{Name: "zh_CN"}
+}
- // TODO: 时区
-
- err := json.Unmarshal(bytes, &zhCN.Langs)
- if err != nil {
+func load(localeStr string) {
+ bytes, err := ioutil.ReadFile("i18n/" + localeStr + ".json")
+ if nil != err {
glog.Error(err)
os.Exit(-1)
}
- Locales["zh_CN"] = zhCN
- glog.V(5).Info("Loaded [zh_CN] locale configuration")
+ l := locale{Name: localeStr}
+
+ err = json.Unmarshal(bytes, &l.Langs)
+ if nil != err {
+ glog.Error(err)
+
+ os.Exit(-1)
+ }
+
+ Locales[localeStr] = l
+
+ glog.V(5).Infof("Loaded [%s] locale configuration", localeStr)
}
-// 获取请求对应的本地语言配置项.
-func Get(r *http.Request, key string) interface{} {
- locale := GetLocale(r)
-
+// 获取语言配置项.
+func Get(locale, key string) interface{} {
return Locales[locale].Langs[key]
}
-// 获取请求对应的本地语言配置.
-func GetAll(r *http.Request) map[string]interface{} {
- locale := GetLocale(r)
-
+// 获取语言配置.
+func GetAll(locale string) map[string]interface{} {
return Locales[locale].Langs
}
-
-// 获取请求对应的 locale.
-func GetLocale(r *http.Request) string {
- // TODO: 从请求中获取 locale
- return "zh_CN"
-}
diff --git a/main.go b/main.go
index c0e9ade..653aa79 100644
--- a/main.go
+++ b/main.go
@@ -57,7 +57,8 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
if "GET" == r.Method {
// 展示登录页面
- model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r), "ver": Ver}
+ model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(conf.Wide.Locale),
+ "locale": conf.Wide.Locale, "ver": Ver}
t, err := template.ParseFiles("view/login.html")
@@ -141,11 +142,12 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
wideSession := session.WideSessions.New(httpSession)
username := httpSession.Values["username"].(string)
+ locale := conf.Wide.GetUser(username).Locale
wideSessions := session.WideSessions.GetByUsername(username)
userConf := conf.Wide.GetUser(username)
- model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r),
+ model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
"session": wideSession, "latestSessionContent": userConf.LatestSessionContent,
"pathSeparator": conf.PathSeparator}
@@ -186,9 +188,10 @@ func startHandler(w http.ResponseWriter, r *http.Request) {
httpSession.Save(r, w)
username := httpSession.Values["username"].(string)
+ locale := conf.Wide.GetUser(username).Locale
userWorkspace := conf.Wide.GetUserWorkspace(username)
- model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r),
+ model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
"username": username, "workspace": userWorkspace, "ver": Ver}
t, err := template.ParseFiles("view/start.html")
@@ -207,7 +210,21 @@ func startHandler(w http.ResponseWriter, r *http.Request) {
func keyboardShortcutsHandler(w http.ResponseWriter, r *http.Request) {
i18n.Load()
- model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r)}
+ httpSession, _ := session.HTTPSession.Get(r, "wide-session")
+
+ if httpSession.IsNew {
+ http.Redirect(w, r, "/login", http.StatusFound)
+
+ return
+ }
+
+ httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
+ httpSession.Save(r, w)
+
+ username := httpSession.Values["username"].(string)
+ locale := conf.Wide.GetUser(username).Locale
+
+ model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale}
t, err := template.ParseFiles("view/keyboard_shortcuts.html")
@@ -225,8 +242,21 @@ func keyboardShortcutsHandler(w http.ResponseWriter, r *http.Request) {
func aboutHandler(w http.ResponseWriter, r *http.Request) {
i18n.Load()
- model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r),
- "ver": Ver}
+ httpSession, _ := session.HTTPSession.Get(r, "wide-session")
+
+ if httpSession.IsNew {
+ http.Redirect(w, r, "/login", http.StatusFound)
+
+ return
+ }
+
+ httpSession.Options.MaxAge = conf.Wide.HTTPSessionMaxAge
+ httpSession.Save(r, w)
+
+ username := httpSession.Values["username"].(string)
+ locale := conf.Wide.GetUser(username).Locale
+
+ model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale, "ver": Ver}
t, err := template.ParseFiles("view/about.html")
diff --git a/notification/notifications.go b/notification/notifications.go
index 1be29c2..d2d3690 100644
--- a/notification/notifications.go
+++ b/notification/notifications.go
@@ -7,6 +7,7 @@ import (
"time"
"strconv"
+ "github.com/b3log/wide/conf"
"github.com/b3log/wide/event"
"github.com/b3log/wide/i18n"
"github.com/b3log/wide/session"
@@ -53,8 +54,12 @@ func event2Notification(e *event.Event) {
return
}
+ httpSession, _ := session.HTTPSession.Get(wsChannel.Request, "wide-session")
+ username := httpSession.Values["username"].(string)
+ locale := conf.Wide.GetUser(username).Locale
+
// 消息国际化处理
- notification.Message = i18n.Get(wsChannel.Request, "notification_"+strconv.Itoa(e.Code)).(string)
+ notification.Message = i18n.Get(locale, "notification_"+strconv.Itoa(e.Code)).(string)
wsChannel.Conn.WriteJSON(¬ification)
diff --git a/output/outputs.go b/output/outputs.go
index a260814..8017307 100644
--- a/output/outputs.go
+++ b/output/outputs.go
@@ -186,6 +186,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
username := httpSession.Values["username"].(string)
+ locale := conf.Wide.GetUser(username).Locale
decoder := json.NewDecoder(r.Body)
@@ -273,7 +274,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
if nil != session.OutputWS[sid] {
// 在前端 output 中显示“开始构建”
- channelRet["output"] = "" + i18n.Get(r, "start-build").(string) + "\n"
+ channelRet["output"] = "" + i18n.Get(locale, "start-build").(string) + "\n"
channelRet["cmd"] = "start-build"
wsChannel := session.OutputWS[sid]
@@ -313,7 +314,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
if 0 == len(buf) { // 说明构建成功,没有错误信息输出
// 设置下一次执行命令(前端会根据该参数发送请求)
channelRet["nextCmd"] = args["nextCmd"]
- channelRet["output"] = "" + i18n.Get(r, "build-succ").(string) + "\n"
+ channelRet["output"] = "" + i18n.Get(locale, "build-succ").(string) + "\n"
go func() { // 运行 go install,生成的库用于 gocode lib-path
cmd := exec.Command("go", "install")
@@ -329,7 +330,7 @@ func BuildHandler(w http.ResponseWriter, r *http.Request) {
} else { // 构建失败
// 解析错误信息,返回给编辑器 gutter lint
errOut := string(buf)
- channelRet["output"] = "" + i18n.Get(r, "build-failed").(string) + "\n" + errOut
+ channelRet["output"] = "" + i18n.Get(locale, "build-failed").(string) + "\n" + errOut
lines := strings.Split(errOut, "\n")
@@ -396,6 +397,7 @@ func GoInstallHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
username := httpSession.Values["username"].(string)
+ locale := conf.Wide.GetUser(username).Locale
decoder := json.NewDecoder(r.Body)
@@ -465,7 +467,7 @@ func GoInstallHandler(w http.ResponseWriter, r *http.Request) {
if nil != session.OutputWS[sid] {
// 在前端 output 中显示“开始 go install”
- channelRet["output"] = "" + i18n.Get(r, "start-install").(string) + "\n"
+ channelRet["output"] = "" + i18n.Get(locale, "start-install").(string) + "\n"
channelRet["cmd"] = "start-install"
wsChannel := session.OutputWS[sid]
@@ -545,9 +547,9 @@ func GoInstallHandler(w http.ResponseWriter, r *http.Request) {
channelRet["lints"] = lints
- channelRet["output"] = "" + i18n.Get(r, "install-failed").(string) + "\n" + errOut
+ channelRet["output"] = "" + i18n.Get(locale, "install-failed").(string) + "\n" + errOut
} else {
- channelRet["output"] = "" + i18n.Get(r, "install-succ").(string) + "\n"
+ channelRet["output"] = "" + i18n.Get(locale, "install-succ").(string) + "\n"
}
if nil != session.OutputWS[sid] {
@@ -573,6 +575,7 @@ func GoGetHandler(w http.ResponseWriter, r *http.Request) {
httpSession, _ := session.HTTPSession.Get(r, "wide-session")
username := httpSession.Values["username"].(string)
+ locale := conf.Wide.GetUser(username).Locale
decoder := json.NewDecoder(r.Body)
@@ -620,7 +623,7 @@ func GoGetHandler(w http.ResponseWriter, r *http.Request) {
if nil != session.OutputWS[sid] {
// 在前端 output 中显示“开始 go get
- channelRet["output"] = "" + i18n.Get(r, "start-get").(string) + "\n"
+ channelRet["output"] = "" + i18n.Get(locale, "start-get").(string) + "\n"
channelRet["cmd"] = "start-get"
wsChannel := session.OutputWS[sid]
@@ -659,11 +662,11 @@ func GoGetHandler(w http.ResponseWriter, r *http.Request) {
if 0 != len(buf) {
glog.V(3).Infof("Session [%s] 's running [go get] [runningId=%d] has done (with error)", sid, runningId)
- channelRet["output"] = "" + i18n.Get(r, "get-failed").(string) + "\n" + string(buf)
+ channelRet["output"] = "" + i18n.Get(locale, "get-failed").(string) + "\n" + string(buf)
} else {
glog.V(3).Infof("Session [%s] 's running [go get] [runningId=%d] has done", sid, runningId)
- channelRet["output"] = "" + i18n.Get(r, "get-succ").(string) + "\n"
+ channelRet["output"] = "" + i18n.Get(locale, "get-succ").(string) + "\n"
}
diff --git a/shell/shells.go b/shell/shells.go
index 07f3644..9d8dbd3 100644
--- a/shell/shells.go
+++ b/shell/shells.go
@@ -42,8 +42,9 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
wideSession := session.WideSessions.New(httpSession)
username := httpSession.Values["username"].(string)
+ locale := conf.Wide.GetUser(username).Locale
- model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(r), "locale": i18n.GetLocale(r),
+ model := map[string]interface{}{"conf": conf.Wide, "i18n": i18n.GetAll(locale), "locale": locale,
"session": wideSession}
wideSessions := session.WideSessions.GetByUsername(username)