New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I translate fieldName? #364
Comments
Hey, @syssam to do this you'll have to register your own translations and either reference an external object to translate, like a map or use the translator already being used; below is an example of the latter. package main
import (
"fmt"
"github.com/go-playground/locales/en_US"
"github.com/go-playground/locales/fr_CA"
"github.com/go-playground/universal-translator"
"gopkg.in/go-playground/validator.v9"
)
func main() {
english := en_US.New()
trans := ut.New(english, english, fr_CA.New())
en, _ := trans.GetTranslator("en")
en.Add("MyField", "Field", false)
fr, _ := trans.GetTranslator("fr_CA")
fr.Add("MyField", "Champ", false)
validate := validator.New()
validate.RegisterTranslation("required", en,
func(ut ut.Translator) error {
return ut.Add("required", "{0} is required", false)
},
func(ut ut.Translator, fe validator.FieldError) string {
fld, _ := ut.T(fe.Field())
t, err := ut.T(fe.Tag(), fld)
if err != nil {
return fe.(error).Error()
}
return t
},
)
validate.RegisterTranslation("required", fr,
func(ut ut.Translator) error {
return ut.Add("required", "{0} est requis", false)
},
func(ut ut.Translator, fe validator.FieldError) string {
fld, _ := ut.T(fe.Field())
t, err := ut.T(fe.Tag(), fld)
if err != nil {
return fe.(error).Error()
}
return t
},
)
s := struct {
MyField *string `validate:"required"`
}{}
errs := validate.Struct(s)
fmt.Println(errs.(validator.ValidationErrors)[0].Translate(en))
fmt.Println(errs.(validator.ValidationErrors)[0].Translate(fr))
} let me know if you have any questions. |
But I am using gopkg.in/go-playground/validator.v9/translations/en", |
According my current understanding, I can using RegisterTagNameFunc to translations the field name, but it will be cause the json key also translated. |
although for example, if you were using this library in an HTTP API the field name could be used as the name of the test field which the javascript need to set an error in. I'm sure there are many other uses also. if you register your own translations, yes you can override the ones in gopkg.in/go-playground/validator.v9/translations/en, you just have to set the override flag to unfortunately to achieve this, there is a little work required, but there's just no way I can make a single set of translations work for everyone's use case, the ones provided are just a general set and why you can register your own for your use case :) This is also the first attempt at integrating translations into this library and I'm still gethering data on how people use them; I know it doesn't help now, but I intend to make it much easier to use translation in the next version. |
um, I have an idea, load a json file, like lang/en/validation.json
The validator default will be load en, provide a function load another lang. |
@syssam it's not a bad idea, just not a direction I want to take the validation library in, I think this functionality should remain in the realm of the translator/translation engine. for example universal-translator already provides similar functionality and handles l10n, which the above example does not account for. by no means does anyone have to use the translation logic that's built in, it's just a convenience for the majority, anyone can use |
Yes, I know universal-translator already provides similar functionality. Sometimes, we need translation from other people, but we do not send a "golang" file, because the translator do not know the program. JSON format is more common and easily to tell the translator how to do it. On the other hand, useris more easily to understand and custom own error message and more normally.I saw many framework i18n also like this. I know you want this package is focus on the validation and user write a golang to translations as they need. But I think more user are hoping you can provide a simple translation with validation and default different language package. It can reduce the user a lot of time. |
Hello! type Person struct {
Name string `json:"name" validate:"required"`
Age int `json:"age" validate:"required,numeric,gte=1"`
} Actual: {
"Person.name": "The name field is required",
"Person.age": "The age field is required"
} Expectation: {
"name": "The name field is required",
"age": "The age field is required"
} Thanks. |
like type Person struct {
Name string `json:"name" validate:"required" trans:"people name"`
Age int `json:"age" validate:"required,numeric,gte=1" trans:"people age"`
} {
"name": "The people name is required",
"age": "The people age is required"
} |
I write a simple validator. else you can follow this sample |
How can I translate fieldName?
The text was updated successfully, but these errors were encountered: