Announcement Announcement Module
Collapse
No announcement yet.
A Locale-sensitive bean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • A Locale-sensitive bean

    I have just embarked on Spring for 2 weeks, in case something is immediately obvious, please kindly bear with me.

    I'm developing a simple prototype Web site with Spring. The site is expected to be fully internationalized. Rather than simply internationalized messages, I need:
    • Localized views - I got this with Tile's locale-based tile support. It's working well.
    • Localized domain objects - That's exactly what I am dealing with in this question.

    Note: The forum would not let me post the question. I tried many times but still it would not pass. I'm making it as a TXT attachment with the full text inside.

  • #2
    Hi,

    If u want session base locale, then u have to do following (I guess with some change it will also work per request based)



    1. Define following bean in ur servlet.xml file.
    <bean id="localeChangeInterceptor"
    class="org.springframework.web.servlet.i18n.Locale ChangeInterceptor">
    <property name="paramName" value="language" />
    </bean>


    2. you have to set the language param in session, whenever u want to change it. (I think interceptor is the best place)
    String language = request.getParamater("urparamoflang");
    if(language==null || language.length()<1){
    language="en";
    }
    request.getSession().setAttribute("language", language);

    Comment


    • #3
      Well, I did try LocaleChangeInterceptor eventually and that looked good (and far simpler to set up than I thought). But it did not solve all of my problems. I did arrive at a workable locale-sensitive bean implementation by leveraging (actually extending) the ResourceBundleMessageSource and using it for message resolution (so that it will query my own RB sources as well as the usual sources). After browsing some Spring code I think this is the only way that I manage to have my own RB implementations playing well with the rest of the Spring ones. Not entirely seamless, but it works.

      Despite this, I still have problems with getting localized application messages to the JSP-tiles layer. It's not being an impossible task now, it's just rather cumbersome. While I agree that using MessageContext.getMessage() works in the controller, it would be tiresome with lots of related code in the controller just doing message resolution tasks.

      I think it is quite intuitive that for encapsulation reason, it would be good to have, say a LocaleAwareMessage bean, that holds message key and only resolve it until the very end (i.e. rendering, say at JSP in my case), so that if I get hold of a certain message bean, I don't need to care whether that is a Chinese message, or a Japanese message for example (in fact, I opt to override equals() and hashCode() to have two messages considered equal provided they point to the same message key regardless of locale). I have implemented this, but I will be required to resolve everything in the controller for every Message instance I need.

      Worse still, I have some model objects of a rather deep structure and those Message instances may appear at some deeper level of the tree. Having to traverse the entire tree just to resolve some messages and preparing a view-specific model, in my opinion, is tiresome. I am asking to see if there is any better way to handle this in Spring MVC.

      So, any further hints I may explore?

      Comment


      • #4
        Having dug up more Spring sources itself I found the answer while tracing how <spring:message /> was implemented. My message object was added "implement MessageSourceResolvable" and that was all that I needed to have my message objects work with the spring message tag just like the Spring ones. Well, it's incredible the solution could be as simple as that!

        Comment

        Working...
        X