-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 30 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Annotations Hibernate Validator
PostPosted: Tue Mar 02, 2010 6:38 am 
Newbie

Joined: Tue Mar 02, 2010 6:29 am
Posts: 10
Hello, I have a problem with hibernate validator annotations. How do I internationalize error messages? Would have a complete example that explains the whole procedure?
Thanks in advance for your cooperation.
Mario


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Tue Mar 02, 2010 8:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

you just have to add a set internationalized ValidationMessages property files to your classpath. Hibernate Validator ships with some internationalized properties files which you can see here http://fisheye.jboss.org/browse/Hiberna ... /validator

Obviously, these files live under org.hibernate.validator. Your own message bundles have to be placed into the root context of your jar file.

--Hardy


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Tue Mar 02, 2010 10:15 am 
Newbie

Joined: Tue Mar 02, 2010 6:29 am
Posts: 10
I'm working with an application in spring.
As the application to handle errors, invokes the default properties file, how do I invoke the properties file that interests me?


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Tue Mar 02, 2010 10:29 am 
Newbie

Joined: Tue Mar 02, 2010 6:29 am
Posts: 10
I want to clarify one thing.
I added the properties file in Italian and if the browser language is Italian, I receive messages properly. If you set your browser in English, always call the properties file in Italian. How do I call the file in question?
We will send you the code that I wrote.
Thank you for availability

public void afterPropertiesSet() throws Exception {
Configuration<?> configuration = Validation.byDefaultProvider().configure();
ValidatorFactory factory = configuration.messageInterpolator(new ContextualMessageInterpolatorLocal(configuration.getDefaultMessageInterpolator(),LocaleContextHolder.getLocale()))
.buildValidatorFactory();
validator = factory.getValidator();
}

ContextualMessageInterpolatorLocal;

