I'm trying to make a simple internationalization example in spring mvc 4 with spring boot, but it's not working. Here is my web app structure
And here's my java configuration:
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter
{
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
Locale defaultLocale = new Locale("en_US");
localeResolver.setDefaultLocale(defaultLocale);
return localeResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new
LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
}
My Thymeleaf page:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport"
content="width=device-width, initialscale=1, maximum-scale=1.0, user-scalable=no" />
<title>Home</title>
<!-- CSS Links -->
</head>
<body>
<div class="container">
<div class="row">
<p th:text="#{welcome}">Hello</p>
</div>
</div>
</body>
</html>
and I defined welcome message in both messages properties files, and I put the following in the application.properties file:
spring.messages.basename=messages
spring.messages.encoding=UTF-8
when I hit localhost:8080/home, it prints out ??welcome_en_us??. I don't know what i have missed. And I found some people writing the basename as "classpath:messages', what does that mean? I mean isn't my project structure right and should put the properties files elsewhere?
Since nobody intends to post an answer for this question I post one.
This answer (just the answer!) is related to your problem.
It says that you put messages of your default language (whether it is English or not) in the messages.properties and make specific file for your other locales.
In case messages.properties is not for English, then make a messages_en.properties file for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With