Announcement Announcement Module
Collapse
No announcement yet.
i18n message.properties - Can't find bundle for base name messages Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • i18n message.properties - Can't find bundle for base name messages

    I'm hoping one of you brilliant Spring developers will be able to help me with this. I've been stuck all day, researching docs and online tips, to no avail.

    Want to support i18n internationalization in a simple test Spring MVC web app using the messages in a JSP.

    My message_xx_YY.properties files are in /src/main/webapp/resources/international/. I currently have messages_en_US.properties and messages_es_ES.properties.

    I've tried every combination of messageSource basename I can think of in my servlet-context.xml file, and I always get the error "nested exception is java.util.MissingResourceException: Can't find bundle for base name messages, locale en_US".

    Here's my ../WEB-INF/spring/appServlet/servlet-context.xml file:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns="http://www.springframework.org/schema/mvc"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:context="http://www.springframework.org/schema/context"
    	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    
    	<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    	
    	<!-- Enables the Spring MVC @Controller programming model -->
    	<annotation-driven />
    
    	<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    	<resources mapping="/resources/**" location="/resources/" />
    
    	<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    	<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<beans:property name="prefix" value="/WEB-INF/views/" />
    		<beans:property name="suffix" value=".jsp" />
    	</beans:bean>
    	
    	<!-- Support i18n message bundles -->
    	<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    		<!-- This is the path to where the property file is located, plus base name of the properties file (messages, ex: the bean will expand to messages_en_US.properties) -->
    		<beans:property name="basename" value="classpath:/resources/international/messages" />
    		<beans:property name="cacheSeconds" value="1" />
    		<beans:property name="defaultEncoding" value="UTF-8" /> 
    		<beans:property name="fallbackToSystemLocale" value="true" />
    	</beans:bean>
    	<context:component-scan base-package="com.mydomain.myapp" />	
    </beans:beans>
    I've tried all of the following:
    Code:
    <beans:property name="basename" value="classpath:/resources/international/messages" />
    <beans:property name="basename" value="/resources/international/messages" />
    <beans:property name="basename" value="resources/international/messages" />
    <beans:property name="basename" value="./resources/international/messages" />
    <beans:property name="basename" value="../resources/international/messages" />
    and have even tried moving the properties files to WEB-INF with appropriate basename - no change.

    My NavigationController, which I don't beleive is the issue, is:
    Code:
    Locale locale = Locale.getDefault();
    ResourceBundle resources = ResourceBundle.getBundle("messages", locale);
    Does anyone have any idea what I'm doing wrong here? Greatly appreciate ANY help, guidance, or assistance.

    Thanks!

  • #2
    If it is feasible for your project, I would recommend moving src/main/webapp/resources under src/main/webapp/WEB-INF. Then, you should change your message source bean definition to:

    Code:
        <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
            <property name="basenames">
                <array>
                    <value>WEB-INF/resources/messages</value>
                </array>
            </property>
            <property name="defaultEncoding" value="ISO-8859-1" />
            <property name="fallbackToSystemLocale" value="false" />
        </bean>

    Comment


    • #3
      Thanks, Manish. The actual issue *was* the controller. But I did move the properties files to a directory under WEB-INF. Appreciate the feedback!

      Comment


      • #4
        Manish, do you have any resources on the best practice for handling language-specific views? For pages with a lot of text, or html formatting, this gets unwieldy. I'm guessing something like ../views/en_us/wordypage.jsp, with a view resolver that manages this?

        Comment


        • #5
          Are you referring to the fact that when a page is mostly content-only, storing its content in i18 files is cumbersome? If this is the concern, and if a page will always have content in a single language only, I would just put the content in the page's view itself rather than externalizing it to an i18 file.

          If however, the page content can be vary by locale and storing all the content in i18 files is a problem, I would migrate the content to a proper content management system (CMS). Then, it will be a matter of writing an implementation of the MessageSource interface to fetch content from the CMS.

          Comment


          • #6
            The content page would have to support multiple languages; an FAQ page would be a decent example. I would think storing separate pages (or at lease separate JSP includes) would be more manageable than a message bundle (again, when there is heavy HTML formatting or custom images involved).

            I would expect a filesystem similar to:

            WEB-INF/views/en_US/faq.jsp
            WEB-INF/views/es_ES/faq.jsp

            And that a locale-sensitive view resolver would know how to set the language, so that the controller would just return "faq" and the resolver would direct it to the correct jsp page. Something similar to or derived from org.springframework.web.servlet.i18n.LocaleChangeI nterceptor.

            If something exists that does that, I haven't been able to find it. And don't want to create my own if it already exists out there. I can't be the first person to need this.

            And thanks for your help!

            Comment


            • #7
              I suggest you take a look at the different view resolver implementations... Especially the ResourceBundleViewResolver, which works in a similair way as the ResourceBundleMessageSource.

              Comment


              • #8
                Thanks, Marten. I think that's what I was looking for. Greatly appreciate the tip.

                Comment

                Working...
                X