【问题标题】:Thymeleaf and Spring Boot not reading properties from localization files - Question marks appear instead of the localized stringThymeleaf 和 Spring Boot 未从本地化文件中读取属性 - 出现问号而不是本地化字符串
【发布时间】:2016-11-22 09:24:40
【问题描述】:

当尝试本地化静态字符串时,显示的消息被问号“??”包围

例如??ticket.type_en_US??

<p th:text="#{ticket.type}">Blah</p>
  • 我正在使用 SpringBoot 1.3.6.RELEASE
  • 百里香叶:3.0.0.RELEASE
  • thymeleaf-spring4 神器

我在 application.properties 中配置了我的消息的基本名称

messages.properties 和 messages_en_US.properties 的内容是:

ticket.type=BUGS!!!!

配置:

spring.messages.basename=messages

启动时的输出:

2016-07-19 08:38:28.673 调试 5175 --- [主要] ationConfigEmbeddedWebApplicationContext:使用 MessageSource [org.springframework.context.support.ResourceBundleMessageSource: basenames=[消息]]

我还尝试在下面的代码中以编程方式使用 MessageResource。我将messages.properties 文件放在与application.properties 文件相同的文件夹中。 src/main/resources/

@Configuration
@ComponentScan("controller") 
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware{

    private ApplicationContext applicationContext;

    @Autowired
    private MessageSource messageSource;

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setTemplateResolver(templateResolver());
        engine.setMessageSource(messageSource);
        return engine;
    }

    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("/WEB-INF/templates/");
        resolver.setTemplateMode(TemplateMode.HTML);
        return resolver;
    }
}

为了完整起见,这里是我的应用程序配置(与其他人一样,我不得不排除 Thymeleaf 类):

@SpringBootApplication(exclude={org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration.class})
public class Application {

    public static void main(String[] args) {

        ApplicationContext ctx = SpringApplication.run(Application.class, args);

    }

}

我还通过在我的一个 REST 端点调用中输出内容来验证消息包正在加载:

@Autowired
    private MessageSource messageSource;

    @GET
    @Produces("application/json")
    public List<MyData> getData() {
        System.out.println("HERE 1 in Conversions");

        System.out.println(messageSource.getMessage("ticket.type", null, Locale.US));

        return getTheData();
    }

这会打印出以下内容,因此我知道 spring-boot 正在加载资源包,但 Thymeleaf 并没有以某种方式获取它们:

BUGS!!!!

这是我的完整 HTML 页面,可能有问题:

<html xmlns:th="http://www.thymeleaf.org">
<head>

    <title>Kitchen Sink</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

    <link href="http://cdn.jsdelivr.net/webjars/bootstrap/3.3.4/css/bootstrap.min.css"
          th:href="@{/webjars/bootstrap/3.3.4/css/bootstrap.min.css}"
          rel="stylesheet" media="screen" />

    <script src="http://cdn.jsdelivr.net/webjars/jquery/2.1.4/jquery.min.js"
            th:src="@{/webjars/jquery/2.1.4/jquery.min.js}"></script>

    <link href="../static/css/mike.css"
          th:href="@{css/mike.css}" rel="stylesheet" media="screen"/>
</head>

<body>
<div class="container">
    <div class="jumbotron">

        <h1>Hello</h1>

        <h2>Welcome to the Kitchen Sink!</h2>

        <p th:text="#{ticket.type}">Blah</p>

        <p th:text="#{test.type}">dssfgf</p>

      </div>

</body>

</html>

【问题讨论】:

  • 你试过ReloadableResourceBundleMessageSource而不是ResourceBundleMessageSource吗?
  • 为什么要自己配置? Spring Boot 已经为您配置了百里香叶、本地化等...使用框架而不是围绕框架工作。
  • @Saravana 我也尝试修改我的代码并使用“classpath:messages”,但它不起作用
  • @M.Deinum 是的,这就是我想做的。我以这种方式开始,但在查看示例后,我尝试以编程方式对其进行配置。我删除了这 4 个功能,它在启动时匹配。 MessageSourceAutoConfiguration 匹配 - 为 spring.messages.basename 找到捆绑:消息 (MessageSourceAutoConfiguration.ResourceBundleCondition) - @ConditionalOnMissingBean (types: org.springframework.context.MessageSource; SearchStrategy: current) 没有找到 bean (OnBeanCondition)

标签: java spring spring-boot localization thymeleaf


【解决方案1】:

好的,所以我想通了。感谢@M.Deinum 指出我应该让 spring-boot 和 Thymeleaf 做他们应该做的事情。

我必须设置:

engine.setMessageSource(messageSource);

并添加:

@Bean 

到其他 2 个函数。这允许将 MessageSource 传递到引擎并正确解析属性。

我将使用正确的来源更新上述问题,以便人们可以将其用于参考

【讨论】:

    【解决方案2】:

    请补充

    @EnableAutoConfiguration

    在你的 Spring boot 启动中它看起来像

    @EnableAutoConfiguration
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            ApplicationContext ctx = SpringApplication.run(Application.class, args);
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      我遇到了类似的问题,您帖子中的代码对我不起作用。我的问题的根源是,我没有 message.proprerties(没有任何语言)。

      我有:

      messages_en.properties
      messages_de.properties
      messages_es.properties
      

      但是没有用。

      只有在我添加时它才开始工作

      messages.properties
      

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 2012-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多