123456789101112131415161718192021222324252627282930313233343536373839 |
- package i18n
- 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": "Тег",
- "Title": "Название",
- "Image": "Картинка",
- "PublishTime": "Дата публикации",
- "Text": "Текст",
- }
- 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
- }
|