Spring Boot 国际化(messages.properties)
Posted
技术标签:
【中文标题】Spring Boot 国际化(messages.properties)【英文标题】:Spring Boot internationalization (messages.properties) 【发布时间】:2016-02-23 14:36:12 【问题描述】:我试图简单地将应用程序的版本号添加到 Thymeleaf 片段。我正在使用 Spring Boot 1.2.5。如果我有一个名为 /resources/messages.properties 的文件定义如下:
application.version=1.0.0
我有一个带有以下片段的 Thymeleaf 视图:
Application Version: <span th:text="#application.version">
它显示的是 ??application.version_en_US?? 之类的东西,而不是 1.0.0。 (我在类路径中也有名为 messages_en.properties 和 messages_en_US.properties 的文件,它们的内容也相同。)我真的不知道如何解决这个问题......我花了好几个小时做一些看似微不足道的事情......
Application.java
@SpringBootApplication
@ComponentScan(basePackages = "org.application")
@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class, ThymeleafAutoConfiguration.class)
@PropertySources(value = @PropertySource("classpath:website.properties"))
public class Application extends SpringBootServletInitializer
public static void main(String[] args) throws Exception
SpringApplication.run(Application.class, args);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
return application.sources(Application.class);
WebConfig.java
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter
@Bean
public SpelAwareProxyProjectionFactory projectionFactory()
return new SpelAwareProxyProjectionFactory();
@Bean
public SessionHandler sessionHandler()
return new SessionHandler();
@Override
public void addViewControllers(ViewControllerRegistry registry)
registry.addViewController("/web/auth/login").setViewName("auth/login");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
registry.addResourceHandler("/resources/**").addResourceLocations(
"/resources/");
@Bean
public LocaleChangeInterceptor localeChangeInterceptor()
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
@Bean
public CookieLocaleResolver localeResolver()
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
localeResolver.setDefaultLocale(Locale.ENGLISH);
return localeResolver;
@Bean
public ResourceBundleMessageSource messageSource()
return new ResourceBundleMessageSource();
@Override
public void addInterceptors(InterceptorRegistry registry)
// Locale change interceptor
registry.addInterceptor(localeChangeInterceptor());
// Utility interceptor which helps with the "active" link styles in the navigation. --mm
registry.addInterceptor(new BaseInterceptor());
// Expire session after a period of time
registry.addInterceptor(sessionHandler());
ThymeleafConfig.java
@Configuration
public class ThymeleafConfig
@Bean
public ServletContextTemplateResolver templateResolver()
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
// must use Legacy HTML5 as the template, otherwise Handlebars will not parse!
//
// this should hopefully be fixed in Thymeleaf 3.0
resolver.setTemplateMode("LEGACYHTML5");
resolver.setCacheable(false);
return resolver;
public SpringTemplateEngine templateEngine()
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
// Add Spring security
Set<IDialect> dialects = new HashSet<IDialect>();
engine.setAdditionalDialects(dialects);
engine.addDialect(new SpringSecurityDialect());
return engine;
@Bean
public ViewResolver viewResolver()
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setOrder(1);
viewResolver.setViewNames(new String[]"*");
viewResolver.setCache(false);
return viewResolver;
将购买虚拟镜头给任何可以解决此问题的人...
【问题讨论】:
【参考方案1】:我想你总是可以在你的 templateEngine 方法中添加这个:
engine.addMessageResolver(new StandardMessageResolver());
or engine.setMessageResolver(new StandardMessageResolver());
另外,从设计的角度来看,我建议你尝试使用 thymeleaf 的自动配置(删除排除),以及 spring boot 自动为你提供的许多其他东西。
【讨论】:
我放弃了这种自制的配置,只是将所有 Thymeleaf 配置放在 application.properties 中。 i18n 现在像冠军一样工作。以上是关于Spring Boot 国际化(messages.properties)的主要内容,如果未能解决你的问题,请参考以下文章