springboot 2.*(spring mvc 5) thymeleaf i18n(internationalization) 国际化问题 - Go语言中文社区

springboot 2.*(spring mvc 5) thymeleaf i18n(internationalization) 国际化问题


    文章目的:使用SpringBoot 2.*(其实就是Spring MVC 5) 加 Thymeleaf 实现国际化。

    其实很简单的问题,但由于网上SpringBoot 教程大多在 1.5.3.RELEASE ,没有实际能解决的。

    看Thymeleaf官方 、SpringBoot 官方、Spring MVC 5 官方文档。所以解决这个问题用了我2天...我还是太菜了。


    进入主题:

    首先是pom.xml依赖加上Thymeleaf。

<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;
}

然后创建国际化资源文件(properties):

我的工程目录结构:

    

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/u010531676/article/details/80352571
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-06-13 15:06:24
  • 阅读 ( 538 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