Skip to content

feat: add support i18n #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 54 commits into from
Closed

Conversation

EndyKaufman
Copy link

Description

Example of usage

import { IsNotEmpty, ValidateIf, IsOptional, Equals, Validator, I18N_MESSAGES } from 'class-validator';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}
Object.assign(I18N_MESSAGES, {
  '$property must be equal to $constraint1': '$property должно быть равно $constraint1',
});
const model = new MyClass();
validator.validate(model).then(errors => {
  expect(errors.length).toEqual(1);
  expect(errors[0].target).toEqual(model);
  expect(errors[0].property).toEqual('title');
  expect(errors[0].constraints).toEqual({ equals: 'title должно быть равно test' });
  expect(errors[0].value).toEqual('bad_value');
});

Checklist

  • the pull request title describes what this PR does (not a vague title like Update index.md)
  • the pull request targets the default branch of the repository (develop)
  • the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • tests are added for the changes I made (if any source code was modified)
  • documentation added or updated
  • I have run the project locally and verified that there are no errors

Fixes

fixes #[issue number], fixes #[issue number]

@EndyKaufman
Copy link
Author

@NoNameProvided ping)

@EndyKaufman
Copy link
Author

Added support integrations with https://crowdin.com/ for manual simplified update translates

Added more examples of usages

Basic set custom messages

import { IsOptional, Equals, Validator, I18N_MESSAGES } from 'class-validator';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

const RU_I18N_MESSAGES = {
  ...I18N_MESSAGES,
  '$property must be equal to $constraint1': '$property должно быть равно $constraint1',
};

const model = new MyClass();

validator.validate(model, messages: RU_I18N_MESSAGES).then(errors => {
  console.log(errors[0].constraints);
  // out: title должно быть равно test
});

Load from file

import { IsOptional, Equals, Validator, I18N_MESSAGES } from 'class-validator';
import { readFileSync } from 'fs';
import { resolve } from 'path';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

const RU_I18N_MESSAGES = JSON.parse(readFileSync(resolve(__dirname, './node_modules/class-validator/i18n/ru.json')).toString());

const model = new MyClass();

validator.validate(model, messages: RU_I18N_MESSAGES).then(errors => {
  console.log(errors[0].constraints);
  // out: title должен быть равен test
});

With override

import { IsOptional, Equals, Validator, I18N_MESSAGES } from 'class-validator';

class MyClass {
  @IsOptional()
  @Equals('test')
  title: string = 'bad_value';
}

Object.assign(I18N_MESSAGES, {
  '$property must be equal to $constraint1': '$property должно быть равно $constraint1',
});

const model = new MyClass();

validator.validate(model).then(errors => {
  console.log(errors[0].constraints);
  // out: title должно быть равно test
});

@EndyKaufman
Copy link
Author

Hi, @NoNameProvided I want to use this feature in https://github.com/EndyKaufman/ngx-dynamic-form-builder, please review this PR :(

@EndyKaufman
Copy link
Author

I create new clean PR for this changes #743

@EndyKaufman EndyKaufman closed this Sep 3, 2020
@github-actions
Copy link

github-actions bot commented Oct 4, 2020

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant