【发布时间】: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