npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-i18n-2

v9.1.8

Published

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

Downloads

4

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:

{
    "statusCode": 400,
    "message": "Invalid input",
    "data": [
        {
            "field": "username",
            "error": "Invalid phone number."
        }
    ]
}

Installation

npm i nestjs-i18n-2

or custom version:

npm i [email protected]

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
    // 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
        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:
        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:
    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:
    {
        "message": {
            "inputInvalid":"Invalid input"
        }
    }
    Create a translated file in your language(my language is vi, so I created src\i18n\vi\response.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