trans.go 786 B

1234567891011121314151617181920212223242526272829303132333435
  1. package helpers
  2. import (
  3. "reflect"
  4. "github.com/go-playground/locales/ru"
  5. ut "github.com/go-playground/universal-translator"
  6. "github.com/go-playground/validator/v10"
  7. ruTranslations "github.com/go-playground/validator/v10/translations/ru"
  8. )
  9. var fieldNames = map[string]string{
  10. "Tag": "Тег",
  11. }
  12. func GetDefaultTranslator(v *validator.Validate) (ut.Translator, error) {
  13. var err error
  14. localeRu := ru.New()
  15. uniTrans := ut.New(localeRu, localeRu)
  16. trans, _ := uniTrans.GetTranslator(localeRu.Locale())
  17. if err = ruTranslations.RegisterDefaultTranslations(v, trans); err != nil {
  18. return nil, err
  19. }
  20. v.RegisterTagNameFunc(func(field reflect.StructField) string {
  21. if name, ok := fieldNames[field.Name]; ok {
  22. return name
  23. }
  24. return field.Name
  25. })
  26. return trans, nil
  27. }