社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
文章目的:使用SpringBoot 2.*(其实就是Spring MVC 5) 加 Thymeleaf 实现国际化。
其实很简单的问题,但由于网上SpringBoot 教程大多在 1.5.3.RELEASE ,没有实际能解决的。
看Thymeleaf官方 、SpringBoot 官方、Spring MVC 5 官方文档。所以解决这个问题用了我2天...我还是太菜了。
进入主题:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@Configuration
@ComponentScan
public class MyConfiguration implements WebMvcConfigurer {
/* @Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("classpath:i18n/messages");//设置默认的国际化资源文件路径
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}*/
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:i18n/messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new LocaleChangeInterceptor());
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
return slr;
}
我的工程目录结构:
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!