带有Spring靴子和百里香的Spring MVC 4 - i18n无法正常工作

M.R*_*R.M 9 spring-mvc internationalization thymeleaf spring-boot

我试图在spring mvc 4中使用spring boot创建一个简单的国际化示例,但它不起作用.这是我的网络应用程序结构

在此输入图像描述

这是我的java配置:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Thymeleaf页面:

<!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>
Run Code Online (Sandbox Code Playgroud)

我在两个消息属性文件中定义了欢迎消息,并将以下内容放在application.properties文件中:

spring.messages.basename=messages
spring.messages.encoding=UTF-8
Run Code Online (Sandbox Code Playgroud)

当我点击localhost:8080/home时,它会打印出来 - welcome_en_us ?? 我不知道我错过了什么.我发现有些人将基本名称写成"classpath:messages",这是什么意思?我的意思是不是我的项目结构正确,应该将属性文件放在别处吗?

Mah*_*zad 1

由于没有人打算发布这个问题的答案,所以我发布了一个。

这个答案(只是答案!)与您的问题有关。

它表示您将默认语言的消息(无论是否是英语)放入 messages.properties 中并为其他语言环境创建特定文件。

如果messages.properties不是英语,则为其创建一个messages_en.properties文件。