如何使用 React、React Intl 和 Redux 翻译 (i18n) 操作中的错误消息?
How to translate (i18n) error messages in actions with React, React Intl and Redux?
我有一个 performLogin
操作,它会触发 API 并尝试使用提供的凭据对用户进行身份验证。此 API 调用可能会导致错误。她是我的行动:
export const performLogin = (email: string, password: string) => {
return (dispatch) => {
UsersAPI.login(email, password).then(response => {
// [..]
}).catch(error => {
const definedError: Error = defaultError;
switch (error.response.status) {
case 401:
definedError.message = 'Invalid email or password';
break;
default:
definedError.message = error.message;
}
dispatch(displayError(definedError));
});
};
};
我的问题是,如何国际化 Invalid email or password
?
我正在使用 React Intl,所以我在组件中有 i18n 的基础结构。我的想法是定义一个错误类型,在操作中设置它,让登录组件或全局错误组件决定应该显示哪个错误。
React-Redux 的方法是什么?
您为什么要尝试翻译正在运行的消息?只需将错误密钥保存在商店中并将其传递给 props
到 component
并在渲染时使用 FormattedMessage
翻译它。
我有一个 performLogin
操作,它会触发 API 并尝试使用提供的凭据对用户进行身份验证。此 API 调用可能会导致错误。她是我的行动:
export const performLogin = (email: string, password: string) => {
return (dispatch) => {
UsersAPI.login(email, password).then(response => {
// [..]
}).catch(error => {
const definedError: Error = defaultError;
switch (error.response.status) {
case 401:
definedError.message = 'Invalid email or password';
break;
default:
definedError.message = error.message;
}
dispatch(displayError(definedError));
});
};
};
我的问题是,如何国际化 Invalid email or password
?
我正在使用 React Intl,所以我在组件中有 i18n 的基础结构。我的想法是定义一个错误类型,在操作中设置它,让登录组件或全局错误组件决定应该显示哪个错误。
React-Redux 的方法是什么?
您为什么要尝试翻译正在运行的消息?只需将错误密钥保存在商店中并将其传递给 props
到 component
并在渲染时使用 FormattedMessage
翻译它。