# nestjs-i18n-2

> The i18n module for Nest. Replace field `data` by `data` in response. Translate `Bad Request` to your language

Latest version **9.1.8** (published 2022-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-i18n-2
pnpm add nestjs-i18n-2
yarn add nestjs-i18n-2
bun add nestjs-i18n-2
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 9.1.8 |
| Published | 2022-07-23 |
| First published | 2022-06-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 333.5 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | Toon van Strijp |
| Maintainers | vncafe |

## Links

- npm: https://www.npmjs.com/package/nestjs-i18n-2
- npm.io page: https://npm.io/package/nestjs-i18n-2

## Dependencies (6)

- [cookie](https://npm.io/package/cookie.md) ^0.5.0
- [iterare](https://npm.io/package/iterare.md) ^1.2.1
- [js-yaml](https://npm.io/package/js-yaml.md) ^4.1.0
- [chokidar](https://npm.io/package/chokidar.md) ^3.5.3
- [string-format](https://npm.io/package/string-format.md) ^2.0.0
- [accept-language-parser](https://npm.io/package/accept-language-parser.md) ^1.5.0

## Recent versions

- 9.1.8 (latest) — 2022-07-23
- 9.1.7 — 2022-07-22
- 9.1.6 — 2022-07-22
- 9.1.5 — 2022-07-22
- 9.1.3 — 2022-06-29
- 9.1.2 — 2022-06-29

## README

# nestjs-i18n-2
This is a fork version from nestjs-i18n. This package help you to translate `message`, instead of default "Bad Request" message. Your input invalid `errors[]` now change to `data[]` field. You can make same API response structure in every case of HTTP Status.
_Example API response:_
```json
{
    "statusCode": 400,
    "message": "Invalid input",
    "data": [
        {
            "field": "username",
            "error": "Invalid phone number."
        }
    ]
}
```
## Installation
```
npm i nestjs-i18n-2
```
or custom version:
```
npm i nestjs-i18n-2@9.1.7
```
**_Note_**: Just install `nestjs-i18n-2` or `nestjs-i18n` only. You should remove one package:
```
npm uninstall nestjs-i18n
```
Update your import (from `nestjs-i18n` to `nestjs-i18n-2`) in any files in `/src`. The best method is use **replace in all file feature** in  your editor.
## Usage (_nestjs-i18n-2_)
1. **Enable i18n validation in** `main.ts`
    ```js
    // after const app = await NestFactory.create(AppModule)
        app.useGlobalPipes(
          new ValidationPipe({
            exceptionFactory: i18nValidationErrorFactory,
            transform: true,
          }),
        );
    ```
2. (Optional) **Custom your `data[]` like as _example API response_**
    ```js
        app.useGlobalFilters(new I18nValidationExceptionFilter({
          errorFormatter(data: ValidationError[]) {
            let customErrors: any[] = []
            data.forEach(error => {
              let element = {} as any
              element.field = error.property
              let errorStringJoin: string = ""
              for (let type in error.constraints) {
                errorStringJoin = errorStringJoin + error.constraints[type] + ". "
              }
              element.error = errorStringJoin.trim()
              customErrors.push(element)
            });
            return customErrors
          }
        }));
    ```
3. **Import `I18nModule` to `AppModule` as root module**
Open _**app.module.ts**_ then import module:
    ```js
        imports: [
        I18nModule.forRoot({
          fallbackLanguage: 'en', // set a default language,
          loaderOptions: {
            path: path.join(__dirname, '/i18n/'),
            watch: true,
          },
          resolvers: [
            { use: QueryResolver, options: ['lang'] },
            AcceptLanguageResolver,
          ],
        }),]
    ```
4. **Write you language JSON file**
    4.1. **Create `i18n` folder if not exist:**
    ```tree
    project:
    ├───src
    │   ├───i18n
    │   │   ├───en (this folder is for English)
    │   │   ├───fr (this folder is for French)
    │   │   ├───vi
    │   │   ├───... more language as you want...
    │   │   └───_code (this folder is custom for your status code... if you use `nestjs-custom-response` package).
    ```
    4.2. **Create `response.json` for multi-language**
    Create file `src\i18n\en\response.json`:
    ```json
    {
        "message": {
            "inputInvalid":"Invalid input"
        }
    }
    ```
    Create a translated file in your language(my language is `vi`, so I created `src\i18n\vi\response.json`):
    ```json
    {
        "message": {
            "inputInvalid":"Thông tin đầu vào không đúng"
        }
    }
    ```

# More infomation
This is a fork package, so you can read orginal package document here:
[![N|Solid](https://img.shields.io/npm/v/nestjs-i18n)](https://nestjs-i18n.com/)

---
_Source: https://npm.io/package/nestjs-i18n-2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
