Example #1
0
// 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
}
Example #2
0
File: form.go Project: zeuson/wego
func (form *UserAdminForm) Valid(v *validation.Validation) {
	if exist, _ := models.IsUserExistByName(form.UserName, int64(form.Id)); exist {
		v.SetError("UserName", "auth.username_already_taken")
	}

	if exist, _ := models.IsUserExistByEmail(form.Email, int64(form.Id)); exist {
		v.SetError("Email", "auth.email_already_taken")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}
}
Example #3
0
func (form *PostForm) Valid(v *validation.Validation) {
	valid := false
	for _, topic := range form.Topics {
		if topic.Id == form.Topic {
			valid = true
		}
	}

	if !valid {
		v.SetError("Topic", "error")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "error")
	}
}
Example #4
0
func (this *BaseRouter) LoginUser(user *models.User, remember bool) string {
	ck := this.Cookies().Get("login_to")
	var loginRedirect string
	if ck != nil {
		loginRedirect = strings.TrimSpace(ck.Value)
	}
	if !utils.IsMatchHost(loginRedirect) {
		loginRedirect = "/"
	} else {
		auth.SetCookie(this, "login_to", "", -1, "/")
	}

	// login user
	auth.LoginUser(user, this.Context, &this.Session, remember)

	this.setLangCookie(i18n.GetLangByIndex(user.Lang))

	return loginRedirect
}
Example #5
0
func (form *PostAdminForm) Valid(v *validation.Validation) {
	var err error
	if _, err = models.GetUserById(form.User); err != nil {
		v.SetError("User", "admin.not_found_by_id")
	}

	if _, err = models.GetUserById(form.LastReply); err != nil {
		v.SetError("LastReply", "admin.not_found_by_id")
	}

	if _, err = models.GetUserById(form.LastAuthor); err != nil {
		v.SetError("LastReply", "admin.not_found_by_id")
	}

	if _, err = models.GetTopicById(form.Topic); err != nil {
		v.SetError("Topic", "admin.not_found_by_id")
	}

	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Not Found")
	}
}
Example #6
0
File: form.go Project: zeuson/wego
func (form *ProfileForm) Valid(v *validation.Validation) {
	if len(i18n.GetLangByIndex(form.Lang)) == 0 {
		v.SetError("Lang", "Can not be empty")
	}
}
Example #7
0
// 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, "?")])
		}
	}
}
Example #8
0
func (m *Post) GetLang() string {
	return i18n.GetLangByIndex(m.Lang)
}
Example #9
0
// 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
}