Friday, 4 October 2013

Internationalization(i18N) in Spring MVC

In this example we will learn i18N in Spring mvc.

for this we have to make properties file of languages which languages you want to implement in your application.

Here we make two properties files

1.  message_en.properties


  • msg.welcome=Welcome


2.  message_de.properties


  • msg.welcome=willkommen

Here we use english and german languages and put files into the resource folder.

Set the following  Spring configuration in spring-servlet.xml file.



<bean id="messageSource"
          class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
       
    </bean>
    <bean id="localeChangeInterceptor"
      class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language" />
    </bean>
    <bean id="localeResolver"
      class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean>
    <bean id="handlerMapping"
      class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
    </bean>


put the above code into your dispatcher xml file.


JSP page includes link for the english and german languages and add the following lines into your jsp page



<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>

<a href="?language=en">English</a>  | <a href="?language=de">German</a>

See <h3><spring:message code="msg.welcome"/> language</h3>




Regards
Rajnikant Panchal

No comments:

Post a Comment