Updates for checking cookie status.

This commit is contained in:
kunw 2016-11-02 18:30:08 +08:00
parent e6b5714f7d
commit 0f553aa4e2
2 changed files with 21 additions and 26 deletions

View File

@ -42,10 +42,12 @@ var mappingLangNames map[string]string
func (b *BaseController) Prepare() {
var lang string
var langHasChanged bool
langRequest := b.GetString("lang")
if langRequest != "" {
lang = langRequest
langHasChanged = true
} else {
langCookie, err := b.Ctx.Request.Cookie("language")
if err != nil {
@ -53,38 +55,31 @@ func (b *BaseController) Prepare() {
}
if langCookie != nil {
lang = langCookie.Value
}
if lang == "" {
sessionLang := b.GetSession("lang")
if sessionLang != nil {
b.SetSession("lang", lang)
lang = sessionLang.(string)
} else {
al := b.Ctx.Request.Header.Get("Accept-Language")
if len(al) > 4 {
al = al[:5] // Only compare first 5 letters.
if i18n.IsExist(al) {
lang = al
}
} else {
al := b.Ctx.Request.Header.Get("Accept-Language")
if len(al) > 4 {
al = al[:5] // Only compare first 5 letters.
if i18n.IsExist(al) {
lang = al
}
}
langHasChanged = true
}
}
if _, exist := supportLanguages[lang]; !exist { //Check if support the request language.
lang = defaultLang //Set default language if not supported.
if langHasChanged {
if _, exist := supportLanguages[lang]; !exist { //Check if support the request language.
lang = defaultLang //Set default language if not supported.
}
cookies := &http.Cookie{
Name: "language",
Value: lang,
HttpOnly: true,
Domain: "/",
}
http.SetCookie(b.Ctx.ResponseWriter, cookies)
}
cookies := &http.Cookie{
Name: "language",
Value: lang,
HttpOnly: true,
Domain: "/",
}
http.SetCookie(b.Ctx.ResponseWriter, cookies)
b.SetSession("lang", lang)
curLang := langType{
Lang: lang,
}

View File

@ -25,7 +25,7 @@
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-harbor-navbar-collapse-1">
<optional-menu version="dev" language="{{ .Lang }}" login-status="//vm.loginStatus//"></optional-menu>
<optional-menu version="{{.Version}}" language="{{.Lang}}" login-status="//vm.loginStatus//"></optional-menu>
<ul class="nav navbar-nav navbar-right">
<li>
<navigation-header></navigation-header>