From 5692522ee0b8b5bb4b257912f33a10e422348f3c Mon Sep 17 00:00:00 2001 From: WindSpiritSR Date: Sat, 1 Mar 2025 22:28:20 +0800 Subject: [PATCH] feat: update user language when the language changed on login page (#3628) --- controllers/auth.go | 12 ++++++++++++ form/auth.go | 1 + web/src/auth/LoginPage.js | 5 +++-- web/src/common/select/LanguageSelect.js | 4 ++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/controllers/auth.go b/controllers/auth.go index f9995830..8be118a9 100644 --- a/controllers/auth.go +++ b/controllers/auth.go @@ -986,6 +986,18 @@ func (c *ApiController) Login() { } } + if authForm.Language != "" { + user := c.getCurrentUser() + if user != nil { + user.Language = authForm.Language + _, err = object.UpdateUser(user.GetId(), user, []string{"language"}, user.IsAdmin) + if err != nil { + c.ResponseError(err.Error()) + return + } + } + } + c.Data["json"] = resp c.ServeJSON() } diff --git a/form/auth.go b/form/auth.go index 72057b90..91a3ee54 100644 --- a/form/auth.go +++ b/form/auth.go @@ -34,6 +34,7 @@ type AuthForm struct { Phone string `json:"phone"` Affiliation string `json:"affiliation"` IdCard string `json:"idCard"` + Language string `json:"language"` Region string `json:"region"` InvitationCode string `json:"invitationCode"` diff --git a/web/src/auth/LoginPage.js b/web/src/auth/LoginPage.js index 6b5b2565..48075c14 100644 --- a/web/src/auth/LoginPage.js +++ b/web/src/auth/LoginPage.js @@ -61,6 +61,7 @@ class LoginPage extends React.Component { isTermsOfUseVisible: false, termsOfUseContent: "", orgChoiceMode: new URLSearchParams(props.location?.search).get("orgChoiceMode") ?? null, + userLang: null, }; if (this.state.type === "cas" && props.match?.params.casApplicationName !== undefined) { @@ -415,6 +416,7 @@ class LoginPage extends React.Component { login(values) { // here we are supposed to determine whether Casdoor is working as an OAuth server or CAS server + values["language"] = this.state.userLang ?? ""; if (this.state.type === "cas") { // CAS const casParams = Util.getCasParameters(); @@ -566,7 +568,7 @@ class LoginPage extends React.Component { return (
" + signinItem.customCss?.replaceAll("", "") + "")}} /> - + {this.setState({userLang: key});}} />
); } else if (signinItem.name === "Signin methods") { @@ -805,7 +807,6 @@ class LoginPage extends React.Component {
item.key), + onClick: props.onClick, }; Setting.Countries.forEach((country) => { @@ -50,6 +51,9 @@ class LanguageSelect extends React.Component { render() { const languageItems = this.getOrganizationLanguages(this.state.languages); const onClick = (e) => { + if (typeof this.state.onClick === "function") { + this.state.onClick(e.key); + } Setting.setLanguage(e.key); };