|
|
@ -15,24 +15,22 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
// Locale handle locale
|
|
|
|
// Locale handle locale
|
|
|
|
func Locale(resp http.ResponseWriter, req *http.Request) translation.Locale { |
|
|
|
func Locale(resp http.ResponseWriter, req *http.Request) translation.Locale { |
|
|
|
hasCookie := false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Check URL arguments.
|
|
|
|
// 1. Check URL arguments.
|
|
|
|
lang := req.URL.Query().Get("lang") |
|
|
|
lang := req.URL.Query().Get("lang") |
|
|
|
|
|
|
|
var changeLang = lang != "" |
|
|
|
|
|
|
|
|
|
|
|
// 2. Get language information from cookies.
|
|
|
|
// 2. Get language information from cookies.
|
|
|
|
if len(lang) == 0 { |
|
|
|
if len(lang) == 0 { |
|
|
|
ck, _ := req.Cookie("lang") |
|
|
|
ck, _ := req.Cookie("lang") |
|
|
|
if ck != nil { |
|
|
|
if ck != nil { |
|
|
|
lang = ck.Value |
|
|
|
lang = ck.Value |
|
|
|
hasCookie = true |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Check again in case someone modify by purpose.
|
|
|
|
// Check again in case someone modify by purpose.
|
|
|
|
if lang != "" && !i18n.IsExist(lang) { |
|
|
|
if lang != "" && !i18n.IsExist(lang) { |
|
|
|
lang = "" |
|
|
|
lang = "" |
|
|
|
hasCookie = false |
|
|
|
changeLang = false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 3. Get language information from 'Accept-Language'.
|
|
|
|
// 3. Get language information from 'Accept-Language'.
|
|
|
@ -43,8 +41,8 @@ func Locale(resp http.ResponseWriter, req *http.Request) translation.Locale { |
|
|
|
lang = tag.String() |
|
|
|
lang = tag.String() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !hasCookie { |
|
|
|
if changeLang { |
|
|
|
req.AddCookie(NewCookie("lang", lang, 1<<31-1)) |
|
|
|
SetCookie(resp, "lang", lang, 1<<31-1) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return translation.NewLocale(lang) |
|
|
|
return translation.NewLocale(lang) |
|
|
|