Wednesday 14 April 2021

Bean Validation: Internationalize Error Messages

You can Internationalize the validation error messages using Resource Bundles.

 

Suppose if you want all the French locale messages, create a file 'ValidationMessages_fr_CA.properties' and keep all the messages in french locale here. Whenever a message specific to the locale is not found, then library takes the message from 'ValidationMessages.properties' file.

 

How to specify locale while calling the application?

Using the VM arguments 'user.country' and 'user.language', you can specify the locale that is used by the application.

 

Example

-Duser.country=CA -Duser.language=fr

 

Find the below steps to develop complete working application.

 

Step 1: Create properties file under src/main/resources folder.

 

validationMessages.properties

javax.validation.constraints.Size.message = User Name '${validatedValue}' is not upto the requirement. Name must have atleast {min} characters

 

validationMessages_fr_CA.properties

 

javax.validation.constraints.Size.message = Le nom d'utilisateur '$ {validatedValue}' n'est pas conforme à l'exigence. Le nom doit comporter au moins {min} caractères

 

Step 2: Define Employee.java

 

Employee.java

 

package com.sample.app.model;

import javax.validation.constraints.Size;

public class Employee {

	private int id;

	@Size(min=5)
	private String name;

	public Employee(int id, String name) {
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}

 

Step 3: Define App.java

 

App.java

package com.sample.app;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.sample.app.model.Employee;

public class App {
	private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
	private static Validator validator = validatorFactory.getValidator();

	private static void validateBean(Employee emp) {
		System.out.println("************************************");
		Set<ConstraintViolation<Employee>> validationErrors = validator.validate(emp);

		if (validationErrors.size() == 0) {
			System.out.println("No validation errors....");
		}

		for (ConstraintViolation<Employee> violation : validationErrors) {
			System.out.println(violation.getPropertyPath() + "," + violation.getMessage());
		}
		System.out.println("");
	}

	public static void main(String args[]) {
		Employee emp1 = new Employee(1, "Ram");
		System.out.println("Validation Errors for emp1");
		validateBean(emp1);

		Employee emp2 = new Employee(2, "RamaKrishna");
		System.out.println("Validation Errors for emp2");
		validateBean(emp2);

	}
}

 

Run App.java, by specifying the VM arguments (-Duser.country=CA -Duser.language=fr) for French Locale.

 

How to set the VM arguments in Eclipse?

Right click on App.java -> Run As -> Run Configurations…

 

 


 

Go to Arguments tab. Under VM arguments section add following VM arguments.

-Duser.country=CA -Duser.language=fr

 

Apply the changes.

 


 

Click on Run button. You will see the messages in french like below.

Validation Errors for emp1
************************************
name,Le nom d'utilisateur '$ {validatedValue}' n'est pas conforme à l'exigence. Le nom doit comporter au moins 5 caractères

Validation Errors for emp2
************************************
No validation errors....




 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment