RHF + yup + i18next: anyone? #3808
-
Has anyone tried internationalization of validation messages using yup + i18next? Yup provides a way to do this using setLocale(), however it doesn't seem to work with RHF. For example, I have used
However, RHF seems to ignore it completely and I still get validation messages like "Email is a required field" instead of "fieldRequired" which I can send to i18next for translation. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 9 replies
-
Don't think that works out the box right now, you may need to use custom resolver. @jorisre I think this is something we can improve on the next major version to consider i18. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I finally got this working! You can try a live demo here. The source repo is here. Interestingly enough, I didn't have to write a custom resolver. The big breakthrough came from my colleague @jamesxliu, who realized that we shouldn't be creating yup schemas statically. So instead of Once this started working, we made bunch of other tweaks to make parameterized validations work e.g. min/max/invalid type etc. Here are some of the interesting files to look at:
@bluebill1049, @jorisre, thank you for getting me started on this journey. If you have any suggestions for improvements, please let me know. Other than that, hope this example helps others who need to address i18n. |
Beta Was this translation helpful? Give feedback.
-
@bluebill1049 was there any work since this discussion that could make using i18next react + yup + RHF easier? |
Beta Was this translation helpful? Give feedback.
I finally got this working! You can try a live demo here. The source repo is here.
Interestingly enough, I didn't have to write a custom resolver. The big breakthrough came from my colleague @jamesxliu, who realized that we shouldn't be creating yup schemas statically. So instead of
const addressSchema = yup.object().shape(...)
we started creating it using a functionconst getAddressSchema = () => (yup.object().shape(...))
. This allowed yup to create the schema based on the locale that was set usingyup.setLocale()
.Once this started working, we made bunch of other tweaks to make parameterized validations work e.g. min/max/invalid type etc. Here are some of the interesting files to look at: