Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 4 with Spring boot and thymeleaf - i18n not working

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

enter image description here

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?

like image 752
M.R.M Avatar asked Mar 10 '16 09:03

M.R.M


1 Answers

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.

like image 138
Mahozad Avatar answered Oct 29 '22 03:10

Mahozad