public class ContextualMessageInterpolatorLocal implements javax.validation.MessageInterpolator{
private final MessageInterpolator delegate;
private final Locale defaultLocale;


public ContextualMessageInterpolatorLocal(MessageInterpolator delegate, Locale locale) {
this.delegate = delegate;
this.defaultLocale = locale;
}


@Override
public String interpolate(String message, Context context) {
return this.delegate.interpolate(message, context);



@Override
public String interpolate(String message, Context context, Locale locale) {
return this.delegate.interpolate(message, context, this.defaultLocale);
}
}


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Tue Mar 02, 2010 11:27 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Using a custom MessageInterpolator is the right ways forward. You have to make sure though that all interpolation calls will use the right Locale.

Quote:
Code:
@Override
public String interpolate(String message, Context context) {
   return this.delegate.interpolate(message, context);
}



should also delegate to the default interpolator using the Locale you set up.

Code:
@Override
public String interpolate(String message, Context context) {
   return this.delegate.interpolate(message, context,  this.defaultLocale);
}


I am not sure where and how this code is run, but you should not rebuild the factory all the time. In a webapp for example add the browser Local to a ThreadLocal and let my custom message interpolator extract it from there before delegating to the default interpolator. Something like this.

Btw, in your case the chosen properties file is probably always the Italian one, because your user Locale is Italian.

--Hardy


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Wed Mar 03, 2010 5:03 am 
Newbie

Joined: Tue Mar 02, 2010 6:29 am
Posts: 10
Hello
thank you for your information. The application still does not work. My problem is that my defaultMessageInterpolator is always Italian and do not know how to call another. How do I? I send you the code.

Configuration<?> configuration = Validation.byDefaultProvider().configure();
Locale locale = LocaleContextHolder.getLocale();
logger.info("Llocale: '" + locale );
MessageInterpolator interpolator = new ContextualMessageInterpolatorLocal( configuration.getDefaultMessageInterpolator(),locale);
ValidatorFactory validatorFactory = configuration.messageInterpolator(interpolator).buildValidatorFactory();
alidator = validatorFactory.usingContext().messageInterpolator(interpolator).getValidator();

Thanks in advance.
Mario


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Wed Mar 03, 2010 8:03 am 
Newbie

Joined: Tue Mar 02, 2010 6:29 am
Posts: 10
My problem is that I want to build a validatorfactory that is not the default


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Wed Mar 03, 2010 9:45 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I think your code look ok. You already have a custom inerpolator ContextualMessageInterpolatorLocal which takes care of selecting the right Locale. At least that's what the code suggests. The default message interpolator for Hibernate Validator is ResourceBundleMessageInterpolator which should always honor the specified Locale. Only if the specified Local is cannot be found it will fall back to the default resource bundle.

Have you tried debuggin your problem. What happens in ResourceBundleMessageInterpolator? Do you have the right resource bundles and can they be found. You can turn on logging. Set the category for org.hibernate.validator.engine.ResourceBundleMessageInterpolator[ to debug or trace. The interpolator should print out some information about the loaded resource bundles.

--Hardy


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Wed Mar 03, 2010 12:48 pm 
Newbie

Joined: Tue Mar 02, 2010 6:29 am
Posts: 10
Hello
I solved the problem in this way. I write the code. Do you think I chose the best solution or you have another solution?
Thank you for your attention and for your help.

The code is as follows:

Locale locale = LocaleContextHolder.getLocale();
logger.info("Llocale: '" + locale );
ResourceBundle bundle = ResourceBundle.getBundle("org/hibernate/validator/ValidationMessages", locale);
Locale.setDefault(LocaleContextHolder.getLocale());
bundle = ResourceBundle.getBundle("org/hibernate/validator/ValidationMessages");
MessageInterpolator interpolator = new ResourceBundleMessageInterpolator(bundle);
ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
validator = validatorFactory.usingContext().messageInterpolator(interpolator).getValidator();

Bye, Mario.


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Wed Mar 03, 2010 1:05 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

no, I don't think this is a good solution. It is not a good idea to call Locale.setDefault(). The default Locale applies fort the whole JVM which is probably not what you want in a multi language web application.
I still don't know where and how the code you are showing is executed, but my guess is you will run into trouble at some stage.

--Hardy


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Thu Mar 04, 2010 7:02 am 
Newbie

Joined: Tue Mar 02, 2010 6:29 am
Posts: 10
Hi,

the code I sent you yesterday I has been used in a project with "Spring". I do not know if it can help you. I can not find another solution from the one that I sent you.

Bye, Mario


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Thu Mar 04, 2010 10:46 am 
Newbie

Joined: Tue Mar 02, 2010 6:29 am
Posts: 10
have you another solution to solve the problem without invoking the method Locale.setDefault ()?

Bye, Mario


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Tue Apr 06, 2010 8:55 pm 
Newbie

Joined: Tue Apr 06, 2010 8:46 pm
Posts: 1
Hi.
I think this is the correct code to bypass the defaultLocale from ResourceBundleMessageInterpolator.
(this is not tested yet).
The point is that you should always call interpolate on delegate with the locale parameter, to avoid the default implementation to access its own defaultLocal.
Note that I haven't tested this yet.

Code:
public class ResourceBundleMessageInterpolator2 implements MessageInterpolator
{
    private final MessageInterpolator delegate;
    private final Locale defaultLocale;

    public ResourceBundleMessageInterpolator2(MessageInterpolator delegate, Locale defaultLocale)
    {
        this.delegate = delegate;
        this.defaultLocale = defaultLocale;
    }

    @Override
    public String interpolate(String message, Context context)
    {
        return delegate.interpolate(message, context, this.defaultLocale);
    }

    @Override
    public String interpolate(String message, Context context, Locale locale)
    {
        return delegate.interpolate(message, context, locale);
    }
}


the rest of your code seems correct to me.

david


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Mon Apr 12, 2010 11:06 am 
Newbie

Joined: Mon Apr 12, 2010 9:22 am
Posts: 1
i think i faced the same problem and found out its cause. See http://opensource.atlassian.com/projects/hibernate/browse/HV-306 for explanation and TestCase.


Top
 Profile  
 
 Post subject: Re: Annotations Hibernate Validator
PostPosted: Wed Jun 02, 2010 6:41 am 
Newbie

Joined: Wed Jun 02, 2010 6:38 am
Posts: 8
Hello,

What steps are necessary for outputing messages in differnet locales depending on the users session?

ResouceBundleMessageInterpolator chooses the english version all the time, why doesnt it have a constructor like
RBMI(ResourceBundle r, Locale l) and then load the resourcebundle by the locale specified by l?

I have tested the ResouceBundleMessageInterpolator2 and it does not work, still the messages are in the default, english.

Thank you


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 30 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.