From 5051a7d2c187db244fc2aa45a6a46f0ad5c54046 Mon Sep 17 00:00:00 2001 From: shy Date: Sun, 10 Dec 2023 14:59:44 +0800 Subject: [PATCH] opt ice.MSG_LANGUAGE --- base/aaa/sess.go | 11 ++++++++++- base/web/html/html.go | 4 ++++ base/web/serve.go | 7 +++++-- core/chat/header.go | 10 ++-------- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/base/aaa/sess.go b/base/aaa/sess.go index beb91aec..ffa631ee 100644 --- a/base/aaa/sess.go +++ b/base/aaa/sess.go @@ -1,8 +1,11 @@ package aaa import ( + "strings" + ice "shylinux.com/x/icebergs" "shylinux.com/x/icebergs/base/mdb" + "shylinux.com/x/icebergs/base/web/html" kit "shylinux.com/x/toolkits" "shylinux.com/x/toolkits/logs" ) @@ -46,14 +49,20 @@ func SessCreate(m *ice.Message, username string) string { return m.Option(ice.MSG_SESSID, m.Cmdx(SESS, mdb.CREATE, username)) } func SessCheck(m *ice.Message, sessid string) bool { - m.Options(ice.MSG_USERNICK, "", ice.MSG_USERNAME, "", ice.MSG_USERROLE, VOID, "aaa.checker", logs.FileLine(-1)) + m.Options(ice.MSG_USERNICK, "", ice.MSG_USERNAME, "", ice.MSG_USERROLE, VOID, ice.MSG_LANGUAGE, "", "aaa.checker", logs.FileLine(-1)) return sessid != "" && m.Cmdy(SESS, CHECK, sessid, logs.FileLineMeta(-1)).Option(ice.MSG_USERNAME) != "" } func SessAuth(m *ice.Message, value ice.Any, arg ...string) *ice.Message { + language := kit.Format(kit.Value(value, LANGUAGE)) + kit.If(language == "", func() { language = kit.Select("", "zh-cn", strings.Contains(m.Option(ice.MSG_USERUA), "zh_CN")) }) + kit.If(language == "" && m.R != nil, func() { language = kit.Select("", kit.Split(m.R.Header.Get(html.AcceptLanguage), ",;"), 0) }) + kit.If(language == "", func() { language = ice.Info.Lang }) + language = strings.ReplaceAll(strings.ToLower(kit.Split(language, " .")[0]), "_", "-") return m.Auth( USERNICK, m.Option(ice.MSG_USERNICK, kit.Format(kit.Value(value, USERNICK))), USERNAME, m.Option(ice.MSG_USERNAME, kit.Format(kit.Value(value, USERNAME))), USERROLE, m.Option(ice.MSG_USERROLE, kit.Format(kit.Value(value, USERROLE))), + LANGUAGE, m.Option(ice.MSG_LANGUAGE, language), arg, logs.FileLineMeta(kit.Select(logs.FileLine(-1), m.Option("aaa.checker"))), ) } diff --git a/base/web/html/html.go b/base/web/html/html.go index 0fd8967f..92509343 100644 --- a/base/web/html/html.go +++ b/base/web/html/html.go @@ -77,3 +77,7 @@ func FormatDanger(value string) string { COLOR, "var(--danger-fg-color)", )) } + +const ( + AcceptLanguage = "Accept-Language" +) diff --git a/base/web/serve.go b/base/web/serve.go index 39869973..8b8c6cbb 100644 --- a/base/web/serve.go +++ b/base/web/serve.go @@ -178,12 +178,15 @@ func _serve_auth(m *ice.Message, key string, cmds []string, w http.ResponseWrite ls := kit.Simple(mdb.Cache(m, m.Option(ice.MSG_USERIP), func() ice.Any { if IsLocalHost(m) { aaa.UserRoot(m) - return kit.Simple(m.Time(), m.OptionSplit(ice.MSG_USERNICK, ice.MSG_USERNAME, ice.MSG_USERROLE)) + return kit.Simple(m.Time(), m.OptionSplit(ice.MSG_USERNICK, ice.MSG_USERNAME, ice.MSG_USERROLE, ice.MSG_LANGUAGE)) } return nil })) if len(ls) > 0 { - m.Auth(aaa.USERNICK, m.Option(ice.MSG_USERNICK, ls[1]), aaa.USERNAME, m.Option(ice.MSG_USERNAME, ls[2]), aaa.USERROLE, m.Option(ice.MSG_USERROLE, ls[3]), CACHE, ls[0]) + m.Auth(aaa.USERNICK, m.Option(ice.MSG_USERNICK, ls[1]), + aaa.USERNAME, m.Option(ice.MSG_USERNAME, ls[2]), + aaa.USERROLE, m.Option(ice.MSG_USERROLE, ls[3]), + aaa.LANGUAGE, m.Option(ice.MSG_LANGUAGE, ls[4]), CACHE, ls[0]) } } m.Cmd(COUNT, mdb.CREATE, aaa.IP, m.Option(ice.MSG_USERIP), m.Option(ice.MSG_USERUA), kit.Dict(ice.LOG_DISABLE, ice.TRUE)) diff --git a/core/chat/header.go b/core/chat/header.go index 24b63a5e..33c29a79 100644 --- a/core/chat/header.go +++ b/core/chat/header.go @@ -1,8 +1,6 @@ package chat import ( - "strings" - ice "shylinux.com/x/icebergs" "shylinux.com/x/icebergs/base/aaa" "shylinux.com/x/icebergs/base/cli" @@ -131,13 +129,9 @@ func init() { return } msg := m.Cmd(aaa.USER, m.Option(ice.MSG_USERNAME)) - kit.For([]string{aaa.LANGUAGE, aaa.USERNICK, aaa.EMAIL}, func(k string) { m.Option(k, msg.Append(k)) }) + m.Option(aaa.LANGUAGE, m.Option(ice.MSG_LANGUAGE)) + kit.For([]string{aaa.USERNICK, aaa.EMAIL}, func(k string) { m.Option(k, msg.Append(k)) }) kit.For([]string{aaa.AVATAR, aaa.BACKGROUND}, func(k string) { m.Option(k, web.RequireFile(m, msg.Append(k))) }) - kit.If(m.Option(aaa.LANGUAGE) == "", func() { - m.Option(aaa.LANGUAGE, kit.Select("", "zh-cn", strings.Contains(m.Option(ice.MSG_USERUA), "zh_CN"))) - }) - kit.If(m.Option(aaa.LANGUAGE) == "", func() { m.Option(aaa.LANGUAGE, kit.Select("", kit.Split(m.R.Header.Get(web.AcceptLanguage), ",;"), 0)) }) - kit.If(m.Option(aaa.LANGUAGE) == "", func() { m.Option(aaa.LANGUAGE, ice.Info.Lang) }) }}, }) }