Internationalization (i18n) working, but accented characters aren't accented - spring

I have a Spring MVC J2EE application that leverages Spring's localeChangeInterceptor and CookieLocaleResolver to render locale-driven support. This is working, however when I try to encode letters with accent marks, the front-end fails to render them as expected.
Here's a snippet from my webmvc-config.xml file:
<!-- Internalization and localization support -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</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>
Along with this I have some message_xx.properties files that contian my tags to render things out. Included are tags like this with accent marks embedded: district.manager.approval=Aprobación del Gerente de Distrito. My beef is that this displays exactly like this on the front-end instead of showing me Aprobación del Gerente de Distrito.
Any idea where I could have gone wrong?

The encoding of .properties files is usually (with few exceptions) expected to be Latin-1. Therefore for presenting Unicode content you need to escape the characters beyond Latin-1 repertoire:
district.manager.approval=Aprobaci\u00F3n del Gerente de Distrito
Or use XML properties files which can be encoded as UTF8.

After poking around a bit, it seems I left out a key detail: this only seems to be happening when I use JSTL <c:set> along with my Spring tags where the encoding does not work properly. As it turns out, when using <c:out>, you need to accompany it with the escapeXml="false" attribute. Here is what I did and it seems to be working appropriately now:
This is set in one page
<c:set var="headerScreenTitle">
<spring:message code='district.manager.review.and.approval' />
</c:set>
This is consumed in an imported page
<c:out value="${headerScreenTitle}" escapeXml="false" />
And it handsomely gives me this:
REVISIÓN Y APROBACIÓN DEL GERENTE DE DISTRITO
Thanks everyone for your responses!

If you use html entities (e.g. &) and use <spring:message code="property.name" /> tag to print values set "htmlEscape" attribute to "false":
<spring:message code="property.name" htmlEscape="false" />

Related

Spring internationalization

Iam working on a small project, when i try to implement i18n in spring,it not working,even when i try to change default language its not working. here is my spring-servlet.xml code
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="fr" />
<!-- <property name="cookieName" value="myAppLocaleCookie"></property>
<property name="cookieMaxAge" value="3600"></property>-->
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
i have 4 messages.properties in classpath messages.properties,messages_en.properties,messages_fr.properties,messages_de.properties
the default language not changing always its using messages_en.properties if remove messages_en.properties file ,then its using messages.properties.
And my hyperlinks are not working in jsp file
Language : English | French | german
when i use ${locale} in jsp it prints nothing..
plz help me tia..
Try ${requestContext.locale} instead of ${locale}.
And maybe jsp you test, has already '?' character in path, so lands on page with 2x'?' ?

Spring webflow view resolver config: subfolders for JSP Files

Using Spring webflow and in the serlvet-configuration I have this for JSP files:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
and this for flow XML files:
<webflow:flow-registry id="flowRegistry"
flow-builder-services="flowBuilderServices">
<webflow:flow-location-pattern value="/WEB-INF/flows/**/*-flow.xml" />
</webflow:flow-registry>
Due to the /**/, any flow XML file in may they be in sub directory or not is resolved automatically so I need not specify the subfolder in the flow definition.
For JSP ** does seem not work.
Is there a way to do the same? I'd like to use subdirectories, because it makes it look cleaner when don't have to scroll through a heap of JSP files in the package explorer. On the other hand, If I have to specify the subdirectory each time, it's probably prone to getting typos not being detected
I don't think there is a way to specify ant style wildcard in 'prefix' or 'suffix' attribute.
As you said, you can specify the path in the flow definition as below
<view-state id="showSearchCriteria" view="customer/search">
<transition on="lookupCriteriaEntered" to="searchCustomer" />
</view-state>
Though not an elegant solution but you can try jsps in multiple subfolders of jsp folder using tiles as:
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.tiles2.TilesView"/>
</bean>
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tilesDef.xml</value>
</list>
</property>
</bean>
Define your tilesDef.xml as:
<tiles-definitions>
<definition name="page1" template="/WEB-INF/jsp/subFolder1/page1.jsp"/>
<definition name="page2" template="/WEB-INF/jsp/subFolder2/page2.jsp"/>
</tiles-definitions>
In your controller return as:
new ModelAndView("page1");
or as:
new ModelAndView("page2");
But the drawback of this would be that you cannot have 2 jsps with same name in different folders as the definition name is unique.

