Translated error messages

This commit is contained in:
Przemek Wiech
2020-09-12 20:04:42 +02:00
parent a886230305
commit 13b147f082
9 changed files with 68 additions and 9 deletions

19
src/util/error_i18n.ts Normal file
View File

@@ -0,0 +1,19 @@
import {InjectedIntl} from 'react-intl';
import {TopolaError} from './error';
/**
* Returns a translated message for the given error. If the message can't be
* translated, the original error.message is returned.
*/
export function getI18nMessage(error: Error, intl: InjectedIntl): string {
if (!(error instanceof TopolaError)) {
return error.message;
}
return intl.formatMessage(
{
id: `error.${error.code}`,
defaultMessage: error.message,
},
error.args,
);
}