@dumanargeyazilim/i18n-library
v1.3.17
Published
i18n error handling for custom error messages and validation messages
Downloads
10,491
Keywords
Readme
Xenon-Smart Xiot Backend Applications
Description
This library localize the custom error messages and validation error messages that thrown from application.
Installation and Setup
$ 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
Usage
Getting Started
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
Step 1: Import the I18nErrorHandlingModule
- Import 'I18nErrorHandlingModule' to your root module. (AppModule)
Step 2: Use the I18nValidationPipe Globally
- Use the I18nValidationPipe globally via APP_PIPE as provider in root module.
Step 3: Define DTOs with Custom Messages
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();