How to internationalize REST Spring-MVC application?

I am new with spring and I still don't know well about hierarchy and terminology of spring. I am implementing a RESTful app with spring. After searching and reading about how to internationalize spring, I tried to do it in my app. But it seems to me it is not configured properly. Because I get exception. I would like to show you the screenshot of my project structure. And I would like to ask you why my applicationContext.xml show a problem exist.
spring-servlet.xml
<!-- SPRING INTERNALIZATION CONFIGURATION -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="language" />
</bean>
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
The javadoc of ReloadableResourceBundleMessageSource.setBaseNames() says:
Set an array of basenames, each following the basic ResourceBundle convention of not specifying file extension or language codes, but in contrast to ResourceBundleMessageSource referring to a Spring resource location: e.g. "WEB-INF/messages" for "WEB-INF/messages.properties", "WEB-INF/messages_en.properties", etc.
But you've placed your message properties files under src/main/resources in the Maven project structure, so they will end up at the root of the classpath instead (which means that even if Spring looked for them in the classpath, the /resources prefix you've used in the configuration would prevent Spring to find them).
So place the properties file under WEB-INF, and use WEB-INF/messages as the basenames property.

Internationalisation with Spring

I have an existing Spring/GWT Application which i need to add internationalisation to. My understanding is that i can use Spring's "ResourceBundleMessageSource" to automatically select the appropriate messages_* file depending on the users location. I tried following this tutorial but i can't seem to get the Application to display my strings in French. As it stands, I've added 2 files messages_en_US.properties and messages_fr_FR.properties into my src/main/resources/i18n folder and added the following to the applicationContext.XML:
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>classpath:i18n/messages</value>
</property>
</bean>
<bean id="localeChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" >
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
Just wondering 1) if i need additional configuration/glue code and 2) if i can test this easily without having to set the Language/Locale to French on my Redhat Server?
It's likely that your browser sends just "fr" language tag in Accept-Header. Spring is notorious for problems with fall-back, so you may need to copy the messages_fr_FR.properties as messages_fr.properties.
I am sure there must be some ways to configure fall-back, so you want have to use messages_en.properties (try your application with other English locales...), just messages.properties should do, but I am just too lazy/tired to look for solution at the moment.
Here you need to specify below bean in spring.xml.
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<mvc:interceptors>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
</mvc:interceptors>
This works perfectly fine when you pass lang=es in query string. If still any issue remain.you can check the working example Here .

Internationalization using Spring ResourceBundleMessageSource and FMT

We are trying to implement internationalization using Spring ResourceBundleMessageSource and FMT. But when we use it in the JSP, pages are displaying value as ???message.key???. Can you please help us in resolving this behavior? Really appreciate your answers.
Below are the configuration:
spring-servlet.xml entry
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>WEB-INF/messages/msgs</value>
</property> </bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName">
<value>locale</value>
</property> </bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver"/>
<bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
<property name="interceptors">
<list>
<ref bean="localeChangeInterceptor" />
</list>
</property>
</bean>
IN the JSP we have added the imported
<%# taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:message key="message.key"/>
Also in the war created we have the msgs.properties file under WEB-INF/messages folder.
Really appreciate help in identifying the mistake we are making. Thank you.
I am using the same as you and I am able to retrieve the messages from the properties file with fmt:message. Can you try to change the resource bundle for this:
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
<property name="fallbackToSystemLocale" value="false"></property>
</bean>
The properties are stored in src/main/resources.
Hope it helps.
Using FMT taglibraries we could not solve the issue. I believe it has got some thing to do with the jstl jar and taglibs which we are using with JBoss 7.1.1 server.
We started using spring tlds for displaying the messages. All is working fine now. Thanks for the help.
Use /WEB-INF/messages/msgs instead of WEB-INF/messages/msgs
Simply, add '/' at the beginning of path.

Resources