question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

yup setLocale with i18n (and Formik)

See original GitHub issue

I’m having some issues with setLocale and i18n (i’m using i18next, but that is not really relevant in this case) in the context of Formik forms.

I’m using a setLocale configuration like below:

setLocale({
        mixed: {
            default: 'forms.validation.invalid',
            required: 'forms.validation.required'
        },
        string: {
            min: ({ min, ...other }) => ({ transKey: 'forms.validation.minLength', values: { min } }),
            max: ({ max }) => ({ transKey: 'forms.validation.maxLength', values: { max } })
        }
    });

Validations work fine, yup setLocale is doing it’s thing and returning strings and objects that are perfectly translatable. My point is where exactly do the translation.

My first attempt was translating the validation errors inside the form render, but that gives all kinds of issues (like detecting which validation message is already translated) and making the translated errors be used by the form.

Now I think I solved it by doing the translation inside setLocale. So instead of returning translation keys and objects with translation keys and other data (for interpolation purposes) I do it like this:

setLocale({
        mixed: {
            default: i18n.t('forms.validation.invalid'),
            required: i18n.t('forms.validation.required')
        },
        string: {
            min: ({ min }) => i18n.t('forms.validation.minLenght', { min }),
            max: ({ max }) => i18n.t('forms.validation.maxLength', { max })
        }
    });

This seems to work fine, validation messages are translated and ready to be used inside formik so I guess this is a nice way of doing translations, but I’m wondering if there are any downsides to this approach because most info about i18n with yup seem to be handling translations inside the component itself.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

9reactions
lgenzeliscommented, Aug 31, 2021

For anyone finding this now, the list of possible values can be seen at https://github.com/jquense/yup/blob/master/src/locale.ts

6reactions
pleunvcommented, Mar 7, 2021

The problem with that approach is that you’re loading most of yup at bootstrap time, while validation is something you generally only need in very specific input scenarios and shouldn’t be in your main application bundle. Generally I think it’d be better to hook the loading of yup locales/translations to some sort of reused form component so they can be loaded on-demand.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to internationalize a Yup validation schema in ... - Medium
In the CodeSandbox below, we have a basic setup of React, react-i18next, Formik and Yup. We display a form with a required email...
Read more >
Yup doesn't work properly with i18n - Stack Overflow
A solution will be to make a function that returns your validation schema. Then call that function in your component with the result...
Read more >
How to internationalize a Yup validation ... - DEV Community ‍ ‍
In the StackBlitz below, we have a basic setup of React, react-i18next, Formik and Yup. We display a form with a required email...
Read more >
react--18next, Yup and Formik basic example - CodeSandbox
An example form built with Formik and React.
Read more >
A brand new website interface for an even better experience!
yup setLocale with i18n (and Formik )
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found