manual translator function registration

pull/1079/head
Rangel Reale 2 years ago
parent 8f07b03682
commit 8b0d4fa1e1
  1. 32
      translations/en/en.go
  2. 21
      validator_instance.go

@ -16,6 +16,19 @@ import (
// RegisterDefaultTranslations registers a set of default translations
// for all built in tag's in validator; you may add your own as desired.
func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (err error) {
tf, err := RegisterDefaultTranslationsFunc(trans)
if err != nil {
return err
}
return v.RegisterTranslationsFunc(trans, tf)
}
// RegisterDefaultTranslationsFunc registers a set of default translations
// for all built in tag's in validator; you may add your own as desired.
// This version returns a map[tag]validator.TranslationFunc, which can be set on a validator using
// validator.Validate.RegisterTranslationsFunc.
func RegisterDefaultTranslationsFunc(trans ut.Translator) (map[string]validator.TranslationFunc, error) {
translations := []struct {
tag string
translation string
@ -1363,24 +1376,31 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
},
}
var err error
ret := map[string]validator.TranslationFunc{}
for _, t := range translations {
if t.customTransFunc != nil && t.customRegisFunc != nil {
err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, t.customTransFunc)
ret[t.tag] = t.customTransFunc
err = t.customRegisFunc(trans)
} else if t.customTransFunc != nil && t.customRegisFunc == nil {
err = v.RegisterTranslation(t.tag, trans, registrationFunc(t.tag, t.translation, t.override), t.customTransFunc)
ret[t.tag] = t.customTransFunc
err = registrationFunc(t.tag, t.translation, t.override)(trans)
} else if t.customTransFunc == nil && t.customRegisFunc != nil {
err = v.RegisterTranslation(t.tag, trans, t.customRegisFunc, translateFunc)
ret[t.tag] = translateFunc
err = t.customRegisFunc(trans)
} else {
err = v.RegisterTranslation(t.tag, trans, registrationFunc(t.tag, t.translation, t.override), translateFunc)
ret[t.tag] = translateFunc
err = registrationFunc(t.tag, t.translation, t.override)(trans)
}
if err != nil {
return
return nil, err
}
}
return
return ret, nil
}
func registrationFunc(tag string, translation string, override bool) validator.RegisterTranslationsFunc {

@ -350,6 +350,27 @@ func (v *Validate) RegisterTranslation(tag string, trans ut.Translator, register
return
}
// RegisterTranslationsFunc registers translations against the provided tag.
// This assumes that the tag translations have already been added to the Translator.
func (v *Validate) RegisterTranslationsFunc(trans ut.Translator, translationsFn map[string]TranslationFunc) (err error) {
if v.transTagFunc == nil {
v.transTagFunc = make(map[ut.Translator]map[string]TranslationFunc)
}
m, ok := v.transTagFunc[trans]
if !ok {
m = make(map[string]TranslationFunc)
v.transTagFunc[trans] = m
}
for tag, fn := range translationsFn {
m[tag] = fn
}
return
}
// Struct validates a structs exposed fields, and automatically validates nested structs, unless otherwise specified.
//
// It returns InvalidValidationError for bad values passed in and nil or ValidationErrors as error otherwise.

Loading…
Cancel
Save