toonvanstrijp / nestjs-i18n

The i18n module for nestjs.
https://nestjs-i18n.com
Other
642 stars 106 forks source link

translations does not work in DTO's #490

Closed allanojeda closed 1 year ago

allanojeda commented 1 year ago

Currently I'm having issues trying to translate the class-validators errors in a DTO. I set up main.ts, and dto as documented.

Main:

app.use(I18nMiddleware);
  app.useGlobalPipes(new ValidationPipe({ transform: true }));
  app.useGlobalPipes(new I18nValidationPipe());

  app.useGlobalFilters(
    new I18nValidationExceptionFilter({
    }),
  );

dto:

export class LoginDto {
  @IsEmail(
    {},
    {
      message: i18nValidationMessage('validation.validateEmail'),
    },
  )
  email: string;

  @IsString({
    message: i18nValidationMessage('validation.password'),
  })
  @Length(1, 18)
  password: string;
}

app.module: I18nModule.forRoot({ fallbackLanguage: 'en', loaderOptions: { path: join(__dirname, '/langs/'), watch: true, }, resolvers: [new HeaderResolver(['lang']), AcceptLanguageResolver], }),

the langs folder has this type of structure:

/langs/en/validation.json

{"password": "Password should be string"
}

I'm not using graphql, and the response I get after trying to see the errors translation for a bad input:

{
    "statusCode": 400,
    "message": [
        "password must be longer than or equal to 1 and shorter than or equal to 18 characters",
        "validation.password|{\"value\":123}"
    ],
    "error": "Bad Request"
}

As seen, I am not able to get the translation of validation.password key. Im not sure whats wrong, because I am able to translate custom messages in my services using I18nService. I'm using node v16.14.1 with NestJS framework.

lucaswiix commented 1 year ago

+1... anyone?

viktorsadikovic commented 1 year ago

does anyone have solution for this issue? @toonvanstrijp

NKwatra commented 1 year ago

Remove the code that instantiates validation pipe. You can pass the same options (except exceptionFactory) to I18nValidationPipe.

  app.useGlobalPipes(new I18nValidationPipe({ transform: true}));
  app.useGlobalFilters(
    new I18nValidationExceptionFilter({
    }),
  );
rubiin commented 1 year ago

Marking this as resolved

stigmat4j commented 1 year ago

same problem

image image image image