Re: #544: Localization & BeanValidation

Sergey Beryozkin
 

Hi Pavel

Where would usually the code wishing to pass the Locale get the list of supported locales from, without even getting JAX-RS involved ?

If there was some well-known mechanism then the JAX-RS runtime would just use it as opposed to having to deal with a new provider...

Cheers, Sergey

On 22/05/17 17:46, Pavel Bucek wrote:

Dear experts,

we are working on improving integration with BeanValidation [1], and seems like if we choose to address this issue, the change will be slightly bigger than I anticipated.

In short, the issue is about providing Locale based on incoming request to the BV runtime, which can then choose it to provide localized error message. Since there is no way how to get a list of supported locales, we'd need to introduce a provider, which would register supported locales:

public interface SupportedLocaleProvider {
    
    List<Locale> getSupportedLocales();
}

If the application contains @Provider annotated subclass of this interface and there is a BV message to be produced, implementation would be required to do something like:

SupportedLocaleProvider supportedLocaleProvider = ...;
javax.ws.rs.Request request = ...;

List<Locale> locales = supportedLocaleProvider.getSupportedLocales();

javax.ws.rs.Variant.VariantListBuilder variantListBuilder = Variant.VariantListBuilder.newInstance();

List<Variant> vs = variantListBuilder.languages(locales.toArray(new Locale[]{})).build();
Variant v = request.selectVariant(vs);

(+ null checks, etc). If selected variant exists, then "v.getLanguage()" would be used as a Locale passed to BV runtime.

Please let us know what do you think about proposed solution of linked issue.

Also, the introduced interface might have more uses that just this (maybe too specific) case - any brainstorming ideas to what it could be used are welcomed.

Thanks and regards, Pavel

[1] https://github.com/jax-rs/api/issues/544


Join jaxrs-spec@javaee.groups.io to automatically receive all group messages.