-
-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Translation of Validation Error Messages #441
Comments
There is no built in way to do this. But the errors are exported and can be caught and re-thrown with a translated message: import { SwissQRBill } from "swissqrbill/pdf";
import { ValidationError, ValidationErrors } from "swissqrbill/errors";
const TRANSLATED_ERRORS_DE: Record<ValidationErrors, string> = {
[ValidationErrors.AMOUNT_LENGTH_IS_INVALID]: "Der Betrag darf maximal 12 Ziffern enthalten",
//...
};
try {
const qrBill = new SwissQRBill(/* data */);
//...
} catch(error) {
if(error instanceof ValidationError){
throw new Error(TRANSLATED_ERRORS_DE[error.message]);
}
} What bothers me with that solution is that the error message currently has to be used for the mapping. I think the best solution would be to add the enum key as an |
I agree, having an enum key would be nice! |
This is now implemented in v4.1.0 and can be used like this: import { SwissQRBill } from "swissqrbill/pdf";
import { ValidationError, ValidationErrors } from "swissqrbill/errors";
const TRANSLATED_ERRORS_DE: Record<ValidationError["code"], string> = {
AMOUNT_LENGTH_IS_INVALID: "Der Betrag darf maximal 12 Ziffern enthalten",
//...
};
try {
const qrBill = new SwissQRBill(/* data */);
//...
} catch(error) {
if(error instanceof ValidationError){
throw new Error(TRANSLATED_ERRORS_DE[error.code]));
}
} |
Nice, thank you!! |
Is there a way to translate the error messages?
We use SwissQRCode to generate a QR Code based on dynamic formular data - and it would be nice to provide the user with information about which validation failed in their language.
The text was updated successfully, but these errors were encountered: