Skip to content
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

🤗 [Question]: How to implement internationalization for parameter validation in V3 #3117

Closed
3 tasks done
layxyer opened this issue Sep 2, 2024 · 5 comments
Closed
3 tasks done

Comments

@layxyer
Copy link

layxyer commented Sep 2, 2024

Question Description

I tried some methods, but the binding validators provided by version 3 lack context information, making it impossible to obtain the user's preferred language information and perform internationalization translations during validation. Although translation can be done in each route, this approach is somewhat repetitive and cumbersome.It would be great if a context parameter could be added to the validator interface, or if there is a better way that I haven't discovered yet.

Code Snippet (optional)

type structValidator struct {
	validate *validator.Validate
}

// Validate needs to implement the Validate method
func (v *structValidator) Validate(out any, c fiber.Ctx) error {
	langs := c.GetReqHeaders()["Accept-Language"]
	lang := "en"
	if len(langs) > 0 {
		lang = langs[0]
	}
	trans, err := getTranslator(lang)
	if err != nil {
		return err
	}
	err = validate.Struct(out)
	i18nError := []string{}
	if err != nil {
		if errs, ok := err.(validator.ValidationErrors); ok {
			for _, e := range errs {
				i18nError = append(i18nError, e.Translate(trans))
			}
		} else {
			fmt.Println(err)
		}
	}
	return errors.New(fmt.Sprintf("%v", i18nError))
}

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have checked for existing issues that describe my questions prior to opening this one.
  • I understand that improperly formatted questions may be closed without explanation.
Copy link

welcome bot commented Sep 2, 2024

Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@gaby
Copy link
Member

gaby commented Sep 2, 2024

@layxyer What do you mean by but the binding validators provided by version 3 lack context information ?

Btw you can use this, to check if en is in the headers.

ctx.AcceptsLanguages("en")

This will return "en", if the request has the value in the Header.

@layxyer
Copy link
Author

layxyer commented Sep 6, 2024

@layxyer你的意思是什么but the binding validators provided by version 3 lack context information

顺便说一句,您可以使用它来检查是否en在标题中。

ctx.AcceptsLanguages("en")

"en"如果请求的 Header 中有值,则将返回。
I mean the validator method does not have a ctx parameter, so it cannot obtain language information.

@layxyer
Copy link
Author

layxyer commented Sep 6, 2024

@layxyer你的意思是but the binding validators provided by version 3 lack context information

顺便说一句,你可以用它来检查是否en在标题中。

ctx.AcceptsLanguages("en")

"en"如果请求的 Header 包含值,则将返回。

I added the ctx parameter to the Validate method in the example code, but in fact, the official method does not include this parameter.

@Skyenought
Copy link
Member

@layxyer 我认为在现在的阶段, 你只能用额外的方法进行参数的校验和替换了

I think at this situatiom, you can only use additional methods for parameter checking and substitution

@layxyer layxyer closed this as completed Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants