mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 16:40:24 +08:00 
			
		
		
		
	Fix switch language (#14484)
Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
		@@ -15,24 +15,22 @@ import (
 | 
			
		||||
 | 
			
		||||
// Locale handle locale
 | 
			
		||||
func Locale(resp http.ResponseWriter, req *http.Request) translation.Locale {
 | 
			
		||||
	hasCookie := false
 | 
			
		||||
 | 
			
		||||
	// 1. Check URL arguments.
 | 
			
		||||
	lang := req.URL.Query().Get("lang")
 | 
			
		||||
	var changeLang = lang != ""
 | 
			
		||||
 | 
			
		||||
	// 2. Get language information from cookies.
 | 
			
		||||
	if len(lang) == 0 {
 | 
			
		||||
		ck, _ := req.Cookie("lang")
 | 
			
		||||
		if ck != nil {
 | 
			
		||||
			lang = ck.Value
 | 
			
		||||
			hasCookie = true
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Check again in case someone modify by purpose.
 | 
			
		||||
	if lang != "" && !i18n.IsExist(lang) {
 | 
			
		||||
		lang = ""
 | 
			
		||||
		hasCookie = false
 | 
			
		||||
		changeLang = false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 3. Get language information from 'Accept-Language'.
 | 
			
		||||
@@ -43,8 +41,8 @@ func Locale(resp http.ResponseWriter, req *http.Request) translation.Locale {
 | 
			
		||||
		lang = tag.String()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if !hasCookie {
 | 
			
		||||
		req.AddCookie(NewCookie("lang", lang, 1<<31-1))
 | 
			
		||||
	if changeLang {
 | 
			
		||||
		SetCookie(resp, "lang", lang, 1<<31-1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return translation.NewLocale(lang)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user