Update BaseController.go

i18n, get lang set from url param
This commit is contained in:
Ben Stone 2021-03-29 17:56:36 +08:00 committed by GitHub
parent 7ab07f2ce2
commit 0694b4554b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,20 +2,20 @@ package controllers
import ( import (
"bytes" "bytes"
"github.com/beego/i18n"
"encoding/json" "encoding/json"
"io" "io"
"strings" "strings"
"time" "time"
"github.com/astaxie/beego"
"github.com/lifei6671/mindoc/conf"
"github.com/lifei6671/mindoc/models"
"github.com/lifei6671/mindoc/utils"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
"path/filepath" "path/filepath"
"github.com/astaxie/beego"
"github.com/mindoc-org/mindoc/conf"
"github.com/mindoc-org/mindoc/models"
"github.com/mindoc-org/mindoc/utils"
) )
type BaseController struct { type BaseController struct {
@ -78,10 +78,17 @@ func (c *BaseController) Prepare() {
if b, err := ioutil.ReadFile(filepath.Join(beego.BConfig.WebConfig.ViewsPath, "widgets", "scripts.tpl")); err == nil { if b, err := ioutil.ReadFile(filepath.Join(beego.BConfig.WebConfig.ViewsPath, "widgets", "scripts.tpl")); err == nil {
c.Data["Scripts"] = template.HTML(string(b)) c.Data["Scripts"] = template.HTML(string(b))
} }
lang := c.Input().Get("lang")
if len(lang) == 0 ||
!i18n.IsExist(lang) {
lang = "zh-cn"
}
c.Data["Lang"] = lang
} }
//判断用户是否登录. //判断用户是否登录.
func (c *BaseController)isUserLoggedIn() bool { func (c *BaseController) isUserLoggedIn() bool {
return c.Member != nil && c.Member.MemberId > 0 return c.Member != nil && c.Member.MemberId > 0
} }
@ -123,7 +130,7 @@ func (c *BaseController) JsonResult(errCode int, errMsg string, data ...interfac
} }
//如果错误不为空,则响应错误信息到浏览器. //如果错误不为空,则响应错误信息到浏览器.
func (c *BaseController) CheckJsonError(code int,err error) { func (c *BaseController) CheckJsonError(code int, err error) {
if err == nil { if err == nil {
return return
@ -195,7 +202,7 @@ func (c *BaseController) ShowErrorPage(errCode int, errMsg string) {
} }
func (c *BaseController) CheckErrorResult(code int,err error) { func (c *BaseController) CheckErrorResult(code int, err error) {
if err != nil { if err != nil {
c.ShowErrorPage(code, err.Error()) c.ShowErrorPage(code, err.Error())
} }