尝试本地化静态字符串时,消息显示时被问号“??”包围,Thymeleaf和Spring Boot不从本地化文件读取属性 - 出现问号而不是本地化字符串
例如?? 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.prope的内容rties是:
ticket.type=BUGS!!!!
配置:
spring.messages.basename=messages
在启动时输出:
2016年7月19日08:38:28.673 DEBUG 5175 --- [主] ationConfigEmbeddedWebApplicationContext:使用MessageSource [org.springframework.context.support.ResourceBundleMes sageSource: basenames = [messages]]
我也尝试在下面的代码中使用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;
}
}
为了完整这里是我的应用程序配置(与其他人一样,我不得不exluse的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();
}
这打印出以下,所以我知道春天引导我■装载资源包,但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>
这种类似http://stackoverflow.com/questions/28750292/custom-spring-boot-starter-how-do -you-contribute-i18n-messages-to-the-messageso – Saravana
你试过'ReloadableResourceBundleMessageSource'而不是'ResourceBundleMessageSource'吗? – Lucky
你为什么要自己配置它? Spring Boot已经为您配置了百里香叶,本地化等......使用框架而不是围绕框架。 –