Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type WebFrameWork interface {
GetConnection() db.Connection
SetContext(ctx interface{}) WebFrameWork
GetCookie() (string, error)
Lang() string
Path() string
Method() string
FormParam() url.Values
Expand Down Expand Up @@ -167,7 +168,7 @@ func (base *BaseAdapter) GetContent(ctx interface{}, getPanelFn types.GetPanelFn
buf := new(bytes.Buffer)
hasError = tmpl.ExecuteTemplate(buf, tmplName, types.NewPage(&types.NewPageParam{
User: user,
Menu: menu.GetGlobalMenu(user, wf.GetConnection()).SetActiveClass(config.URLRemovePrefix(newBase.Path())),
Menu: menu.GetGlobalMenu(user, wf.GetConnection(), newBase.Lang()).SetActiveClass(config.URLRemovePrefix(newBase.Path())),
Panel: panel.GetContent(config.IsProductionEnvironment()),
Assets: template.GetComponentAssetImportHTML(),
Buttons: navButtons.CheckPermission(user),
Expand Down
5 changes: 5 additions & 0 deletions adapter/beego/beego.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func (bee *Beego) GetCookie() (string, error) {
return bee.ctx.GetCookie(bee.CookieKey()), nil
}

// Lang implements the method Adapter.Lang.
func (bee *Beego) Lang() string {
return bee.ctx.Request.URL.Query().Get("__ga_lang")
}

// Path implements the method Adapter.Path.
func (bee *Beego) Path() string {
return bee.ctx.Request.URL.Path
Expand Down
5 changes: 5 additions & 0 deletions adapter/buffalo/buffalo.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ func (bu *Buffalo) GetCookie() (string, error) {
return bu.ctx.Cookies().Get(bu.CookieKey())
}

// Lang implements the method Adapter.Lang.
func (bu *Buffalo) Lang() string {
return bu.ctx.Request().URL.Query().Get("__ga_lang")
}

// Path implements the method Adapter.Path.
func (bu *Buffalo) Path() string {
return bu.ctx.Request().URL.Path
Expand Down
5 changes: 5 additions & 0 deletions adapter/chi/chi.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ func (ch *Chi) GetCookie() (string, error) {
return cookie.Value, err
}

// Lang implements the method Adapter.Lang.
func (ch *Chi) Lang() string {
return ch.ctx.Request.URL.Query().Get("__ga_lang")
}

// Path implements the method Adapter.Path.
func (ch *Chi) Path() string {
return ch.ctx.Request.URL.Path
Expand Down
5 changes: 5 additions & 0 deletions adapter/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ func (e *Echo) GetCookie() (string, error) {
return cookie.Value, err
}

// Lang implements the method Adapter.Lang.
func (e *Echo) Lang() string {
return e.ctx.Request().URL.Query().Get("__ga_lang")
}

// Path implements the method Adapter.Path.
func (e *Echo) Path() string {
return e.ctx.Request().URL.Path
Expand Down
5 changes: 5 additions & 0 deletions adapter/fasthttp/fasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ func (fast *Fasthttp) GetCookie() (string, error) {
return string(fast.ctx.Request.Header.Cookie(fast.CookieKey())), nil
}

// Lang implements the method Adapter.Lang.
func (fast *Fasthttp) Lang() string {
return string(fast.ctx.Request.URI().QueryArgs().Peek("__ga_lang"))
}

// Path implements the method Adapter.Path.
func (fast *Fasthttp) Path() string {
return string(fast.ctx.Path())
Expand Down
5 changes: 5 additions & 0 deletions adapter/gf/gf.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ func (gf *Gf) GetCookie() (string, error) {
return gf.ctx.Cookie.Get(gf.CookieKey()), nil
}

// Lang implements the method Adapter.Lang.
func (gf *Gf) Lang() string {
return gf.ctx.Request.URL.Query().Get("__ga_lang")
}

// Path implements the method Adapter.Path.
func (gf *Gf) Path() string {
return gf.ctx.URL.Path
Expand Down
5 changes: 5 additions & 0 deletions adapter/gin/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ func (gins *Gin) GetCookie() (string, error) {
return gins.ctx.Cookie(gins.CookieKey())
}

// Lang implements the method Adapter.Lang.
func (gins *Gin) Lang() string {
return gins.ctx.Request.URL.Query().Get("__ga_lang")
}

// Path implements the method Adapter.Path.
func (gins *Gin) Path() string {
return gins.ctx.Request.URL.Path
Expand Down
5 changes: 5 additions & 0 deletions adapter/gorilla/gorilla.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ func (g *Gorilla) GetCookie() (string, error) {
return cookie.Value, err
}

// Lang implements the method Adapter.Lang.
func (g *Gorilla) Lang() string {
return g.ctx.Request.URL.Query().Get("__ga_lang")
}

// Path implements the method Adapter.Path.
func (g *Gorilla) Path() string {
return g.ctx.Request.RequestURI
Expand Down
5 changes: 5 additions & 0 deletions adapter/iris/iris.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ func (is *Iris) GetCookie() (string, error) {
return is.ctx.GetCookie(is.CookieKey()), nil
}

// Lang implements the method Adapter.Lang.
func (is *Iris) Lang() string {
return is.ctx.Request().URL.Query().Get("__ga_lang")
}

// Path implements the method Adapter.Path.
func (is *Iris) Path() string {
return is.ctx.Path()
Expand Down
10 changes: 10 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ func (ctx *Context) Query(key string) string {
return ctx.Request.URL.Query().Get(key)
}

// QueryAll get the query parameters of url.
func (ctx *Context) QueryAll(key string) []string {
return ctx.Request.URL.Query()[key]
}

// QueryDefault get the query parameter of url. If it is empty, return the default.
func (ctx *Context) QueryDefault(key, def string) string {
value := ctx.Query(key)
Expand All @@ -344,6 +349,11 @@ func (ctx *Context) QueryDefault(key, def string) string {
return value
}

// Lang get the query parameter of url with given key __ga_lang.
func (ctx *Context) Lang() string {
return ctx.Query("__ga_lang")
}

// Headers get the value of request headers key.
func (ctx *Context) Headers(key string) string {
return ctx.Request.Header.Get(key)
Expand Down
14 changes: 10 additions & 4 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ func (eng *Engine) AddNavButtons(title template2.HTML, icon string, action types
return eng
}

// AddNavButtonsRaw add the nav buttons.
func (eng *Engine) AddNavButtonsRaw(btns ...types.Button) *Engine {
*eng.NavButtons = append(*eng.NavButtons, btns...)
return eng
}

type navJumpButtonParam struct {
Exist bool
Icon string
Expand Down Expand Up @@ -512,7 +518,7 @@ func (eng *Engine) HTML(method, url string, fn types.GetPanelInfoFn, noAuth ...b

hasError := tmpl.ExecuteTemplate(buf, tmplName, types.NewPage(&types.NewPageParam{
User: user,
Menu: menu.GetGlobalMenu(user, eng.Adapter.GetConnection()).SetActiveClass(config.URLRemovePrefix(ctx.Path())),
Menu: menu.GetGlobalMenu(user, eng.Adapter.GetConnection(), ctx.Lang()).SetActiveClass(config.URLRemovePrefix(ctx.Path())),
Panel: panel.GetContent(eng.config.IsProductionEnvironment()),
Assets: template.GetComponentAssetImportHTML(),
Buttons: eng.NavButtons.CheckPermission(user),
Expand Down Expand Up @@ -560,7 +566,7 @@ func (eng *Engine) HTMLFile(method, url, path string, data map[string]interface{

hasError := tmpl.ExecuteTemplate(buf, tmplName, types.NewPage(&types.NewPageParam{
User: user,
Menu: menu.GetGlobalMenu(user, eng.Adapter.GetConnection()).SetActiveClass(eng.config.URLRemovePrefix(ctx.Path())),
Menu: menu.GetGlobalMenu(user, eng.Adapter.GetConnection(), ctx.Lang()).SetActiveClass(eng.config.URLRemovePrefix(ctx.Path())),
Panel: types.Panel{
Content: template.HTML(cbuf.String()),
},
Expand Down Expand Up @@ -621,7 +627,7 @@ func (eng *Engine) htmlFilesHandler(data map[string]interface{}, files ...string

hasError := tmpl.ExecuteTemplate(buf, tmplName, types.NewPage(&types.NewPageParam{
User: user,
Menu: menu.GetGlobalMenu(user, eng.Adapter.GetConnection()).SetActiveClass(eng.config.URLRemovePrefix(ctx.Path())),
Menu: menu.GetGlobalMenu(user, eng.Adapter.GetConnection(), ctx.Lang()).SetActiveClass(eng.config.URLRemovePrefix(ctx.Path())),
Panel: types.Panel{
Content: template.HTML(cbuf.String()),
},
Expand All @@ -647,7 +653,7 @@ func (eng *Engine) errorPanelHTML(ctx *context.Context, buf *bytes.Buffer, err e

hasError := tmpl.ExecuteTemplate(buf, tmplName, types.NewPage(&types.NewPageParam{
User: user,
Menu: menu.GetGlobalMenu(user, eng.Adapter.GetConnection()).SetActiveClass(eng.config.URLRemovePrefix(ctx.Path())),
Menu: menu.GetGlobalMenu(user, eng.Adapter.GetConnection(), ctx.Lang()).SetActiveClass(eng.config.URLRemovePrefix(ctx.Path())),
Panel: template.WarningPanel(err.Error()).GetContent(eng.config.IsProductionEnvironment()),
Assets: template.GetComponentAssetImportHTML(),
Buttons: (*eng.NavButtons).CheckPermission(user),
Expand Down
22 changes: 21 additions & 1 deletion modules/language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,20 @@ func GetWithScope(value string, scopes ...string) string {
return value
}

if locale, ok := Lang[config.GetLanguage()][JoinScopes(scopes)+strings.ToLower(value)]; ok {
return GetWithScopeAndLanguageSet(value, config.GetLanguage(), scopes...)
}

// GetWithLang return the value of given language set.
func GetWithLang(value, lang string) string {
if lang == "" {
lang = config.GetLanguage()
}
return GetWithScopeAndLanguageSet(value, lang)
}

// GetWithScopeAndLanguageSet return the value of given scopes and language set.
func GetWithScopeAndLanguageSet(value, lang string, scopes ...string) string {
if locale, ok := Lang[lang][JoinScopes(scopes)+strings.ToLower(value)]; ok {
return locale
}

Expand Down Expand Up @@ -125,6 +138,13 @@ func Add(key string, lang map[string]string) {
Lang[key] = lang
}

// AppendTo add more language translations to the given language set.
func AppendTo(lang string, set map[string]string) {
for key, value := range set {
Lang[lang][key] = value
}
}

func JoinScopes(scopes []string) string {
j := ""
for _, scope := range scopes {
Expand Down
28 changes: 17 additions & 11 deletions modules/menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"html/template"
"regexp"
"strconv"
"strings"

"github.com/GoAdminGroup/go-admin/modules/db/dialect"

Expand Down Expand Up @@ -165,7 +166,7 @@ func NewMenu(conn db.Connection, data NewMenuData) (int64, error) {
}

// GetGlobalMenu return Menu of given user model.
func GetGlobalMenu(user models.UserModel, conn db.Connection, pluginNames ...string) *Menu {
func GetGlobalMenu(user models.UserModel, conn db.Connection, lang string, pluginNames ...string) *Menu {

var (
menus []map[string]interface{}
Expand Down Expand Up @@ -202,19 +203,14 @@ func GetGlobalMenu(user models.UserModel, conn db.Connection, pluginNames ...str
var title string
for i := 0; i < len(menus); i++ {

if menus[i]["type"].(int64) == 1 {
title = language.Get(menus[i]["title"].(string))
} else {
title = menus[i]["title"].(string)
}

title = language.GetWithLang(menus[i]["title"].(string), lang)
menuOption = append(menuOption, map[string]string{
"id": strconv.FormatInt(menus[i]["id"].(int64), 10),
"title": title,
})
}

menuList := constructMenuTree(menus, 0)
menuList := constructMenuTree(menus, 0, lang)
maxOrder := int64(0)
if len(menus) > 0 {
maxOrder = menus[len(menus)-1]["parent_id"].(int64)
Expand All @@ -228,7 +224,7 @@ func GetGlobalMenu(user models.UserModel, conn db.Connection, pluginNames ...str
}
}

func constructMenuTree(menus []map[string]interface{}, parentID int64) []Item {
func constructMenuTree(menus []map[string]interface{}, parentID int64, lang string) []Item {

branch := make([]Item, 0)

Expand All @@ -243,14 +239,24 @@ func constructMenuTree(menus []map[string]interface{}, parentID int64) []Item {

header, _ := menus[j]["header"].(string)

uri := menus[j]["uri"].(string)

if lang != "" {
if strings.Index(uri, "?") != -1 {
uri += "&__ga_lang=" + lang
} else {
uri += "?__ga_lang=" + lang
}
}

child := Item{
Name: title,
ID: strconv.FormatInt(menus[j]["id"].(int64), 10),
Url: menus[j]["uri"].(string),
Url: uri,
Icon: menus[j]["icon"].(string),
Header: header,
Active: "",
ChildrenList: constructMenuTree(menus, menus[j]["id"].(int64)),
ChildrenList: constructMenuTree(menus, menus[j]["id"].(int64), lang),
}

branch = append(branch, child)
Expand Down
2 changes: 1 addition & 1 deletion modules/page/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func SetPageContent(ctx *context.Context, user models.UserModel, c func(ctx inte

err = tmpl.ExecuteTemplate(buf, tmplName, types.NewPage(&types.NewPageParam{
User: user,
Menu: menu.GetGlobalMenu(user, conn).SetActiveClass(config.URLRemovePrefix(ctx.Path())),
Menu: menu.GetGlobalMenu(user, conn, ctx.Lang()).SetActiveClass(config.URLRemovePrefix(ctx.Path())),
Panel: panel.GetContent(config.IsProductionEnvironment()),
Assets: template.GetComponentAssetImportHTML(),
TmplHeadHTML: template.Default().GetHeadHTML(),
Expand Down
4 changes: 2 additions & 2 deletions plugins/admin/controller/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (h *Handler) ExecuteWithBtns(ctx *context.Context, user models.UserModel, p
Tmpl: tmpl,
Panel: panel,
Config: *h.config,
Menu: menu.GetGlobalMenu(user, h.conn, plugName).SetActiveClass(h.config.URLRemovePrefix(ctx.Path())),
Menu: menu.GetGlobalMenu(user, h.conn, ctx.Lang(), plugName).SetActiveClass(h.config.URLRemovePrefix(ctx.Path())),
Animation: option.Animation,
Buttons: btns,
Iframe: ctx.Query(constant.IframeKey) == "true",
Expand All @@ -220,7 +220,7 @@ func (h *Handler) Execute(ctx *context.Context, user models.UserModel, panel typ
Tmpl: tmpl,
Panel: panel,
Config: *h.config,
Menu: menu.GetGlobalMenu(user, h.conn, plugName).SetActiveClass(h.config.URLRemovePrefix(ctx.Path())),
Menu: menu.GetGlobalMenu(user, h.conn, ctx.Lang(), plugName).SetActiveClass(h.config.URLRemovePrefix(ctx.Path())),
Animation: option.Animation,
Buttons: (*h.navButtons).CheckPermission(user),
Iframe: ctx.Query(constant.IframeKey) == "true",
Expand Down
6 changes: 3 additions & 3 deletions plugins/admin/controller/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (h *Handler) NewMenu(ctx *context.Context) {
// TODO: use transaction
menuModel, createErr := models.Menu().SetConn(h.conn).
New(param.Title, param.Icon, param.Uri, param.Header, param.PluginName, param.ParentId,
(menu.GetGlobalMenu(user, h.conn, param.PluginName)).MaxOrder+1)
(menu.GetGlobalMenu(user, h.conn, ctx.Lang(), param.PluginName)).MaxOrder+1)

if db.CheckError(createErr, db.INSERT) {
h.showNewMenu(ctx, createErr)
Expand All @@ -229,7 +229,7 @@ func (h *Handler) NewMenu(ctx *context.Context) {
}
}

menu.GetGlobalMenu(user, h.conn, param.PluginName).AddMaxOrder()
menu.GetGlobalMenu(user, h.conn, ctx.Lang(), param.PluginName).AddMaxOrder()

h.getMenuInfoPanel(ctx, param.PluginName, "")
ctx.AddHeader("Content-Type", "text/html; charset=utf-8")
Expand All @@ -255,7 +255,7 @@ func (h *Handler) getMenuInfoPanel(ctx *context.Context, plugName string, alert
}

tree := aTree().
SetTree((menu.GetGlobalMenu(user, h.conn, plugName)).List).
SetTree((menu.GetGlobalMenu(user, h.conn, ctx.Lang(), plugName)).List).
SetEditUrl(h.routePath("menu_edit_show")).
SetUrlPrefix(h.config.Prefix()).
SetDeleteUrl(h.routePath("menu_delete")).
Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/modules/response/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Alert(ctx *context.Context, desc, title, msg string, conn db.Connection, bt
Title: pageTitle,
},
Config: *config.Get(),
Menu: menu.GetGlobalMenu(user, conn).SetActiveClass(config.URLRemovePrefix(ctx.Path())),
Menu: menu.GetGlobalMenu(user, conn, ctx.Lang()).SetActiveClass(config.URLRemovePrefix(ctx.Path())),
Animation: true,
Buttons: *btns,
IsPjax: ctx.IsPjax(),
Expand Down
4 changes: 2 additions & 2 deletions plugins/admin/modules/table/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,10 @@ func (tb *DefaultTable) getTempModelData(res map[string]interface{}, params para
tempModelData["__goadmin_edit_params"] = types.InfoItem{Content: template.HTML("&" + editParams[:len(editParams)-1])}
}
if deleteParams != "" {
tempModelData["__goadmin_detele_params"] = types.InfoItem{Content: template.HTML("&" + deleteParams[:len(editParams)-1])}
tempModelData["__goadmin_delete_params"] = types.InfoItem{Content: template.HTML("&" + deleteParams[:len(deleteParams)-1])}
}
if detailParams != "" {
tempModelData["__goadmin_detail_params"] = types.InfoItem{Content: template.HTML("&" + detailParams[:len(editParams)-1])}
tempModelData["__goadmin_detail_params"] = types.InfoItem{Content: template.HTML("&" + detailParams[:len(detailParams)-1])}
}

primaryKeyField := tb.Info.FieldList.GetFieldByFieldName(tb.PrimaryKey.Name)
Expand Down
2 changes: 1 addition & 1 deletion plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func Execute(ctx *context.Context, conn db.Connection, navButtons types.Buttons,
Tmpl: tmpl,
Panel: panel,
Config: *config.Get(),
Menu: menu.GetGlobalMenu(user, conn).SetActiveClass(config.URLRemovePrefix(ctx.Path())),
Menu: menu.GetGlobalMenu(user, conn, ctx.Lang()).SetActiveClass(config.URLRemovePrefix(ctx.Path())),
Animation: options.Animation,
Buttons: navButtons.CheckPermission(user),
NoCompress: options.NoCompress,
Expand Down
Loading