You need to enable JavaScript to run this app. Spring Boot MVC项目国际化配置后多语言消息显示异常问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot MVC项目国际化配置后多语言消息显示异常问题求助

Spring Boot MVC 国际化配置及问题

项目现有配置

  • application.properties 配置项:
    spring.messages.basename=i18n/messages
  • 德语消息资源文件路径:/home/michael/IdeaProjects/adsmanager/application/src/main/resources/i18n/messages_de_DE.properties
    文件内配置:
    greeting=Willkommen!
  • 英语消息资源文件路径:/home/michael/IdeaProjects/adsmanager/application/src/main/resources/i18n/messages_en_US.properties
    文件内配置:
    greeting=Welcome!
  • GeneralConfig.java 配置类完整代码:
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.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;


@Configuration
public class GeneralConfig implements WebMvcConfigurer {
    @Bean
    public LocaleResolver localeResolver() {
        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
        sessionLocaleResolver.setDefaultLocale(Locale.US);
        return sessionLocaleResolver;
    }

    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor() {
        LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
        lci.setParamName("lang");
        return lci;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }
}
  • 前端页面home.html 片段:
<div th:text="#{greeting}"></div>

问题现象

  • 访问http://localhost:8080/,页面输出内容为:??greeting_en_US??
  • 访问http://localhost:8080/?lang=de_DE,页面输出内容为:??greeting_de_DE??

补充说明

曾尝试将消息属性文件直接放置在resources目录下,同时移除application.properties中的basename配置,问题仍然存在,求解决方案。

内容的提问来源于stack exchange,提问作者Trts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.09 14:54:04