You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.1 KiB
130 lines
3.1 KiB
8 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
8 years ago
|
"github.com/go-playground/locales/en"
|
||
8 years ago
|
ut "github.com/go-playground/universal-translator"
|
||
8 years ago
|
"gopkg.in/go-playground/validator.v9"
|
||
8 years ago
|
en_translations "gopkg.in/go-playground/validator.v9/translations/en"
|
||
8 years ago
|
)
|
||
|
|
||
|
// User contains user information
|
||
|
type User struct {
|
||
|
FirstName string `validate:"required"`
|
||
|
LastName string `validate:"required"`
|
||
|
Age uint8 `validate:"gte=0,lte=130"`
|
||
|
Email string `validate:"required,email"`
|
||
|
FavouriteColor string `validate:"iscolor"` // alias for 'hexcolor|rgb|rgba|hsl|hsla'
|
||
|
Addresses []*Address `validate:"required,dive,required"` // a person can have a home and cottage...
|
||
|
}
|
||
|
|
||
|
// Address houses a users address information
|
||
|
type Address struct {
|
||
|
Street string `validate:"required"`
|
||
|
City string `validate:"required"`
|
||
|
Planet string `validate:"required"`
|
||
|
Phone string `validate:"required"`
|
||
|
}
|
||
|
|
||
8 years ago
|
// use a single instance , it caches struct info
|
||
|
var (
|
||
|
uni *ut.UniversalTranslator
|
||
|
validate *validator.Validate
|
||
|
)
|
||
8 years ago
|
|
||
|
func main() {
|
||
|
|
||
8 years ago
|
// NOTE: ommitting allot of error checking for brevity
|
||
|
|
||
|
en := en.New()
|
||
|
uni = ut.New(en, en)
|
||
|
|
||
|
// this is usually know or extracted from http 'Accept-Language' header
|
||
|
// also see uni.FindTranslator(...)
|
||
|
trans, _ := uni.GetTranslator("en")
|
||
|
|
||
8 years ago
|
validate = validator.New()
|
||
8 years ago
|
en_translations.RegisterDefaultTranslations(validate, trans)
|
||
8 years ago
|
|
||
8 years ago
|
translateAll(trans)
|
||
|
translateIndividual(trans)
|
||
|
translateOverride(trans) // yep you can specify your own in whatever locale you want!
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func translateAll(trans ut.Translator) {
|
||
8 years ago
|
|
||
8 years ago
|
type User struct {
|
||
|
Username string `validate:"required"`
|
||
|
Tagline string `validate:"required,lt=10"`
|
||
|
Tagline2 string `validate:"required,gt=1"`
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
user := User{
|
||
|
Username: "Joeybloggs",
|
||
|
Tagline: "This tagline is way too long.",
|
||
|
Tagline2: "1",
|
||
8 years ago
|
}
|
||
|
|
||
|
err := validate.Struct(user)
|
||
|
if err != nil {
|
||
|
|
||
8 years ago
|
// translate all error at once
|
||
|
errs := err.(validator.ValidationErrors)
|
||
8 years ago
|
|
||
8 years ago
|
// returns a map with key = namespace & value = translated error
|
||
|
// NOTICE: 2 errors are returned and you'll see something surprising
|
||
|
// translations are i18n aware!!!!
|
||
|
// eg. '10 characters' vs '1 character'
|
||
|
fmt.Println(errs.Translate(trans))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func translateIndividual(trans ut.Translator) {
|
||
8 years ago
|
|
||
8 years ago
|
type User struct {
|
||
|
Username string `validate:"required"`
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
var user User
|
||
|
|
||
|
err := validate.Struct(user)
|
||
|
if err != nil {
|
||
|
|
||
|
errs := err.(validator.ValidationErrors)
|
||
|
|
||
|
for _, e := range errs {
|
||
|
// can translate each error one at a time.
|
||
|
fmt.Println(e.Translate(trans))
|
||
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
func translateOverride(trans ut.Translator) {
|
||
8 years ago
|
|
||
8 years ago
|
validate.RegisterTranslation("required", trans, func(ut ut.Translator) error {
|
||
|
return ut.Add("required", "{0} must have a value!", true) // see universal-translator for details
|
||
|
}, func(ut ut.Translator, fe validator.FieldError) string {
|
||
|
t, _ := ut.T("required", fe.Field())
|
||
8 years ago
|
|
||
8 years ago
|
return t
|
||
|
})
|
||
8 years ago
|
|
||
8 years ago
|
type User struct {
|
||
|
Username string `validate:"required"`
|
||
8 years ago
|
}
|
||
|
|
||
8 years ago
|
var user User
|
||
|
|
||
|
err := validate.Struct(user)
|
||
|
if err != nil {
|
||
|
|
||
|
errs := err.(validator.ValidationErrors)
|
||
|
|
||
|
for _, e := range errs {
|
||
|
// can translate each error one at a time.
|
||
|
fmt.Println(e.Translate(trans))
|
||
|
}
|
||
|
}
|
||
8 years ago
|
}
|