1234567891011121314151617181920212223242526272829303132333435 |
- package helpers
- import (
- "reflect"
- "github.com/go-playground/locales/ru"
- ut "github.com/go-playground/universal-translator"
- "github.com/go-playground/validator/v10"
- ruTranslations "github.com/go-playground/validator/v10/translations/ru"
- )
- var fieldNames = map[string]string{
- "Tag": "Тег",
- }
- func GetDefaultTranslator(v *validator.Validate) (ut.Translator, error) {
- var err error
- localeRu := ru.New()
- uniTrans := ut.New(localeRu, localeRu)
- trans, _ := uniTrans.GetTranslator(localeRu.Locale())
- if err = ruTranslations.RegisterDefaultTranslations(v, trans); err != nil {
- return nil, err
- }
- v.RegisterTagNameFunc(func(field reflect.StructField) string {
- if name, ok := fieldNames[field.Name]; ok {
- return name
- }
- return field.Name
- })
- return trans, nil
- }
|