How to use my translations in validation messages using zod in sveltekit #3036
-
Hi there, I'm trying to find a solution on how to make translated validation messages work on my forms using zod in my sveltekit project. I think this has to do with the fact that in my schema, I do not have any context of what the current language is. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Can you show code on how you are using messages in zod? |
Beta Was this translation helpful? Give feedback.
Although this does work, it is not the right solution for me. Zod is expecting a string when defining messages directly inside the schema. As well as my error component which is based on the FormErrors component from
formsnap
.I looked into something different, Zod offers a setErrorMap function. I was inspired by an existing solution to translate zod messages for i18next zod-i18n.
I now create and set the global errorMap once in my layout's onMount function. Although not ideal, it works. Do you, from your experience, see any concerns doing it in this way?
I was rather hoping to set it in a earlier stage, like the
hooks.server.ts
file for instance, that didn't work, messages turned undefin…