This commit is contained in:
Liang Ding 2014-10-23 21:43:35 +08:00
parent 1331c3964e
commit 73d06c441d
7 changed files with 81 additions and 40 deletions

View File

@ -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 // 用户集
}

View File

@ -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": [],

View File

@ -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"
}

42
main.go
View File

@ -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")

View File

@ -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(&notification)

View File

@ -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"] = "<span class='start-build'>" + i18n.Get(r, "start-build").(string) + "</span>\n"
channelRet["output"] = "<span class='start-build'>" + i18n.Get(locale, "start-build").(string) + "</span>\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"] = "<span class='build-succ'>" + i18n.Get(r, "build-succ").(string) + "</span>\n"
channelRet["output"] = "<span class='build-succ'>" + i18n.Get(locale, "build-succ").(string) + "</span>\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"] = "<span class='build-failed'>" + i18n.Get(r, "build-failed").(string) + "</span>\n" + errOut
channelRet["output"] = "<span class='build-failed'>" + i18n.Get(locale, "build-failed").(string) + "</span>\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"] = "<span class='start-install'>" + i18n.Get(r, "start-install").(string) + "</span>\n"
channelRet["output"] = "<span class='start-install'>" + i18n.Get(locale, "start-install").(string) + "</span>\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"] = "<span class='install-failed'>" + i18n.Get(r, "install-failed").(string) + "</span>\n" + errOut
channelRet["output"] = "<span class='install-failed'>" + i18n.Get(locale, "install-failed").(string) + "</span>\n" + errOut
} else {
channelRet["output"] = "<span class='install-succ'>" + i18n.Get(r, "install-succ").(string) + "</span>\n"
channelRet["output"] = "<span class='install-succ'>" + i18n.Get(locale, "install-succ").(string) + "</span>\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"] = "<span class='start-get'>" + i18n.Get(r, "start-get").(string) + "</span>\n"
channelRet["output"] = "<span class='start-get'>" + i18n.Get(locale, "start-get").(string) + "</span>\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"] = "<span class='get-failed'>" + i18n.Get(r, "get-failed").(string) + "</span>\n" + string(buf)
channelRet["output"] = "<span class='get-failed'>" + i18n.Get(locale, "get-failed").(string) + "</span>\n" + string(buf)
} else {
glog.V(3).Infof("Session [%s] 's running [go get] [runningId=%d] has done", sid, runningId)
channelRet["output"] = "<span class='get-succ'>" + i18n.Get(r, "get-succ").(string) + "</span>\n"
channelRet["output"] = "<span class='get-succ'>" + i18n.Get(locale, "get-succ").(string) + "</span>\n"
}

View File

@ -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)