Announcement Announcement Module
Collapse
No announcement yet.
Locale based handler mapping problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Locale based handler mapping problem

    Hi,

    Since I did not find anything about locale based handler mapping from this forum, I decided to make a new thread about my problem, which is following:

    I want to create a handler mapping which would manipulate the url address visible on the browser's address bar based on to the user's locale. However, both urls would be handled by the same controller.

    Example:

    User's locale is 'fi', the used action would be: fi.action

    User's locale is 'en', the used action would be: en.action

    However, both requests would be handled by XController class. Of course this could be solved by implementing a custom handler mapping, but I would like to do that only as a last resource.

    Any help is highly appreciated.

  • #2
    Hi Loke,

    One way of achieving this (I am sure there are cleaner ones) is to have your action() method inside your specific Controller class decides which specific method to call depending on the locale.

    1. As explained in code dot google dot com slash p slash nestorurquiza slash wiki slash SpringMVCTutorial (I cannot post URLs) you get your specific locale.
    2. Using beanshell, jformula or your own reflection technique call methods following <languageCountryOrBoth><WhateverActionFromYourCont roller>, for example:
    Controller#enUsAction(), Controller#esEsAction() and so on.

    I cannot think of a scenario right now where I would do the above considering the fact that commonly when the application has different business rules depending on locales you simply can set constrains in specific blocks of your code rather than creating specific methods per locale.

    I think this goes to the eternal question about more parameters versus longer method names. It all depends on your specifics of course.

    Best regards,

    -Nestor

    Comment


    • #3
      Hi Nestor,

      thank you for your answer. It give me interested information about localization even though it did not solve my problem. Perhaps I should have described it better, but the feature preventing me to paste urls is making it a bit hard. Anyway, here is (hopefully) a better description of my problem:

      Problem:

      I have been playing around with an idea of a public service, which has got localized urls for obtaining a better visibility on Google's search results. Now I am searching for the optimal solution for achieving this goal in a prototype application. The problem can actually be divided into two main areas:
      1. How to create localized links in a JSP page
      2. How to map the localized links to the correct controller

      Results achieved so far:

      By using SimpleUrlHandlerMapping it is possible to achieve a good starting point for solving the problem number 2. See:

      Code:
      <bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
              <property name="mappings">
                  <props>
                      <prop key="/suomi.action">controller</prop>
                      <prop key="/english.action">controller</prop>
                  </props>
              </property>		
      </bean>
      
      <bean id="controller" class="urltest.TestController"/>
      However, the solution leads into a bit messy configuration, and I would like to extract the actual configuration from the Spring configuration file. I would prefer a configuration based on resource bundles, because this would perhaps make it easier to solve the problem 1. I have, however, some fears concerning a solution like this:
      1. The configuration of the project is too heavy
      2. The development of new features will be slow due to the localization

      Any ideas or comments?
      Last edited by Loke; Sep 27th, 2008, 12:25 PM.

      Comment


      • #4
        I favor conventions whenever is possible.

        1. How to create localized links in a JSP page
        Depending on your localization strategy (it might be inspecting Accept-Language header or using cookies or ... you name it) this is easy. Do you see the only thing you have to do is:

        <code>
        RequestContext requestContext = new RequestContext(request);
        Locale myLocale = requestContext.getLocale();
        if(myLocale == null)
        myLocale = Locale.US;
        ModelAndView mav = new ModelAndView("myMultiLanguageView");
        mav.addObject("link", /spring/" + myLocale.toString() + "/myAction");
        ....
        </code>

        In your myMultiLanguageView.jsp:
        <code>
        <a href="<%=request.getAttribute("link") %>/">You might want to get the string describing your link from a properties file</a>
        <code>

        2. How to map the localized links to the correct controller
        If locale.toString() is "en_US" then when clicking the link you will end up calling:
        "/spring/en_US/myAction"

        And if you are following the conventions I suggest in my tutorial the path above will map to a Controller like:yourBasePackagePath.En_US#myAction().

        Of course you can eliminate the underscore and apply proper Camel casing but you get the idea.

        The above is a solution for any language and you do not need to touch any XML to add a new JSP View, link, controller to support new languages in the future.

        Clearer now?

        Cheers,

        -Nestor

        Comment

        Working...
        X