How to get values from message bundle? - spring

In my application configuration class, I have the following entry:
#Bean(name = "messageSource")
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/i18n/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
#Bean
public LocaleResolver localeResolver(){
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(new Locale("pt_BR"));
return resolver;
}
The message_pt_BR.properties is located according to the attached image.
I'm trying to access an entry on this with a <span th:text="#{app.name}"></span> file and all I get is:
??app.name_pt_br??
What am I missing here?

Your message bundle name should be
messages_pt_BR.properties
It's case sensitive.
It's OK without default message bundle(messages.properties). Mine works fine with only _en and _zh_TW.
And it should be ok that you put your resource bundle everywhere if you give the correct path to set basename.

You need to replace your messages bundle to /resources folder.
Also you need to add messages.properties bundle, it's required.

Related

How to switch between externalized messages in Spring with thymeleaf?

Spring + thymeleaf
I want to display the message from Messages_pl.properties or Messages_en.properties depending on the need. And here's my problem as I don't know what to do when I want to view a message from the second file ( Messages_pl.properties is taken into account by default).
To access resource bundles by using specified basename I added the bean below to my #Configuration class:
#Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("Messages");
return messageSource;
}
Messages_pl.properties:
welcome.message=siemanko
Messages_en.properties:
welcome.message=hello
The fragment of my html file where I use this property:
<h1 th:text="#{message.welcome}"></h1>
Result: siemanko
what should I do to get the result as hello?
You can define on the same file Messages.properties the following :
welcome.message.pl=siemanko
welcome.message.en=hello
Then you can can a local resolver to make your project capable of determining the locale which is currently being used :
#Bean
public LocaleResolver localeResolver() {
return new CookieLocaleResolver();
}
Then add an interceptor of your language :
#Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
}
After that its simple to switch between languages , you just have to change the value of parameter lang on your link :
localhost:8080/your_page?lang=pl // will show siemanko on your page
localhost:8080/your_page?lang=en // will show hello on your page

ReloadableResourceBundleMessageSource vs ResourceBundleMessageSource - boundle not found

When I put WAR file with Spring application on Tomcat server, if I use ReloadableResourceBundleMessageSource everything works fine.
#Bean
AbstractMessageSource messageSource()
{
ReloadableResourceBundleMessageSource bundle
= new ReloadableResourceBundleMessageSource();
bundle.setBasename("/WEB-INF/classes/messages");
return bundle;
}
But if I change message source implementation to ResourceBundleMessageSource:
#Bean
AbstractMessageSource messageSource()
{
ResourceBundleMessageSource bundle
= new ResourceBundleMessageSource();
bundle.setBasename("/WEB-INF/classes/messages");
return bundle;
}
I got following error:
org.springframework.context.support.ResourceBundleMessageSource.getResourceBundle ResourceBundle [/WEB-INF/classes/messages] not found for MessageSource: Can't find bundle for base name /WEB-INF/classes/messages, locale pl_PL
What makes a difference?

i18n works with Thymeleaf but not with MessageSource

I am working with Spring MVC+Thymeleaf and getting the following exception when I tried to get a message from de the MessageSource: NoSuchMessageException
My configuration:
#Bean(name="messageSource")
public MessageSource messageSource(){
ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource();
source.setBasename("classpath:i18n/messages");
source.setUseCodeAsDefaultMessage(true);
source.setDefaultEncoding( propWebEncoding );
source.setCacheSeconds(0); /* check the last-modified timestamp */
return source;
}
My files are under:
src/main/resources/i18n/messages_es.properties
src/main/resources/i18n/messages_en.properties
My locale is “en”
When I use the message “myproperty.example” in HTML (Thymeleaf) it works fine but not when I try to get the message in my #Controller or #Service with:
#Autowired MessageSource messageSource;
And
messageSource.getMessage(“myproperty.example”, null, Locale.EN);
It raises NoSuchMessageException exception impossible to find … for locale ‘en’
This is all configured with maven and my i18n files are in the target folder under:
WEB-INF/classes/i18n/ [files]
What I am doing wrong?
The problem: I was working in two different contexts. I was calling MessageSource in the #Service tier (getRootConfigClasses) and it was configured in the ServletConfigClasses.
Solution: Only work in the root context (getRootConfigClasses)

Spring-Boot Thymeleaf localization issue

My application works fine when I run it through Intellij (spring-boot jar). However, localized messages are not resolved when I manually deploy the war file to stand-alone tomcat. I get ??key??en_US
(key being the message key)
Any ideas on how to fix this?
I was able to resolve this issue by defining my own message source bundle.
#Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setAlwaysUseMessageFormat(true);
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setBasenames("classpath:messages");
return messageSource;
}

Why messages don't show up?

I have message property files in my Spring web application. The following is a related configuration:
#Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:messages");
messageSource.setUseCodeAsDefaultMessage(true);
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(0);
return messageSource;
}
The message in the property doesn't show up but the key with a language suffix. For example, a key, nav.welcome, in the property file is shown on a web page as ??nav.welcome_en?? I use Gradlew to run my application. And I see the messages.properties file on the root of the class path.
C:\Users\vic\workspace-sts\myapp\build\tmp\tomcatRunWar\work\Tomcat\localhost\_\WEB-INF\classes>
After turning up org.springframework.web log level, I see the following related log messages:
DEBUG: AnnotationConfigWebApplicationContext:649 - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource#2a83e48a]
and
DEBUG: AnnotationConfigWebApplicationContext:639 - Using MessageSource [org.springframework.context.support.ResourceBundleMessageSource: basenames=[classpath:messages]]
What is missing here?

Resources