golang validator 提示消息本地化(中英文案例)

validator 提示消息本地化(中英文案例)

validator验证错误时有提示语,可以根据本地语言的不同设置不同的提示语

案例
package validate

import (
    "fmt"
    "github.com/go-playground/locales/en"
    "github.com/go-playground/locales/zh"
    ut "github.com/go-playground/universal-translator"
    "github.com/go-playground/validator/v10"
    en_translations "github.com/go-playground/validator/v10/translations/en"   // 提示语翻译包
    zh_translations "github.com/go-playground/validator/v10/translations/zh"
)

var (
    uni *ut.UniversalTranslator
)


func Translation() {
    // en 英文,zh 中文
    local := "zh"
    en := en.New()
    zh := zh.New()
    //初始化翻译器,并指定支持的语种
    uni = ut.New(en, zh, en)
    // 获取指定语种的翻译器
    trans, _ := uni.GetTranslator(local)

    switch local {
    case "en":
        en_translations.RegisterDefaultTranslations(validate, trans)
    case "zh":
        zh_translations.RegisterDefaultTranslations(validate, trans)
    default:
        en_translations.RegisterDefaultTranslations(validate, trans)
    }

    // 验证validator提示语翻译器
    translateAll(trans)
}

func translateAll(trans ut.Translator) {
    type User struct {
        UserName string `v:"required"`
        Tagline  string `v:"required,lt=10"`
        Tagline2 string `v:"required,gt=1"`
    }
    user := User{
        Tagline:  "kkk is a good boy",
        Tagline2: "",
    }

    err := validate.Struct(user)

    if err != nil {
        errs := err.(validator.ValidationErrors)
        /*
        	// 一次性将所有字段的提示消息翻译出来
        fmt.Println(errs.Translate(trans))
        */
        // 逐条翻译
        for _, e := range errs {
           fmt.Println(e.Translate(trans))
        }
    }

}

测试

PS D:\GIT\gorun\validator-practice> go run main.go
UserName为必填字段
Tagline长度必须小于10个字符
Tagline2为必填字段

重写翻译器
package validate

import (
    "fmt"
    "github.com/go-playground/locales/en"
    "github.com/go-playground/locales/zh"
    ut "github.com/go-playground/universal-translator"
    "github.com/go-playground/validator/v10"
    en_translations "github.com/go-playground/validator/v10/translations/en"   // 提示语翻译包
    zh_translations "github.com/go-playground/validator/v10/translations/zh"
)

var (
    uni *ut.UniversalTranslator
)


func Translation() {
    // en 英文,zh 中文
    local := "zh"
    en := en.New()
    zh := zh.New()
    //初始化翻译器,并指定支持的语种
    uni = ut.New(en, zh, en)
    // 获取指定语种的翻译器
    trans, _ := uni.GetTranslator(local)

    switch local {
    case "en":
        en_translations.RegisterDefaultTranslations(validate, trans)
    case "zh":
        zh_translations.RegisterDefaultTranslations(validate, trans)
    default:
        en_translations.RegisterDefaultTranslations(validate, trans)
    }

    // 重写翻译器
    translateOverride(trans)
}

// 重写翻译器
func translateOverride(trans ut.Translator) {
    local := trans.Locale()
    switch local {
    case "en":
        // 注册翻译函数
        validate.RegisterTranslation("required", trans, func(ut ut.Translator) error {
            return ut.Add("required", "{0} must have a value!--override", true)
        }, func(ut ut.Translator, fe validator.FieldError) string {
            t, _ := ut.T("required", fe.Field())
            return t
        })
    case "zh":
        validate.RegisterTranslation("required", trans, func(ut ut.Translator) error {
            return ut.Add("required", "{0} 为必填字段!--override", true)
        }, func(ut ut.Translator, fe validator.FieldError) string {
            t, _ := ut.T("required", fe.Field())
            return t
        })
    }

    type User struct {
        UserName string `v:"required"`
    }

    var user User

    err := validate.Struct(user)
    if err != nil {
        errs := err.(validator.ValidationErrors)
        for _, e := range errs {
            fmt.Println(e.Translate(trans))
        }
    }

}

测试

UserName 为必填字段!--override

不过感觉一般不用翻译,统一英文就好,省点性能

你可能感兴趣的:(golang,开发语言,后端)