Announcement Announcement Module
No announcement yet.
Spring MVC and ICU ULocale Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC and ICU ULocale

    I am working on adding a Spring MVC module to an existing application. This application uses the ICU library for its internationalization, in particular the ULocale object. I am aware that the Spring MVC library automatically links each request with a java.util.Locale object so that it can be included in a Controller method as an argument. I would like to do the same thing with the ICU ULocale object. Does anyone have any advice on how best to accomplish this?

    Here is an example of what I would like to be able to do:
    public ModelAndView exampleControllerMethod(@RequestParam("id") param, ULocale icuLocale) {
    The simplest solution I can think of is to write a simple converter that takes the java.util.Locale object that the Spring MVC LocaleResolver creates and pass it into an ICU ULocale object (The ULocale constructor takes a java.util.Locale). If I do this, does anyone know what converter I should use/interface I should implement?

    Another possibility is to figure out how to make a similar LocaleResolver structure for the ULocale object. Advice on how to do that would be greatly appreciated. Thanks for your help.
    Last edited by ttmrb; Mar 11th, 2012, 10:45 PM.

  • #2
    Any help would be greatly appreciated. Thanks.


    • #3
      I finally managed to figure out how to accomplish my goal above. I am posting the answer for those who may stumble upon this in the future.

      In Spring 3.1 there is a new interface called HandlerMethodArgumentResolver. It allows for the translation of web requests into the correct method argument. I created a new implementation of this interface called ULocaleMethodArgumentResolver which translates the java.util.Locale that the standard Spring MVC LocalResolver creates into a ICU ULocale object. Here is the code:
      public class ULocaleMethodArgumentResolver implements HandlerMethodArgumentResolver {
          public boolean supportsParameter(MethodParameter methodParameter) {
              return methodParameter.getParameterType().equals(ULocale.class);
          public Object resolveArgument(MethodParameter methodParameter, 
                                        ModelAndViewContainer modelAndViewContainer, 
                                        NativeWebRequest nativeWebRequest, 
                                        WebDataBinderFactory webDataBinderFactory) throws Exception {
              return ULocale.forLocale(nativeWebRequest.getLocale());
      Then in your Spring MVC context file, you add the bean to the argument-resolvers element in the annotation-driven element:
              <bean class=""/>
      For those using an older version of Spring, I found the WebArgumentResolver interface. I am not sure how it works but it looks similar.