Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch language on the front end #189

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion modules/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ type Service struct {
P Processor
}

func (s *Service) Name() string {
func (*Service) Name() string {
return "auth"
}

Expand Down
57 changes: 49 additions & 8 deletions modules/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ type Config struct {

AssetRootPath string `json:"asset_root_path,omitempty" yaml:"asset_root_path,omitempty" ini:"asset_root_path,omitempty"`

URLFormat URLFormat `json:"url_format,omitempty" yaml:"url_format,omitempty" ini:"url_format,omitempty"`
URLFormat *URLFormat `json:"url_format,omitempty" yaml:"url_format,omitempty" ini:"url_format,omitempty"`

prefix string `json:"-" yaml:"-" ini:"-"`
lock sync.RWMutex `json:"-" yaml:"-" ini:"-"`
Expand Down Expand Up @@ -438,7 +438,7 @@ type URLFormat struct {
Update string `json:"update,omitempty" yaml:"update,omitempty" ini:"update,omitempty"`
}

func (f URLFormat) SetDefault() URLFormat {
func (f *URLFormat) SetDefault() {
f.Detail = utils.SetDefault(f.Detail, "", "/info/:__prefix/detail")
f.ShowEdit = utils.SetDefault(f.ShowEdit, "", "/info/:__prefix/edit")
f.ShowCreate = utils.SetDefault(f.ShowCreate, "", "/info/:__prefix/new")
Expand All @@ -448,13 +448,53 @@ func (f URLFormat) SetDefault() URLFormat {
f.Export = utils.SetDefault(f.Export, "", "/export/:__prefix")
f.Info = utils.SetDefault(f.Info, "", "/info/:__prefix")
f.Update = utils.SetDefault(f.Update, "", "/update/:__prefix")
return f
}

type ExtraInfo map[string]interface{}

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

// UserConfig type is the user config of goAdmin.
type UserConfig struct {
// user id
UserId int64 `json:"userid",yaml:"userid",ini:"userid"`

// Used to set as the user language which show in the
// interface.
Language string `json:"language",yaml:"language",ini:"language"`

// Extend
// ...
}

var userConfig []UserConfig

// Set SetUserConfig the config.
func SetUserConfig(uConf UserConfig) {
// insert or update to database, If there is a database
for i := 0; i < len(userConfig); i++ {
if userConfig[i].UserId == uConf.UserId {
userConfig[i] = uConf
return
}
}
userConfig = append(userConfig, uConf)
}

// Get GetUserConf the config.
func GetUserConf(uId int64) *UserConfig {
for i := 0; i < len(userConfig); i++ {
if userConfig[i].UserId == uId {
return &userConfig[i]
}
}
SetUserConfig(UserConfig{
UserId: uId,
Language: _global.Language,
})
return &userConfig[len(userConfig)-1]
}

// see more: https://daneden.github.io/animate.css/
type PageAnimation struct {
Type string `json:"type,omitempty" yaml:"type,omitempty" ini:"type,omitempty"`
Expand Down Expand Up @@ -910,7 +950,8 @@ func SetDefault(cfg *Config) *Config {
} else {
cfg.prefix = cfg.UrlPrefix
}
cfg.URLFormat = cfg.URLFormat.SetDefault()
cfg.URLFormat = new(URLFormat)
cfg.URLFormat.SetDefault()
return cfg
}

Expand Down Expand Up @@ -971,8 +1012,8 @@ func AssertPrefix() string {
return _global.AssertPrefix()
}

// GetIndexURL get the index url with prefix.
func GetIndexURL() string {
// GetFullIndexURL get the index url with prefix.
func GetFullIndexURL() string {
return _global.GetIndexURL()
}

Expand All @@ -995,7 +1036,7 @@ func Url(suffix string) string {
return _global.Url(suffix)
}

func GetURLFormats() URLFormat {
func GetURLFormats() *URLFormat {
return _global.URLFormat
}

Expand Down Expand Up @@ -1291,7 +1332,7 @@ type Service struct {
C *Config
}

func (s *Service) Name() string {
func (*Service) Name() string {
return "config"
}

Expand Down
2 changes: 2 additions & 0 deletions modules/language/cn.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,6 @@ var cn = LangSet{
"admin.basic admin": "基础Admin",
"admin.a built-in plugins of goadmin which help you to build a crud manager platform quickly.": "一个内置GoAdmin插件,帮助您快速搭建curd简易管理后台。",
"admin.official": "GoAdmin官方",

"language": "语言",
}
1 change: 1 addition & 0 deletions modules/language/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,5 @@ var en = LangSet{
"admin.basic admin": "Basic Admin",
"admin.a built-in plugins of goadmin which help you to build a crud manager platform quickly.": "A built-in plugins of GoAdmin which help you to build a crud manager platform quickly.",
"admin.official": "Official",
"language": "Language",
}
6 changes: 6 additions & 0 deletions modules/language/jp.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,10 @@ var jp = LangSet{
"admin.basic admin": "Basic Admin",
"admin.a built-in plugins of goadmin which help you to build a crud manager platform quickly.": "A built-in plugins of GoAdmin which help you to build a crud manager platform quickly.",
"admin.official": "Official",

"used for login": "ログインに使用",
"used to display": "表示に使用",
"a path a line": "パスの文字列",

"language": "言語",
}
31 changes: 31 additions & 0 deletions modules/language/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,34 @@ func JoinScopes(scopes []string) string {
}
return j
}

// GetUser return the value of user scope.
func GetUser(value string, uid int64) string {
return GetUserWithScope(value, uid)
}

// GetUserWithScope return the value of given scopes.
func GetUserWithScope(value string, uid int64, scopes ...string) string {
if config.GetUserConf(uid).Language == "" {
return value
}

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

return value
}

// GetUserFromHtml return the value of given scopes and template.HTML value.
func GetUserFromHtml(value template.HTML, uid int64, scopes ...string) template.HTML {
if config.GetUserConf(uid).Language == "" {
return value
}

if locale, ok := Lang[config.GetUserConf(uid).Language][JoinScopes(scopes)+strings.ToLower(string(value))]; ok {
return template.HTML(locale)
}

return value
}
1 change: 1 addition & 0 deletions modules/language/tc.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,5 @@ var tc = LangSet{
"admin.basic admin": "基礎Admin",
"admin.a built-in plugins of goadmin which help you to build a crud manager platform quickly.": "壹個內置GoAdmin插件,幫助您快速搭建curd簡易管理後臺。",
"admin.official": "GoAdmin官方",
"language": "語言",
}
16 changes: 11 additions & 5 deletions modules/menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,19 @@ func GetGlobalMenu(user models.UserModel, conn db.Connection, lang string, plugi
var title string
for i := 0; i < len(menus); i++ {

title = language.GetWithLang(menus[i]["title"].(string), lang)
if menus[i]["type"].(int64) == 1 {
title = language.GetUser(menus[i]["title"].(string), user.Id)
} else {
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, lang)
menuList := constructMenuTree(menus, 0, lang, user.Id)
maxOrder := int64(0)
if len(menus) > 0 {
maxOrder = menus[len(menus)-1]["parent_id"].(int64)
Expand All @@ -224,15 +229,16 @@ func GetGlobalMenu(user models.UserModel, conn db.Connection, lang string, plugi
}
}

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

branch := make([]Item, 0)

var title string
for j := 0; j < len(menus); j++ {
if parentID == menus[j]["parent_id"].(int64) {

if menus[j]["type"].(int64) == 1 {
title = language.Get(menus[j]["title"].(string))
title = language.GetUser(menus[j]["title"].(string), uid)
} else {
title = menus[j]["title"].(string)
}
Expand All @@ -256,7 +262,7 @@ func constructMenuTree(menus []map[string]interface{}, parentID int64, lang stri
Icon: menus[j]["icon"].(string),
Header: header,
Active: "",
ChildrenList: constructMenuTree(menus, menus[j]["id"].(int64), lang),
ChildrenList: constructMenuTree(menus, menus[j]["id"].(int64), lang, uid),
}

branch = append(branch, child)
Expand Down
2 changes: 1 addition & 1 deletion modules/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Service struct {

const ServiceKey = "ui"

func (s *Service) Name() string {
func (*Service) Name() string {
return "ui"
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (admin *Admin) InitPlugin(services service.List) {
}

func (admin *Admin) GetIndexURL() string {
return config.GetIndexURL()
return config.GetFullIndexURL()
}

func (admin *Admin) GetInfo() plugins.Info {
Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/controller/api_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (h *Handler) ApiList(ctx *context.Context) {
params := parameter.GetParam(ctx.Request.URL, panel.GetInfo().DefaultPageSize, panel.GetInfo().SortField,
panel.GetInfo().GetSort())

panel, panelInfo, urls, err := h.showTableData(ctx, prefix, params, panel, "api_")
panel, panelInfo, urls, err := h._showTableData(ctx, prefix, params, panel, "api_")
if err != nil {
response.Error(ctx, err.Error())
return
Expand Down
7 changes: 5 additions & 2 deletions plugins/admin/controller/detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,21 @@ $('.delete-btn').on('click', function (event) {
desc := ""

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

if isNotIframe {
title = detail.Title

if title == "" {
title = info.Title + language.Get("Detail")
title = info.Title + language.GetUser("Detail", user.Id)
}

desc = detail.Description

if desc == "" {
desc = info.Description + language.Get("Detail")
desc = info.Description + language.GetUser("Detail", user.Id)
}
}

Expand Down
14 changes: 7 additions & 7 deletions plugins/admin/controller/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"net/url"

"github.com/GoAdminGroup/go-admin/modules/language"
"github.com/GoAdminGroup/go-admin/modules/logger"

"github.com/GoAdminGroup/go-admin/template"
Expand All @@ -15,7 +16,6 @@ import (
"github.com/GoAdminGroup/go-admin/context"
"github.com/GoAdminGroup/go-admin/modules/auth"
"github.com/GoAdminGroup/go-admin/modules/file"
"github.com/GoAdminGroup/go-admin/modules/language"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules"
"github.com/GoAdminGroup/go-admin/plugins/admin/modules/constant"
form2 "github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
Expand All @@ -28,10 +28,10 @@ import (
// ShowForm show form page.
func (h *Handler) ShowForm(ctx *context.Context) {
param := guard.GetShowFormParam(ctx)
h.showForm(ctx, "", param.Prefix, param.Param, false)
h._showForm(ctx, "", param.Prefix, param.Param, false)
}

func (h *Handler) showForm(ctx *context.Context, alert template2.HTML, prefix string, param parameter.Parameters, isEdit bool, animation ...bool) {
func (h *Handler) _showForm(ctx *context.Context, alert template2.HTML, prefix string, param parameter.Parameters, isEdit bool, animation ...bool) {

panel := h.table(prefix, ctx)

Expand Down Expand Up @@ -147,7 +147,7 @@ func (h *Handler) EditForm(ctx *context.Context) {
if ctx.WantJSON() {
response.Error(ctx, err.Error())
} else {
h.showForm(ctx, aAlert().Warning(err.Error()), param.Prefix, param.Param, true)
h._showForm(ctx, aAlert().Warning(err.Error()), param.Prefix, param.Param, true)
}
return
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func (h *Handler) EditForm(ctx *context.Context) {
"token": h.authSrv().AddToken(),
})
} else {
h.showForm(ctx, aAlert().Warning(err.Error()), param.Prefix, param.Param, true)
h._showForm(ctx, aAlert().Warning(err.Error()), param.Prefix, param.Param, true)
}
return
}
Expand All @@ -203,7 +203,7 @@ func (h *Handler) EditForm(ctx *context.Context) {
}

if isEditUrl(param.PreviousPath, param.Prefix) {
h.showForm(ctx, param.Alert, param.Prefix, param.Param, true, false)
h._showForm(ctx, param.Alert, param.Prefix, param.Param, true, false)
return
}

Expand All @@ -223,7 +223,7 @@ func (h *Handler) EditForm(ctx *context.Context) {
return
}

buf := h.showTable(ctx, param.Prefix, param.Param.DeletePK().DeleteEditPk(), nil)
buf := h._showTable(ctx, param.Prefix, param.Param.DeletePK().DeleteEditPk(), nil)

ctx.HTML(http.StatusOK, buf.String())
ctx.AddHeader(constant.PjaxUrlHeader, param.PreviousPath)
Expand Down
2 changes: 1 addition & 1 deletion plugins/admin/controller/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (h *Handler) NewForm(ctx *context.Context) {
return
}

buf := h.showTable(ctx, param.Prefix, param.Param, nil)
buf := h._showTable(ctx, param.Prefix, param.Param, nil)

ctx.HTML(http.StatusOK, buf.String())
ctx.AddHeader(constant.PjaxUrlHeader, h.routePathWithPrefix("info", param.Prefix)+param.Param.GetRouteParamStr())
Expand Down
Loading