foxhound87 / mobx-react-form

Reactive MobX Form State Management
https://foxhound87.github.io/mobx-react-form
MIT License
1.09k stars 129 forks source link

Translating form labels with i18n #559

Closed Rvdmaas closed 8 months ago

Rvdmaas commented 3 years ago

I'm trying to translate the form labels with the i18n(v9) t function, however when i try to export my file like this: export default withNamespaces()(UploadForm);

I get an error saying: TypeError: form.values is not a function Is there a valid way to export and translate labels with the t function like this? label: t('test'),

foxhound87 commented 8 months ago

You can use computed values to compute the labels or other props when your app locale changes:

Assume you have a store.locale observable which changes when the user switches language:


const $label = (id: string) => store.locale && t(`form.contact.labels.${id}`)
const $placeholder = (id: string) => store.locale && t(`form.contact.placeholders.${id}`)

export const Form = new Form({
    fields: [
        'email',
    ],
    labels: {
        email: () => $label('email'),
    },
    placeholders: {
        email: () => $placeholder('email'),
    },
})