zh_cn.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package unity
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin/binding"
  5. "github.com/go-playground/locales/en"
  6. "github.com/go-playground/locales/zh"
  7. ut "github.com/go-playground/universal-translator"
  8. "github.com/go-playground/validator/v10"
  9. enTranslations "github.com/go-playground/validator/v10/translations/en"
  10. chTranslations "github.com/go-playground/validator/v10/translations/zh"
  11. )
  12. var Trans ut.Translator
  13. func Translator(local string) (err error) {
  14. if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
  15. zhT := zh.New()
  16. enT := en.New()
  17. uni := ut.New(enT, zhT, enT)
  18. var o bool
  19. Trans, o = uni.GetTranslator(local)
  20. if !o {
  21. return fmt.Errorf("uni.GetTranslator(%s) failed", local)
  22. }
  23. //register translate
  24. // 注册翻译器
  25. switch local {
  26. case "en":
  27. err = enTranslations.RegisterDefaultTranslations(v, Trans)
  28. case "zh":
  29. err = chTranslations.RegisterDefaultTranslations(v, Trans)
  30. default:
  31. err = enTranslations.RegisterDefaultTranslations(v, Trans)
  32. }
  33. return
  34. }
  35. return
  36. }