本文整理匯總了Golang中github.com/beego/i18n.GetLangByIndex函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetLangByIndex函數的具體用法?Golang GetLangByIndex怎麽用?Golang GetLangByIndex使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetLangByIndex函數的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Valid
func (form *UserAdminForm) Valid(v *validation.Validation) {
qs := models.Users()
if models.CheckIsExist(qs, "UserName", form.UserName, form.Id) {
v.SetError("UserName", "auth.username_already_taken")
}
if models.CheckIsExist(qs, "Email", form.Email, form.Id) {
v.SetError("Email", "auth.email_already_taken")
}
if len(i18n.GetLangByIndex(form.Lang)) == 0 {
v.SetError("Lang", "Can not be empty")
}
if len(form.LangAdds) > 0 {
adds := make(models.SliceStringField, 0, len(form.LangAdds))
for _, l := range form.LangAdds {
if d, err := utils.StrTo(l).Int(); err == nil {
if form.Lang == d {
continue
}
if len(i18n.GetLangByIndex(form.Lang)) == 0 {
v.SetError("Lang", "Can not be empty")
return
}
adds = append(adds, l)
}
}
form.LangAdds = adds
}
}
示例2: Valid
func (form *PostAdminForm) Valid(v *validation.Validation) {
user := models.User{Id: form.User}
if user.Read() != nil {
v.SetError("User", "admin.not_found_by_id")
}
user.Id = form.LastReply
if user.Read() != nil {
v.SetError("LastReply", "admin.not_found_by_id")
}
user.Id = form.LastAuthor
if user.Read() != nil {
v.SetError("LastReply", "admin.not_found_by_id")
}
topic := models.Topic{Id: form.Topic}
if topic.Read() != nil {
v.SetError("Topic", "admin.not_found_by_id")
}
if len(i18n.GetLangByIndex(form.Lang)) == 0 {
v.SetError("Lang", "Not Found")
}
}
示例3: setLang
// setLang sets site language version.
func (this *baseRouter) setLang() bool {
isNeedRedir := false
hasCookie := false
// get all lang names from i18n
langs := utils.Langs
// 1. Check URL arguments.
lang := this.GetString("lang")
// 2. Get language information from cookies.
if len(lang) == 0 {
lang = this.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. 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.Ctx.Input.Header("Accept-Language")
if len(al) > 4 {
al = al[:5] // Only compare first 5 letters.
if i18n.IsExist(al) {
lang = al
}
}
}
// 4. DefaucurLang language is English.
if len(lang) == 0 {
lang = "en-US"
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
}
示例4: Valid
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")
}
valid = false
for _, cat := range form.Categories {
if cat.Id == form.Category {
valid = true
}
}
if !valid {
v.SetError("Category", "error")
}
if len(i18n.GetLangByIndex(form.Lang)) == 0 {
v.SetError("Lang", "error")
}
}
示例5: Valid
func (model *UserAdminModel) Valid(v *validation.Validation) {
qs := userServ.UserService{}.Queryable()
if services.CheckIsExist(qs, "user_name", model.Username, model.Id) {
v.SetError("Username", "auth.username_already_taken")
}
if services.CheckIsExist(qs, "email", model.Email, model.Id) {
v.SetError("Email", "auth.email_already_taken")
}
if len(i18n.GetLangByIndex(model.Lang)) == 0 {
v.SetError("Lang", "Can not be empty")
}
//if len(model.LangAdds) > 0 {
// adds := make(models.SliceStringField, 0, len(model.LangAdds))
// for _, l := range model.LangAdds {
// if d, err := utils.StrTo(l).Int(); err == nil {
// if model.Lang == d {
// continue
// }
// if len(i18n.GetLangByIndex(model.Lang)) == 0 {
// v.SetError("Lang", "Can not be empty")
// return
// }
// adds = append(adds, l)
// }
// }
// model.LangAdds = adds
//}
}
示例6: LoginUser
func (this *BaseRouter) LoginUser(user *models.User, remember bool) string {
loginRedirect := strings.TrimSpace(this.Ctx.GetCookie("login_to"))
if utils.IsMatchHost(loginRedirect) == false {
loginRedirect = "/"
} else {
this.Ctx.SetCookie("login_to", "", -1, "/")
}
// login user
auth.LoginUser(user, this.Ctx, remember)
this.setLangCookie(i18n.GetLangByIndex(user.Lang))
return loginRedirect
}
示例7: Valid
func (form *UserAdminForm) Valid(v *validation.Validation) {
qs := models.Users()
if models.CheckIsExist(qs, "UserName", form.UserName, form.Id) {
v.SetError("UserName", "auth.username_already_taken")
}
if models.CheckIsExist(qs, "Email", form.Email, form.Id) {
v.SetError("Email", "auth.email_already_taken")
}
if len(i18n.GetLangByIndex(form.Lang)) == 0 {
v.SetError("Lang", "Can not be empty")
}
}
示例8: setLang
func (this *BaseController) setLang() {
// get all lang names from i18n
langs := models.Langs
// Get language information from cookies.
lang := this.Ctx.GetCookie("lang")
// Check again in case someone modify by purpose.
if !i18n.IsExist(lang) {
lang = ""
}
// check if isLogin then use user setting
if len(lang) == 0 && this.IsLogin {
lang = i18n.GetLangByIndex(this.User.Lang)
}
// Get language information from 'Accept-Language'.
if len(lang) == 0 {
al := this.Ctx.Input.Header("Accept-Language")
if len(al) > 4 {
al = al[:5] // Only compare first 5 letters.
if i18n.IsExist(al) {
lang = al
}
}
}
// Default language is English.
if len(lang) == 0 {
lang = "en-US"
}
// Save language information in cookies.
// this.Ctx.SetCookie("lang", lang, 60*60*24*365, "/", nil, nil, false)
// Set language properties.
this.Locale.Lang = lang
this.Data["Lang"] = lang
this.Data["Langs"] = langs
}
示例9: Login
// Login implemented user login.
func (this *LoginRouter) Login() {
this.Data["IsLoginPage"] = true
this.TplNames = "auth/login.html"
// no need login
if this.CheckLoginRedirect(false) {
return
}
var user models.User
var key string
ajaxErrMsg := "auth.login_error_ajax"
form := models.LoginForm{}
// valid form and put errors to template context
if this.ValidFormSets(&form) == false {
if this.IsAjax() {
goto ajaxError
}
return
}
key = "auth.login." + form.UserName + this.Ctx.Input.IP()
if times, ok := utils.TimesReachedTest(key, utils.LoginMaxRetries); ok {
if this.IsAjax() {
ajaxErrMsg = "auth.login_error_times_reached"
goto ajaxError
}
this.Data["ErrorReached"] = true
} else if models.VerifyUser(&user, form.UserName, form.Password) {
loginRedirect := strings.TrimSpace(this.Ctx.GetCookie("login_to"))
if utils.IsMatchHost(loginRedirect) == false {
loginRedirect = "/"
} else {
this.Ctx.SetCookie("login_to", "", -1, "/")
}
// login user
models.LoginUser(&user, &this.Controller, form.Remember)
this.setLangCookie(i18n.GetLangByIndex(user.Lang))
if this.IsAjax() {
this.Data["json"] = map[string]interface{}{
"success": true,
"message": this.Tr("auth.login_success_ajax"),
"redirect": loginRedirect,
}
this.ServeJson()
return
}
this.Redirect(loginRedirect, 302)
return
} else {
utils.TimesReachedSet(key, times, utils.LoginFailedBlocks)
if this.IsAjax() {
goto ajaxError
}
}
this.Data["Error"] = true
return
ajaxError:
this.Data["json"] = map[string]interface{}{
"success": false,
"message": this.Tr(ajaxErrMsg),
"once": this.Data["once_token"],
}
this.ServeJson()
}
示例10: GetLang
func (m *Post) GetLang() string {
return i18n.GetLangByIndex(m.Lang)
}
示例11: Login
// Login implemented POST user login.
func (this *LoginRouter) Login() {
this.Data["IsLoginPage"] = true
this.TplNames = "auth/login.html"
// no need login
if this.CheckLoginRedirect(false) {
return
}
var user models.User
form := models.LoginForm{Locale: this.Locale}
// valid form and put errors to template context
if this.ValidFormSets(&form) == false {
if this.IsAjax() {
goto ajaxError
}
return
}
if models.VerifyUser(&user, form.Email, form.Password) {
loginRedirect := strings.TrimSpace(this.Ctx.GetCookie("login_to"))
if utils.IsMatchHost(loginRedirect) == false {
loginRedirect = "/home"
} else {
this.Ctx.SetCookie("login_to", "", -1, "/")
}
// login user
models.LoginUser(&user, &this.Controller, form.Remember)
this.setLangCookie(i18n.GetLangByIndex(user.Lang))
if this.IsAjax() {
this.Data["json"] = map[string]interface{}{
"success": true,
"message": this.Tr("auth.login_success_ajax"),
"redirect": loginRedirect,
}
this.ServeJson()
return
}
this.Redirect(loginRedirect, 302)
return
} else {
if this.IsAjax() {
goto ajaxError
}
}
this.Data["Error"] = true
return
ajaxError:
this.Data["json"] = map[string]interface{}{
"success": false,
"message": this.Tr("auth.login_error_ajax"),
"once": this.Data["once_token"],
}
this.ServeJson()
}