// get language from context func getContextLanguage(ctx *tango.Context) string { // get from cookie lang := ctx.Cookie(langCookieName) // get from header if lang == "" { al := ctx.Req().Header.Get("Accept-Language") if len(al) > 4 { lang = al[:5] // Only compare first 5 letters. } } // get from query param if lang == "" { lang = ctx.Param(langParamName) } // get default if not find in context lang = strings.ToLower(lang) if !i18n.IsExist(lang) { lang = i18n.GetLangByIndex(0) } return lang }
func (r *RenderBase) CurLang() string { al := r.Req().Header.Get("Accept-Language") if len(al) > 4 { al = al[:5] // Only compare first 5 letters. if i18n.IsExist(formatLang(al)) { return formatLang(al) } } return "en-US" }
// I18n is a middleware provides localization layer for your application. // Paramenter langs must be in the form of "en-US", "zh-CN", etc. // Otherwise it may not recognize browser input. func I18n(options ...Options) macaron.Handler { opt := prepareOptions(options) initLocales(opt) return func(ctx *macaron.Context) { isNeedRedir := false hasCookie := false // 1. Check URL arguments. lang := ctx.Query(opt.Parameter) // 2. Get language information from cookies. if len(lang) == 0 { lang = ctx.GetCookie("lang") 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 := ctx.Req.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 the first element in the list. if len(lang) == 0 { lang = i18n.GetLangByIndex(0) isNeedRedir = false } curLang := LangType{ Lang: lang, } // Save language information in cookies. if !hasCookie { ctx.SetCookie("lang", curLang.Lang, 1<<31-1, "/"+strings.TrimPrefix(opt.SubURL, "/")) } restLangs := make([]LangType, 0, i18n.Count()-1) langs := i18n.ListLangs() names := i18n.ListLangDescs() for i, v := range langs { if lang != v { restLangs = append(restLangs, LangType{v, names[i]}) } else { curLang.Name = names[i] } } // Set language properties. locale := Locale{i18n.Locale{lang}} ctx.Map(locale) ctx.Locale = locale ctx.Data[opt.TmplName] = locale ctx.Data["Tr"] = i18n.Tr ctx.Data["Lang"] = locale.Lang ctx.Data["LangName"] = curLang.Name ctx.Data["AllLangs"] = append([]LangType{curLang}, restLangs...) ctx.Data["RestLangs"] = restLangs if opt.Redirect && isNeedRedir { ctx.Redirect(opt.SubURL + ctx.Req.RequestURI[:strings.Index(ctx.Req.RequestURI, "?")]) } } }
// 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 }
// setLang sets site language version. func (this *BaseRouter) setLang() bool { isNeedRedir := false hasCookie := false // get all lang names from i18n langs := setting.Langs // 1. Check URL arguments. lang := this.Req().FormValue("lang") // 2. Get language information from cookies. if len(lang) == 0 { lang = auth.GetCookie(this.Req(), "lang") hasCookie = true } else { isNeedRedir = true } // Check again in case someone modify by purpose. if !i18n.IsExist(lang) { lang = "" isNeedRedir = false hasCookie = false } // 3. check if isLogin then use user setting if len(lang) == 0 && this.IsLogin { lang = i18n.GetLangByIndex(this.User.Lang) } // 4. Get language information from 'Accept-Language'. if len(lang) == 0 { al := this.Header().Get("Accept-Language") if len(al) > 4 { al = al[:5] // Only compare first 5 letters. if i18n.IsExist(al) { lang = al } } } // 4. DefaultLang language is Chinese. if len(lang) == 0 { //lang = "en-US" lang = "zh-CN" isNeedRedir = false } // Save language information in cookies. if !hasCookie { this.setLangCookie(lang) } // Set language properties. this.Data["Lang"] = lang this.Data["Langs"] = langs this.Lang = lang return isNeedRedir }