Today I am changing the background page. When the parameter verification is wrong, the output is all in English. It is difficult to understand what is wrong when using it.
Therefore, I decided to do the i18n front-end internationalization. I stepped on a lot of pitfalls when I changed, so I recorded it and recorded the following ways to check the problem.
Effect
Change from the original Title is required to the title required field
Completed code:
Here we mainly define the variables that initialize a Chinese trans and Validate and initialize them.
Initialization mainly does the following:
Registered TagName function// RegisterTagNameFunc registers a function to get alternate names for StructFields.
This method mainly provides a tag parser and returns a string replaced by Field
I have defined a label tag for replacement
Registered the translation function of validate
Directly use the original Chinese conversion, and make corresponding internationalization of required tags.
package service import ( zhongwen "/go-playground/locales/zh" ut "/go-playground/universal-translator" "/go-playground/validator/v10" zh_translations "/go-playground/validator/v10/translations/zh" "reflect" "strings" ) var Validate * var trans func init() { zh := () uni := (zh, zh) trans, _ = ("zh") Validate = () (func(field ) string { label := ("label") if label == "" { return } return label }) zh_translations.RegisterDefaultTranslations(Validate, trans) } func Translate(errs ) string { var errList []string for _, e := range errs { // can translate each error one at a time. errList = append(errList,(trans)) } return (errList,"|") }
Call method
type ArticlesPost struct { Title string `json:"title" validate:"required,max=32,min=4" label:"title"` } var ap ArticlePost err = (ap) if err!=nil{ errStr =Translate(errs) (errStr) }
Ideas
- I first went to Baidu to check it out, but it was fruitless
- I checked Iris's documentation, but it was fruitless
- After reading the validate document and finding the universal-translator package, you can initially change the styles such as is required to required fields
- Still can't map the field name into Chinese. Google searched How can I translate fieldName? #364 This issue is given in the comment ("MyField", "Field", false). Finally, when registering required, it is converted to the corresponding Chinese fld, _ := (()). Considering that the fields of Struct must be registered every time, and the same key in the global world must not be able to define different values, deprecate
- The first time I thought about whether the verification itself has provided the corresponding position. After looking at the interface, I had some knowledge and half-understanding in English, but I didn’t find the result, so I gave up.
- Continue, think about whether you can customize the tag, and then rewrite the type TranslationFunc func(ut , fe FieldError) string function. In this translation stage, you can dynamically pass the tag value in the struct so that it will not be repeated.
- After studying the parameter transmission of this function, only the data corresponding to the field is left in the FieldError, and the tag information cannot be obtained. I almost want to give up.
- Study again the validator function about tag
The first is to set a new tag to replace validate, and the other description is to register a method to get the replaced name for the structure field
Look carefully at the instructions, it is indeed this. Let’s take a look at the signature of TagNameFunc. The parameters are, you can get a series of information such as tags.
// TagNameFunc allows for adding of a custom tag name parser type TagNameFunc func(field ) string // SetTagName allows for changing of the default tag name of 'validate' func (v *Validate) SetTagName(name string) { = name } // RegisterTagNameFunc registers a function to get alternate names for StructFields. // // eg. to use the names which have been specified for JSON representations of structs, rather than normal Go field names: // // (func(fld ) string { // name := (("json"), ",", 2)[0] // if name == "-" { // return "" // } // return name // }) func (v *Validate) RegisterTagNameFunc(fn TagNameFunc) { = fn = true }
At this point, the correct solution was finally found
Summarize
Here I found that in order to solve this problem, I took many detours and looked up a lot of information before I found that this function was even provided.
Discovering several problems with myself:
- English is not very good, and occasionally I don’t recognize some words, which prevents further discovery of problems. I suddenly thought that better English can really benefit a lot from learning programming.
- I don’t read the document very carefully. I think most programming problems are not very profound. I can read and understand what the error means. Then I check the document or search engines to solve it. Another thing is that most programming documents are better in English, and detailed things may be omitted when translated.
This is the end of this article about the implementation of golang validator parameter verification. For more related golang validator parameter verification content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!