Skip to content

Commit 669ff8e

Browse files
lunny6543lafriks
authored
Fix switch language (#14484)
Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv>
1 parent af7f712 commit 669ff8e

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

modules/middlewares/locale.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@ import (
1515

1616
// Locale handle locale
1717
func Locale(resp http.ResponseWriter, req *http.Request) translation.Locale {
18-
hasCookie := false
19-
2018
// 1. Check URL arguments.
2119
lang := req.URL.Query().Get("lang")
20+
var changeLang = lang != ""
2221

2322
// 2. Get language information from cookies.
2423
if len(lang) == 0 {
2524
ck, _ := req.Cookie("lang")
2625
if ck != nil {
2726
lang = ck.Value
28-
hasCookie = true
2927
}
3028
}
3129

3230
// Check again in case someone modify by purpose.
3331
if lang != "" && !i18n.IsExist(lang) {
3432
lang = ""
35-
hasCookie = false
33+
changeLang = false
3634
}
3735

3836
// 3. Get language information from 'Accept-Language'.
@@ -43,8 +41,8 @@ func Locale(resp http.ResponseWriter, req *http.Request) translation.Locale {
4341
lang = tag.String()
4442
}
4543

46-
if !hasCookie {
47-
req.AddCookie(NewCookie("lang", lang, 1<<31-1))
44+
if changeLang {
45+
SetCookie(resp, "lang", lang, 1<<31-1)
4846
}
4947

5048
return translation.NewLocale(lang)

0 commit comments

Comments
 (0)