This library localize the custom error messages and validation error messages that thrown from application.
$ npm install @dumanargeyazilim/i18n-library
Additionally, ensure you have the following peer dependencies installed in your project:
$ npm install @nestjs/common class-validator class-transformer
This custom i18n library makes localization for custom error messages defined in Xiot Project and makes localization for validation errors that thrown by class-validator.
How it works?
It takes language as parameter from the header for every request made and returns the translated errors in that language. It supports 3 languages :
- tr
- en
- fr
4 languages will be added in future: arabic, chineese, italian and espanol
## Using I18nValidationPipe for Validation Errors
- Import 'I18nErrorHandlingModule' to your root module. (AppModule)
- Use the I18nValidationPipe globally via APP_PIPE as provider in root module.
-
Must use the message property in decorators for localization support for validation errors:
-
Static Translation Key (e.g., 'validation.isEmail') @IsEmail({}, { message: 'validation.isEmail' })
-
Dynamic Constraint Values (e.g., 'validation.length{3,15}') @Length(3, 8, { message: 'validation.length{3,8}' })
## Using Custom Error Messages for Application Add CustomErrorMessageService from the library to your service as readonly. You can call the defined error methods for spesific error and it returns localizaed error message with it's status code.
- this.i18nErrorService.getWrongPasswordOrEmailMessage();