Skip to content

Commit 3d44557

Browse files
zeripathGusted
and
Gusted
committed
Reset locale on login (go-gitea#18023)
Backport go-gitea#18023 Although we reset the locale in a number of places there were several ways of logging in that were missing the same code. Fix go-gitea#18020 Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Gusted <[email protected]>
1 parent fe91d96 commit 3d44557

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

routers/web/user/auth.go

+33
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,33 @@ func AutoSignIn(ctx *context.Context) (bool, error) {
9898
return false, err
9999
}
100100

101+
if err := resetLocale(ctx, u); err != nil {
102+
return false, err
103+
}
104+
101105
middleware.DeleteCSRFCookie(ctx.Resp)
102106
return true, nil
103107
}
104108

109+
func resetLocale(ctx *context.Context, u *user_model.User) error {
110+
// Language setting of the user overwrites the one previously set
111+
// If the user does not have a locale set, we save the current one.
112+
if len(u.Language) == 0 {
113+
u.Language = ctx.Locale.Language()
114+
if err := user_model.UpdateUserCols(db.DefaultContext, u, "language"); err != nil {
115+
return err
116+
}
117+
}
118+
119+
middleware.SetLocaleCookie(ctx.Resp, u.Language, 0)
120+
121+
if ctx.Locale.Language() != u.Language {
122+
ctx.Locale = middleware.Locale(ctx.Resp, ctx.Req)
123+
}
124+
125+
return nil
126+
}
127+
105128
func checkAutoLogin(ctx *context.Context) bool {
106129
// Check auto-login.
107130
isSucceed, err := AutoSignIn(ctx)
@@ -738,6 +761,11 @@ func handleOAuth2SignIn(ctx *context.Context, u *models.User, gothUser goth.User
738761
log.Error("UpdateExternalUser failed: %v", err)
739762
}
740763

764+
if err := resetLocale(ctx, u); err != nil {
765+
ctx.ServerError("resetLocale", err)
766+
return
767+
}
768+
741769
if redirectTo := ctx.GetCookie("redirect_to"); len(redirectTo) > 0 {
742770
middleware.DeleteRedirectToCookie(ctx.Resp)
743771
ctx.RedirectToFirst(redirectTo)
@@ -1447,6 +1475,11 @@ func handleAccountActivation(ctx *context.Context, user *models.User) {
14471475
log.Error("Error storing session[%s]: %v", ctx.Session.ID(), err)
14481476
}
14491477

1478+
if err := resetLocale(ctx, user); err != nil {
1479+
ctx.ServerError("resetLocale", err)
1480+
return
1481+
}
1482+
14501483
ctx.Flash.Success(ctx.Tr("auth.account_activated"))
14511484
ctx.Redirect(setting.AppSubURL + "/")
14521485
}

0 commit comments

Comments
 (0)