Thymeleaf和Spring Boot无法从本地化文件中读取属性 - 出现问号而不是本地化字符串

时间:2016-07-19 12:56:44

标签: java spring spring-boot localization thymeleaf

尝试本地化静态字符串时,将显示包含问号“??”

的消息

e.g。的 ?? ticket.type_en_US ??

<p th:text="#{ticket.type}">Blah</p>
  • 我正在使用SpringBoot 1.3.6.RELEASE
  • Thymeleaf: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 DEBUG 5175 --- [主要]   ationConfigEmbeddedWebApplicationContext:使用MessageSource   [org.springframework.context.support.ResourceBundleMessageSource:   基本名称= [消息]]

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

@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>

3 个答案:

答案 0 :(得分:1)

好的,所以我明白了。谢谢@ M.Deinum说我应该让spring-boot和Thymeleaf做他们应该做的事。

我必须设置:

engine.setMessageSource(messageSource);

并添加:

@Bean 

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

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

答案 1 :(得分:0)

请添加

  

@EnableAutoConfiguration

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

@EnableAutoConfiguration
@SpringBootApplication
public class Application {

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

    }
}

答案 2 :(得分:0)

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

我有:

messages_en.properties
messages_de.properties
messages_es.properties

但是没有用。

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

messages.properties