Internationalization Spring 3.1.1

servlet-context.xml is src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

xmlns:mvc = “http://www.springframework.org/schema/mvc”

servlet-context.xml is src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml

<!– i18n –>
<bean id=”messageSource”
class=”org.springframework.context.support.ReloadableResourceBundleMessageSource”>

<!– if you are keeping files in src/main provide value: “classpath:messages” –>

<property name=”basename” value=”/resources/i18n/messages” />
<property name=”defaultEncoding” value=”UTF-8″/>
</bean>

<mvc:interceptors>
<bean class=”org.springframework.web.servlet.i18n.LocaleChangeInterceptor”>
<property name=”paramName” value=”lang” />
</bean>
</mvc:interceptors>

<bean id=”localeResolver”
class=”org.springframework.web.servlet.i18n.CookieLocaleResolver”>
<property name=”defaultLocale” value=”fr”/>
</bean>

Message files

i18n-1

Working:

i18n-2

i18n-e

 

messages_en.properties:

home.button.download = Download

messages_es.properties:

home.button.download = Descargar

Note: Set property file encoding as “UTF-8”

JSP:

<spring:message code=”home.button.download” text=”” />

Array can be created like this which can be used in JS later:

<script type=”text/javascript”>
var i18n_js = new Array();
i18n_js[‘home.button.download’] = “<spring:message code=’home.button.download’ javaScriptEscape=’true’ />”;
i18n_js[‘home.app.title’] = “<spring:message code=’home.app.title’ javaScriptEscape=’true’ />”;
i18n_js[‘home.app.sub.title’] = “<spring:message code=’home.app.sub.title’ javaScriptEscape=’true’ />”;
</script>