Example #1
0
func (this *CookieAction) Update() {
	if this.GetString("submit") == "Delete" {
		this.SetCookie(xweb.NewCookie(cookieName, "", -1))
	} else {
		this.SetCookie(xweb.NewCookie(cookieName, this.GetString("cookie"), 0))
	}
	this.Redirect("/", 301)
}
Example #2
0
// setLangVer sets site language version.
func (this *baseAction) setLangVer() bool {
	isNeedRedir := false
	hasCookie := false

	// 1. Check URL arguments.
	lang := this.GetString("lang")

	// 2. Get language information from cookies.
	if len(lang) == 0 {
		cookie, _ := this.GetCookie("lang")
		if cookie != nil {
			lang = cookie.Value
			hasCookie = true
		}
	} else {
		isNeedRedir = true
	}

	// Check again in case someone modify by purpose.
	if !i18n.IsExist(lang) {
		lang = ""
		isNeedRedir = false
		hasCookie = false
	}

	// 3. Get language information from 'Accept-Language'.
	if len(lang) == 0 {
		al := this.Request.Header.Get("Accept-Language")
		if len(al) > 4 {
			al = al[:5] // Only compare first 5 letters.
			if i18n.IsExist(al) {
				lang = al
			}
		}
	}

	// 4. Default language is English.
	if len(lang) == 0 {
		lang = "en-US"
		isNeedRedir = false
	}

	curLang := langType{
		Lang: lang,
	}

	// Save language information in cookies.
	if !hasCookie {
		cookie := xweb.NewCookie("lang", curLang.Lang, 1<<31-1)
		this.SetCookie(cookie)
	}

	restLangs := make([]*langType, 0, len(langTypes)-1)
	for _, v := range langTypes {
		if lang != v.Lang {
			restLangs = append(restLangs, v)
		} else {
			curLang.Name = v.Name
		}
	}

	// Set language properties.
	this.AddTmplVars(&xweb.T{
		"Lang":      curLang.Lang,
		"CurLang":   curLang.Name,
		"RestLangs": restLangs,
	})
	this.Lang = lang

	return isNeedRedir
}