Announcement Announcement Module
Collapse
No announcement yet.
Thymeleaf isn't resolving localized messages Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Thymeleaf isn't resolving localized messages

    I created an app with Roo and ran the "web mvc setup" command. I'm deleting the actual JSP views and tiles setup, etc as I'm replacing that with Thymeleaf.

    This line is the default that Roo generates for a MessageSource and I've added some entries to WEB-INF/i18n/messages.properties, but they're not being picked up.
    Code:
    <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource" p:basenames="WEB-INF/i18n/messages,WEB-INF/i18n/application" p:fallbackToSystemLocale="false"/>
    Here is a fragment from my view which checks to see if a list is empty, declares a variable who's value is a localized message in messages.properties so that it's available in the template called "empty-table".
    Code:
    <div th:if="${#lists.isEmpty(activities)}" th:with="tableText=#{activities.noDataFound}">
        <table th:substituteby="templates/empty-table :: empty-table"/>
    </div>
    Here is my messages.properties
    Code:
    tables.noDataFound=No data found.
    
    activities.noDataFound=No activities found.
    When I run my app, the message #{activities.noDataFound} isn't resolved, instead I get "??activities.noDataFound_en_US??". If I look at the console output when I run jetty, I can see my MessageSource is picked up and it shows the basenames indicated in the bean config, so shouldn't my message get resolved?
    Last edited by pgrimard; Nov 30th, 2012, 11:54 AM.

  • #2
    Oddly, if I add the messageSource bean to the main applicationContext.xml, messages get resolved. So how come it's not working when just defined in webmvc-config.xml?

    Comment


    • #3
      Originally posted by pgrimard View Post
      Oddly, if I add the messageSource bean to the main applicationContext.xml, messages get resolved. So how come it's not working when just defined in webmvc-config.xml?
      You have probably configured Thymeleaf in an applicationContext-*.xml file.
      messageSource bean is configured in webmvc-config.xml, in Roo generated configs.

      applicationContext-*.xml are loaded by ContextLoaderListener, while webmvc-config.xml is loaded by by DispatcherServlet
      The resulting contexts are not merged.
      The context loaded by DispatcherServlet (webmvc-config.xml) is a child of the main context, loaded by ContextLoaderListener (applicationContext-*.xml).
      This way, your Thymeleaf engine cannot see messageSource in child context.

      If you put both (messageSource and Thymeleaf configurations) into webmvc-config.xml, it will work

      Lorenzo

      Comment

      Working...
      X