Hibernate verifier Change the locale at runtime

I created an application with hibernate and used comments for verification.
I realized that to translate the message into my language, I must put it in the resource folder A file called ValidationMessage_xx.properties. The question is what should be the default language, but I have to let the visitor choose to change the language of the website, so I also need to verify

Add the class that I use the validator Code

class example {

@NotEmpty
private String fieldOne;
@NotEmpty
private String fieldTwo;< br />
public String getFieldOne(){
return fieldOne;
}

public void setFieldOne(String fieldOne){
this.fieldOne = fieldOne
}

....
}

The default locale (e.g. ValidationMessage.properties) can be any language you wish, it all depends on the application. Because I can speak English, I tend to make the file include English-based translations, and I will do it as needed Extend to other languages.

As for choosing the appropriate locale, you need to provide a way to pass the value downstream of the application layer to the validation framework.

For example If you use spring, your application can set thread-local variables or use LocalContextHolder, which will allow you to set a thread-specific Locale, and you can statically access downstream in your code.

According to my past Experience, we usually only have one resource package, and we want to use it in bean validation shared with controllers and services. We provide a parser implementation for bean validation, which loads the resource package based on thread-local variables and uses this Open international news in various ways.

A mention Examples provided:

// This class uses spring's LocaleContextHolder class to access the requested
// application Locale instead a ThreadLocal variable. See spring's javadocs
// for details on how to use LocaleContextHolder.
public class ContextualMessageInterpolator
extends ResourceBundleMessageInterpolator {
private static final String BUNDLE_NAME = "applicationMessages";

@Override
public ContextualMessageInterpolator() {
super( new PlatformResourceBundleLocator( BUNDLE_NAME) );
}

@Override
public String interpolate(String template, Context context) {
return super.interpolate( template, context, LocaleContextHolder.getLocale() );
}

@Override
public String interpolate(String template, Context context, Locale locale) {
return super.interpolate( template, context, LocaleContextHolder.getLocale() );
}
}

The next step is to ask the Hibernate Validator Provide an instance of ContextualMessageInterpolator. This can be done by creating a validation.xml and placing META-INF at the root of the classpath. In a web application, this will be WEB-INF/classes/META-INF.

< p>

 xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
xmlns:xsi="http:/ /www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration"
version="1.1">
com.company.ContextualMessageInterpolator

Since I use applicationMessages as my package name, I only need Create a default applicationMessages.properties file and subsequent locale-specific versions, and add validation message strings to these properties files.

javax.validation.constraints .NotNull.message=Field must not be empty.
javax.validation.constraints.Max.message=Field must be less-than or equal-to {value}.
javax.validation.constraints.Min .message=Field must be greater-than or equal-to {value}.

I hope it helps.

I created one with hibernate App and use comments for verification.
I realize that I want to translate the message In my language, I have to put a file named ValidationMessage_xx.properties in the resource folder. The question is what should be the default language, but I have to let the visitor choose to change the language of the website, so I also need to verify

Add the class code for my validator

class example {

@NotEmpty
private String fieldOne;< br /> @NotEmpty
private String fieldTwo;

public String getFieldOne(){
return fieldOne;
}

public void setFieldOne (String fieldOne){
this.fieldOne = fieldOne
}

....
}

The default locale (e.g. ValidationMessage.properties) can be any language you wish, it all depends on the application. Because I speak English, I tend to make the file contain English-based translations, and I will Expand to other languages ​​as needed.

As for selecting the appropriate locale selection, you need to provide a way to pass values ​​downstream of the application layer to the validation framework.

For example, if you use spring, your application can set thread-local variables or use LocalContextHolder, which will allow you to set a thread-specific Locale, and you can statically access downstream in your code.

According to In my past experience, we usually only have one resource bundle, and we want to use it in bean validation shared with controllers and services. We provide a parser implementation for bean validation, which loads the resource bundle according to thread-local variables, and Expose internationalized messages in this way.

A provided example:

// This class uses spring's LocaleContextHolder class to access the requested
// application Locale instea da ThreadLocal variable. See spring's javadocs
// for details on how to use LocaleContextHolder.
public class ContextualMessageInterpolator
extends ResourceBundleMessageInterpolator {
private static final String BUNDLE_NAME = "applicationMessages";< br />
@Override
public ContextualMessageInterpolator() {
super( new PlatformResourceBundleLocator( BUNDLE_NAME) );
}

@Override
public String interpolate(String template, Context context) {
return super.interpolate( template, context, LocaleContextHolder.getLocale() );
}

@Override
public String interpolate(String template, Context context, Locale locale) {
return super.interpolate( template, context, LocaleContextHolder.getLocale() );
}
}

xmlns=”http://jboss.org/xml/ns/javax/validation/configuration”
xmlns:xsi=”http://www.w3.org/ 2001/XMLSchema-instance”
xsi:schemaLocation=”http://jboss.org/xml/ns/javax/validation/configuration”
version=”1.1″>
com.company.ContextualMessageInterpolator

Since I use applicationMessages as my package name, I only need to create a default applicationMessages.properties File and subsequent locale-specific versions, and add validation message strings to these properties files.

javax.validation.constraints.NotNull.message=Field must not be empty.
javax.validation.constraints.Max.message=Field must be less-than or equal-to {value}.
javax.validation.constraints.Min.message=Field must be greater -than or equal-to {value}.

I hope it helps.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4479 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.