Skip to content

Commit e678a16

Browse files
laijinhangcg33
authored andcommitted
Switch language on the front end, implement the back end code
1 parent e031b8d commit e678a16

File tree

7 files changed

+113
-11
lines changed

7 files changed

+113
-11
lines changed

modules/config/config.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,47 @@ type ExtraInfo map[string]interface{}
455455

456456
type UpdateConfigProcessFn func(values form.Values) (form.Values, error)
457457

458+
// UserConfig type is the user config of goAdmin.
459+
type UserConfig struct {
460+
// user id
461+
UserId int64 `json:"userid",yaml:"userid",ini:"userid"`
462+
463+
// Used to set as the user language which show in the
464+
// interface.
465+
Language string `json:"language",yaml:"language",ini:"language"`
466+
467+
// Extend
468+
// ...
469+
}
470+
471+
var userConfig []UserConfig
472+
473+
// Get gets the config.
474+
func SetUserConfig(uConf UserConfig) {
475+
// insert or update to database, If there is a database
476+
for i := 0; i < len(userConfig); i++ {
477+
if userConfig[i].UserId == uConf.UserId {
478+
userConfig[i] = uConf
479+
return
480+
}
481+
}
482+
userConfig = append(userConfig, uConf)
483+
}
484+
485+
// Get GetUserConf the config.
486+
func GetUserConf(uId int64) *UserConfig {
487+
for i := 0; i < len(userConfig); i++ {
488+
if userConfig[i].UserId == uId {
489+
return &userConfig[i]
490+
}
491+
}
492+
SetUserConfig(UserConfig{
493+
UserId: uId,
494+
Language: _global.Language,
495+
})
496+
return &userConfig[len(userConfig)-1]
497+
}
498+
458499
// see more: https://daneden.github.io/animate.css/
459500
type PageAnimation struct {
460501
Type string `json:"type,omitempty" yaml:"type,omitempty" ini:"type,omitempty"`

modules/language/language.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,34 @@ func JoinScopes(scopes []string) string {
160160
}
161161
return j
162162
}
163+
164+
// GetUser return the value of user scope.
165+
func GetUser(value string, uid int64) string {
166+
return GetUserWithScope(value, uid)
167+
}
168+
169+
// GetUserWithScope return the value of given scopes.
170+
func GetUserWithScope(value string, uid int64, scopes ...string) string {
171+
if config.GetUserConf(uid).Language == "" {
172+
return value
173+
}
174+
175+
if locale, ok := Lang[config.GetUserConf(uid).Language][JoinScopes(scopes)+strings.ToLower(value)]; ok {
176+
return locale
177+
}
178+
179+
return value
180+
}
181+
182+
// GetUserFromHtml return the value of given scopes and template.HTML value.
183+
func GetUserFromHtml(value template.HTML, uid int64, scopes ...string) template.HTML {
184+
if config.GetUserConf(uid).Language == "" {
185+
return value
186+
}
187+
188+
if locale, ok := Lang[config.GetUserConf(uid).Language][JoinScopes(scopes)+strings.ToLower(string(value))]; ok {
189+
return template.HTML(locale)
190+
}
191+
192+
return value
193+
}

modules/menu/menu.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,19 @@ func GetGlobalMenu(user models.UserModel, conn db.Connection, lang string, plugi
203203
var title string
204204
for i := 0; i < len(menus); i++ {
205205

206-
title = language.GetWithLang(menus[i]["title"].(string), lang)
206+
if menus[i]["type"].(int64) == 1 {
207+
title = language.GetUser(menus[i]["title"].(string), user.Id)
208+
} else {
209+
title = language.GetWithLang(menus[i]["title"].(string), lang)
210+
}
211+
207212
menuOption = append(menuOption, map[string]string{
208213
"id": strconv.FormatInt(menus[i]["id"].(int64), 10),
209214
"title": title,
210215
})
211216
}
212217

213-
menuList := constructMenuTree(menus, 0, lang)
218+
menuList := constructMenuTree(menus, 0, lang, user.Id)
214219
maxOrder := int64(0)
215220
if len(menus) > 0 {
216221
maxOrder = menus[len(menus)-1]["parent_id"].(int64)
@@ -224,15 +229,16 @@ func GetGlobalMenu(user models.UserModel, conn db.Connection, lang string, plugi
224229
}
225230
}
226231

227-
func constructMenuTree(menus []map[string]interface{}, parentID int64, lang string) []Item {
232+
func constructMenuTree(menus []map[string]interface{}, parentID int64, lang string, uid int64) []Item {
228233

229234
branch := make([]Item, 0)
230235

231236
var title string
232237
for j := 0; j < len(menus); j++ {
233238
if parentID == menus[j]["parent_id"].(int64) {
239+
234240
if menus[j]["type"].(int64) == 1 {
235-
title = language.Get(menus[j]["title"].(string))
241+
title = language.GetUser(menus[j]["title"].(string), uid)
236242
} else {
237243
title = menus[j]["title"].(string)
238244
}
@@ -256,7 +262,7 @@ func constructMenuTree(menus []map[string]interface{}, parentID int64, lang stri
256262
Icon: menus[j]["icon"].(string),
257263
Header: header,
258264
Active: "",
259-
ChildrenList: constructMenuTree(menus, menus[j]["id"].(int64), lang),
265+
ChildrenList: constructMenuTree(menus, menus[j]["id"].(int64), lang, uid),
260266
}
261267

262268
branch = append(branch, child)

plugins/admin/controller/detail.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,19 @@ $('.delete-btn').on('click', function (event) {
118118
desc := ""
119119

120120
isNotIframe := ctx.Query(constant.IframeKey) != "true"
121+
if title == "" {
122+
title = panel.GetInfo().Title + language.GetUser("Detail", user.Id)
123+
}
121124

122125
if isNotIframe {
123126
title = detail.Title
124127

125128
if title == "" {
126129
title = info.Title + language.Get("Detail")
127130
}
131+
if desc == "" {
132+
desc = panel.GetInfo().Description + language.GetUser("Detail", user.Id)
133+
}
128134

129135
desc = detail.Description
130136

plugins/admin/controller/edit.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import (
1414

1515
"github.com/GoAdminGroup/go-admin/context"
1616
"github.com/GoAdminGroup/go-admin/modules/auth"
17+
"github.com/GoAdminGroup/go-admin/modules/config"
1718
"github.com/GoAdminGroup/go-admin/modules/file"
1819
"github.com/GoAdminGroup/go-admin/modules/language"
20+
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
1921
"github.com/GoAdminGroup/go-admin/plugins/admin/modules"
2022
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/constant"
2123
form2 "github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
@@ -228,3 +230,15 @@ func (h *Handler) EditForm(ctx *context.Context) {
228230
ctx.HTML(http.StatusOK, buf.String())
229231
ctx.AddHeader(constant.PjaxUrlHeader, param.PreviousPath)
230232
}
233+
234+
func (h *Handler) SetLanguage(ctx *context.Context) {
235+
if len(ctx.PostForm()["language"]) == 0 {
236+
return
237+
}
238+
if user, ok := ctx.UserValue["user"].(models.UserModel); ok {
239+
config.SetUserConfig(config.UserConfig{
240+
UserId: user.Id,
241+
Language: ctx.PostForm()["language"][0],
242+
})
243+
}
244+
}

plugins/admin/controller/show.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (h *Handler) showTable(ctx *context.Context, prefix string, params paramete
137137
ext := template2.HTML("")
138138
if deleteUrl != "" {
139139
ext = html.LiEl().SetClass("divider").Get()
140-
allActionBtns = append([]types.Button{types.GetActionButton(language.GetFromHtml("delete"),
140+
allActionBtns = append([]types.Button{types.GetActionButton(language.GetUserFromHtml("delete", user.Id),
141141
types.NewDefaultAction(`data-id='{{.Id}}' data-param='{{(index .Value "__goadmin_delete_params").Content}}' style="cursor: pointer;"`,
142142
ext, "", ""), "grid-row-delete")}, allActionBtns...)
143143
}
@@ -146,14 +146,14 @@ func (h *Handler) showTable(ctx *context.Context, prefix string, params paramete
146146
if editUrl == "" && deleteUrl == "" {
147147
ext = html.LiEl().SetClass("divider").Get()
148148
}
149-
allActionBtns = append([]types.Button{types.GetActionButton(language.GetFromHtml("detail"),
149+
allActionBtns = append([]types.Button{types.GetActionButton(language.GetUserFromHtml("detail", user.Id),
150150
action.Jump(detailUrl+"&"+constant.DetailPKKey+`={{.Id}}{{(index .Value "__goadmin_detail_params").Content}}`, ext))}, allActionBtns...)
151151
}
152152
if editUrl != "" {
153153
if detailUrl == "" && deleteUrl == "" {
154154
ext = html.LiEl().SetClass("divider").Get()
155155
}
156-
allActionBtns = append([]types.Button{types.GetActionButton(language.GetFromHtml("edit"),
156+
allActionBtns = append([]types.Button{types.GetActionButton(language.GetUserFromHtml("edit", user.Id),
157157
action.Jump(editUrl+"&"+constant.EditPKKey+`={{.Id}}{{(index .Value "__goadmin_edit_params").Content}}`, ext))}, allActionBtns...)
158158
}
159159

plugins/admin/modules/response/response.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/GoAdminGroup/go-admin/modules/errors"
1111
"github.com/GoAdminGroup/go-admin/modules/language"
1212
"github.com/GoAdminGroup/go-admin/modules/menu"
13+
"github.com/GoAdminGroup/go-admin/plugins/admin/models"
1314
"github.com/GoAdminGroup/go-admin/template"
1415
"github.com/GoAdminGroup/go-admin/template/types"
1516
)
@@ -37,9 +38,10 @@ func OkWithData(ctx *context.Context, data map[string]interface{}) {
3738
}
3839

3940
func BadRequest(ctx *context.Context, msg string) {
41+
user := ctx.UserValue["user"].(models.UserModel)
4042
ctx.JSON(http.StatusBadRequest, map[string]interface{}{
4143
"code": http.StatusBadRequest,
42-
"msg": language.Get(msg),
44+
"msg": language.GetUser(msg, user.Id),
4345
})
4446
}
4547

@@ -75,9 +77,10 @@ func Alert(ctx *context.Context, desc, title, msg string, conn db.Connection, bt
7577
}
7678

7779
func Error(ctx *context.Context, msg string, datas ...map[string]interface{}) {
80+
user := ctx.UserValue["user"].(models.UserModel)
7881
res := map[string]interface{}{
7982
"code": http.StatusInternalServerError,
80-
"msg": language.Get(msg),
83+
"msg": language.GetUser(msg, user.Id),
8184
}
8285
if len(datas) > 0 {
8386
res["data"] = datas[0]
@@ -86,9 +89,10 @@ func Error(ctx *context.Context, msg string, datas ...map[string]interface{}) {
8689
}
8790

8891
func Denied(ctx *context.Context, msg string) {
92+
user := ctx.UserValue["user"].(models.UserModel)
8993
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
9094
"code": http.StatusForbidden,
91-
"msg": language.Get(msg),
95+
"msg": language.GetUser(msg, user.Id),
9296
})
9397
}
9498

0 commit comments

Comments
 (0)