Сообщения об ошибках RSuite переводятся с использованием схемы rsuite. NextJS i18n

У меня небольшая проблема с обработкой ошибок RSuite и схемы. Прямо сейчас я не могу переводить сообщения об ошибках из схемы, потому что схема написана вне компонента, поэтому я не могу использовать хуки. Можно ли переводить сообщения об ошибках вне компонента?

    const { StringType, DateType } = Schema.Types;
const model = Schema.Model({
    username: StringType().isRequired('FORM.ERR.REQUIRED'),
    password: StringType().isRequired('FORM.ERR.REQUIRED')
});

export default function login() {
    const [formValue, setFormValue] = useState();
    const { t } = useTranslation()
    const form = useRef();

const formSubmit = () => {
    if (form.current.check()) {
        console.log(formValue);
    }
}

return (
    <section className="LOGIN">
        <div className="LOGIN__container">
            <Form
            className="LOGIN__container-form"
                model={model}
                onChange={formValue => setFormValue(formValue)}
                onSubmit={() => formSubmit()}
                ref={form}>
                <FormGroup>
                    <FormGroup>
                        <FormControl placeholder="Username" name="username" style={{width: '100%'}} />
                    </FormGroup>
                    <FormGroup>
                        <FormControl placeholder="Password" name="password" type="password" style={{width: '100%'}} />
                    </FormGroup>
                </FormGroup>
                <ButtonToolbar>
                    <Button appearance="primary" type="submit">
                        {t('COMMON.SUBMIT')}
                    </Button>
                </ButtonToolbar>

            </Form>
        </div>
    </section>
)

} Это весь компонент

1 ответ

конечно можно,но не использовать хуки,я например использую React-intl.

      import { FormattedMessage } from 'react-intl';

export const formatMessage = (id: string, values: BaseObject = {}): any => {
  return <FormattedMessage id={id} values={values} />;
};

вы можете использовать эту функцию для возврата React.ReactNode.like this

      const model = Schema.Model({
    username: StringType().isRequired(formatMessage('FORM.ERR.REQUIRED')),
    password: StringType().isRequired(formatMessage('FORM.ERR.REQUIRED'))
});
Другие вопросы по тегам