Search code examples
springinternationalizationmultilingual

Spring4 internationalization multiple language


http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/

Trying to follow this online tutorial to create a multi language web application the problem I am having is that I don't think my spring container is finding / loading my properties files. I am not sure what is wrong.

File Structure

enter image description here

welcome.properties

welcome.springmvc = Happy learning Spring MVC

welcome.properties

welcome.springmvc = \u5feb\u4e50\u5b66\u4e60 Spring MVC

Index.jsp

Language : <a href="?language=en">English</a>|<a href="?language=zh_CN">Chinese</a>

    <h2>
    welcome.springmvc : <spring:message code="welcome.springmvc" text="default text" />
    </h2>

    Current Locale : ${pageContext.response.locale}

app-dispatcher-servlet: I am sure that my interceptors are working because index.jsp ${pageContext.response.locale} is showing en/zh_cn

Internationalization: Multi lang support

Resource: http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/ http://howtodoinjava.com/spring/spring-mvc/spring-mvc-internationalization-i18n-and-localization-i10n-example/

http://www.technicalkeeda.com/spring-tutorials/spring-mvc-internationalization-i18n-example

<bean id="localeResolver"
    class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
    <property name="defaultLocale" value="en" />
</bean>

<mvc:interceptors>
    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language" />
    </bean>
</mvc:interceptors>

<!-- <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
    <property name="interceptors">
       <list>
        <ref bean="localeChangeInterceptor" />
        </list>
    </property>
</bean>  -->


<!-- Register the welcome.properties -->
<bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="welcome" />
</bean>



<!-- ViewResolver JSP -->
<bean id="jspViewResolver"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/html/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>

but my result in index.jsp is

enter image description here

Where it says "default text" should really show "Happy learning Spring MVC" from the properties files.


Solution

  • I have changed my app-dispatcher-servlet.xml explicit state class path

    From:

    <!-- Register the welcome.properties -->
     <bean id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="welcome" />
    </bean>
    

    To:

    <bean id="messageSource"
        class="com.app.service.CustomMessageSource">
        <property name="basenames">
            <list>
                <value>classpath:com/app/properties/welcome</value>
            </list>
        </property>
        <property name="defaultEncoding" value="UTF-8" />
    
    </bean>