如何用React I18Next翻译formik errormessage
我有以下组件:
import { Formik, Form, useField, ErrorMessage } from "formik";
import * as Yup from "yup";
import Select from "react-select";
const iceCreamOptions = [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
{ value: "vanilla", label: "Vanilla" }
];
const FormSelect = ({ name, options }) => {
const [field, meta, helpers] = useField(name);
return (
<>
<Select
name={name}
value={field.value}
onChange={(value) => helpers.setValue(value)}
options={options}
onBlur={() => helpers.setTouched(true)}
/>
<ErrorMessage name={name} />
</>
);
};
const initialValues = {
icecream: null
};
const validationSchema = Yup.object().shape({
icecream: Yup.object()
.shape({
value: Yup.string(),
label: Yup.string()
})
.required("Please select a value")
.nullable()
});
export default function App() {
return (
<Formik
initialValues={initialValues}
onSubmit={(values) => console.log(values)}
validationSchema={validationSchema}
>
{(props) => {
return (
<Form>
<FormSelect name="icecream" options={iceCreamOptions} />
</Form>
);
}}
</Formik>
);
}
现在,我想在我的应用程序中添加一个板,因此我将使用React I18Next。 基本示例就是这样:
const { t } = useTranslation();
<p>{t('message')}</p>
现在,如何将这些翻译与我的错误组件一起使用? 我确定,我不能这样做:D
t(<ErrorMessage name={name} />)
因此,如果无法使用t
和errormessage
来处理它,我认为我需要做以下操作:
{meta.touched && meta.error ? (
<div className="error">{t(meta.error)}</div>
) : null}
I have following components:
import { Formik, Form, useField, ErrorMessage } from "formik";
import * as Yup from "yup";
import Select from "react-select";
const iceCreamOptions = [
{ value: "chocolate", label: "Chocolate" },
{ value: "strawberry", label: "Strawberry" },
{ value: "vanilla", label: "Vanilla" }
];
const FormSelect = ({ name, options }) => {
const [field, meta, helpers] = useField(name);
return (
<>
<Select
name={name}
value={field.value}
onChange={(value) => helpers.setValue(value)}
options={options}
onBlur={() => helpers.setTouched(true)}
/>
<ErrorMessage name={name} />
</>
);
};
const initialValues = {
icecream: null
};
const validationSchema = Yup.object().shape({
icecream: Yup.object()
.shape({
value: Yup.string(),
label: Yup.string()
})
.required("Please select a value")
.nullable()
});
export default function App() {
return (
<Formik
initialValues={initialValues}
onSubmit={(values) => console.log(values)}
validationSchema={validationSchema}
>
{(props) => {
return (
<Form>
<FormSelect name="icecream" options={iceCreamOptions} />
</Form>
);
}}
</Formik>
);
}
Now I would like to add tranlations to my application, so i will use React i18next.
Basic example is something like that:
const { t } = useTranslation();
<p>{t('message')}</p>
Now, How can i use this translations with my ErrorMessage component?
I am sure, I can't do something like that :D
t(<ErrorMessage name={name} />)
So if there is no way to handle it with t
and ErrorMessage
, I think I need to do following thing:
{meta.touched && meta.error ? (
<div className="error">{t(meta.error)}</div>
) : null}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以像这样使用yup.typeerror()。
You can use Yup.typeError() like this.
我认为您可以直接翻译
verionationschema
这样。并使用它
I think you can directly translate the
validationSchema
like this;and use it
另一种方法是从
for
formik
中使用函数,但是以相同的方式,您必须将
yup
消息更改为翻译钥匙。errormessage-render
然后,在组件主体中
Another approach would be to use the
render
function fromformik
, but in the same way you would have to change theyup
message to be the translation key.ErrorMessage-render
and then, in the component body