Hello I'm trying to do a i18n in: Spring-MVC-Maven configured-by-context.xml project
I have files (path:\src\main\resources):
messages.properties
messages_pl.properties
The app used to work properly when there is only one file -> reads
< spring:messages ...> fome the one.
But when there are 2 files app reads only messages_pl.properties no mather about ?lang=en.
DispatcherServlet-context.xml
/* [..] */
<mvc:annotation-driven enable-matrix-variables="true"/>
<mvc:resources location="/resources/" mapping="/resource/**"/>
<context:component-scan base-package="com.ehr" />
<mvc:interceptors>
<bean class="com.ehr.webstore.interceptor.PerformanceMonitorInterceptor"/>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
/* [...] */
webpage.jsp
< div class="pull-right" style="padding-right:50px">
< a href="?lang=en" >en</a>|<a href="?lang=pl" >pl< /a>
< /div>
Ok, I've found the root of the problem. All in all it's a FallbackToSystemLocale property which by default is setted on "true". For more info check ResourceBundleMessageSource doc.
Solution:
added property <property name="fallbackToSystemLocale" value="false"/> to <bean id="messageSource" [...] ResourceBundleMessageSource">
Related
i'm having some issues handling the i18n of my webapp.
Is it possible through Spring to found a message in a MessageSource if it's not found in the other?
This is my Spring configuration
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="xxMessage" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en" />
</bean>
Example:
if the key is "login.user" and the locale is "en" the app shows the value in my xx_en.properties if exists, BUT i want to search in the other file (xx_es.properties) if the key doesn't exists.
Is that possible?
P.S: sorry for my english :D
Thanks in advance!
I think you need to have interceptor configuration in your spring configuration
<interceptors>
<beans:bean
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<beans:property name="paramName" value="locale" />
</beans:bean>
</interceptors>
Thanks, for your answer #DDK i've already managed to resolve the problem :D
Solution below.
Define chained message sources, provided by the interface HierarchicalMessageSource.
For example, if you have your i18n files
baseMessages.properties
and
messages.properties
you could chain them as
<bean id="baseMessageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="baseMessages" />
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="parentMessageSource" ref="baseMessageSource" />
</bean>
So, I see all answer this problem in Stackoverflow, but not be of any help to me.
(
SpringMVC+Thymeleaf ,error message is : template might not exist or might not be accessible by any of the configured Template Resolvers
Error resolving template "pages", template might not exist or might not be accessible by any of the configured Template Resolvers
SpringMVC+Thymeleaf ,error message is : template might not exist or might not be accessible by any of the configured Template Resolvers
...
)
Here my servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- Basic Configurations -->
<context:annotation-config/>
<context:component-scan base-package="com.podium.italia.controller"/>
<context:component-scan base-package="com.podium.italia.service"/>
<context:component-scan base-package="com.podium.italia.model"/>
<context:component-scan base-package="com.podium.italia.repository"/>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<!-- i18n -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="WEB-INF/i18n"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="useCodeAsDefaultMessage" value="true"/>
</bean>
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.FixedLocaleResolver">
<property name="defaultLocale" value="en"/>
</bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
<!-- Email support -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="protocol" value="smtp" />
<property name="username" value="smmailsender#gmail.com" />
<property name="password" value="Style#mix2014" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.quitwait">true</prop>
</props>
</property>
</bean>
<!-- THYMELEAF: Template Resolver for email templates -->
<bean id="emailTemplateResolver" class="org.thymeleaf.templateresolver.ClassLoaderTemplateResolver">
<property name="prefix" value="mail/" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="1" />
</bean>
<!-- THYMELEAF: Template Resolver for webapp pages -->
<!-- (we would not need this if our app was not web) -->
<bean id="webTemplateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/" />
<property name="templateMode" value="HTML5" />
<property name="characterEncoding" value="UTF-8" />
<property name="order" value="2" />
</bean>
<!-- THYMELEAF: Template Engine (Spring3-specific version) -->
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolvers">
<set>
<ref bean="emailTemplateResolver" />
<ref bean="webTemplateResolver" />
</set>
</property>
</bean>
<!-- THYMELEAF: View Resolver - implementation of Spring's ViewResolver interface -->
<!-- (we would not need this if our app was not web) -->
<bean id="viewResolver" class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="characterEncoding" value="UTF-8" />
</bean>
<import resource="daoContext.xml"/>
Here emailService:
final WebContext ctx = new WebContext(request,response, request.getServletContext(), locale);
//.....
// Create the HTML body using Thymeleaf
final String htmlContent = this.templateEngine.process("email-inlineimage.html", ctx);
message.setText(htmlContent, true /* isHtml */);
And Error:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template "email-
inlineimage.html", template might not exist or might not be accessible by any of the
configured Template Resolvers
org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:924)
org.thymeleaf.TemplateEngine.process(TemplateEngine.java:898)
com.podium.italia.service.EmailService.sendMailWithInlineImages(EmailService.java:112)
I can't spot error in your servlet-context.xml so problem must be buried somewhere else (are you sure you have template file on classpath exactly at mail/email-inlineimage.html?). I'm providing working example as whole project (since it's overkill to paste everything to code samples here) which you can import to STS and run.
Spring Thymeleaf Mailing
I am using Spring MVC 3. I have message bundles in following format :
message_en_US.properties
message_en.properties
message_USA.properties
In my JSP I am using :
Now, the order of finding the values should be en_US, then en then USA property files.
how can i customize this ?
You should use configuration below. So, if you want to set default language you can use the second one.
In my src/main/resources I have mymessages_en_US.properties and mymessages_es_ES.properties
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<array>
<value>classpath:mymessages</value>
</array>
</property>
<property name="defaultEncoding" value="UTF-8" />
<property name="cacheSeconds" value="0" />
</bean>
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="defaultLocale" value="es_ES" />
</bean>
Hi I am trying to configure message sources in my Spring MVC web application .
I have currently got it running with ReloadableResourceBundleMessageSource but I am not able to get it running with ResourceBundleMessageSource . I would have preferred to use ResourceBundleMessageSource because I dont need the Reload capability and ResourceBundleMessageSource is slightly more efficient.
in my rootApplicationContext , I have defined the beans as follows.
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/resources/locale/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.SessionLocaleResolver" />
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<ref bean="localeChangeInterceptor" />
</property>
</bean>
This works FINE ..
But as soon as I change to
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="/resources/locale/messages" />
<property name="defaultEncoding" value="UTF-8"/>
</bean>
The application breaks with the exception :
12:35:57,433 ERROR
[org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/SpringJAXWS].[jsp]]
(http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet jsp
threw exception: org.apache.tiles.util.TilesIOException: JSPException
including path '/jsp/views/layout/top.jsp'. at
org.apache.tiles.servlet.context.ServletUtil.wrapServletException(ServletUtil.java:241)
[tiles-servlet-2.2.2.jar:2.2.2] at
org.apache.tiles.jsp.context.JspTilesRequestContext.include(JspTilesRequestContext.java:105)
[tiles-jsp-2.2.2.jar:2.2.2]
HELP !
The Entire project code is available at GITHUB
https://github.com/localghost8080/JaxWS
Here is the entire stack trace for all who are interested.
https://github.com/localghost8080/JaxWS/blob/master/ResourceBundleException.txt
ResourceBundleMessageSource uses a different format for passing basename values
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="resources.locale.messages" />
<property name="defaultEncoding" value="UTF-8"/>
I've changed default locale in spring configuration, but spring always uses messages_en.properties instead of messages.properties.It seems that Spring is ignoring my choice of locale.
defined locases:
messages.properties
messages_en.properties
Spring configuration:
application-context.xml
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="cs"/>
</bean>
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang" />
</bean>
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>messages</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
servlet-context.xml
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<exclude-mapping path="/admin/**"/>
<exclude-mapping path="/image/**"/>
<exclude-mapping path="/ajax/**"/>
<beans:bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
In JSP page
<spring:message code="csn.terminology.csnId" />
<p>Current Locale : ${pageContext.response.locale}</p>
<!-- output is 'cs', but messages are from messages_en.properties file -->
In project is used Spring Framework 3.2.4
Thank you in advance for your help.
if it's the complete configuration then you forgot to add the locale resolver
You can add a SessionLocaleResolver like this and set the default locale property
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.SessionLocaleResolver">
<property name="defaultLocale" value="en"></property>
</bean>
When locale is null, AbstractMessageSource calls Locale.getDefault()
So you should set set default locale as follow:
#Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource=new ResourceBundleMessageSource();
messageSource.setBasename("messages");
// SET DEFAULT LOCALE AS WELL
Locale.setDefault(Locale.US);
return messageSource;
}