Skip to content

What is the best way to translate validation messages in a schema in React (with react-hook-form) #130

Answered by ivanhofer
Sti2nd asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @Sti2nd,
react is always a bit tricky when you want to share something outside the react-codebase.
I'm no react expert, so I don't think I can give you the "best" approach how to handle this.
I probably would use it like this:

import type { TranslationFunctions } from 'src/i18n/i18n-types'

const getFormSchema = (LL: TranslationFunctions) => z.object({
   name: z.string().min(1, LL.required()),
});

export default function CreateWishList() {
   const { LL } = useContext(I18nContext)
   
   let register

   useEffect(() => {
      register = useForm({ resolver: zodResolver(getFormSchema(LL)) }).register
   }, [LL])

   // Rest of component here, such as the form
}

I havent tested it, I …

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@Sti2nd
Comment options

@ivanhofer
Comment options

@Sti2nd
Comment options

@ivanhofer
Comment options

Answer selected by Sti2nd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants