Springboot I18n国际化配置

为满足不同国家语言的快速切换,可以使用i18来实现页面的动态调整

//添加i18n配置类
@Configuration
public class I18nConfig extends WebMvcConfigurationSupport {

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor();
		//设置动态切换的参数值为lang
                localeInterceptor.setParamName("lang");
		registry.addInterceptor(localeInterceptor);
	}


        @Bean
	public LocaleResolver localeResolver() {
		SessionLocaleResolver localeResolver = new SessionLocaleResolver();
		localeResolver.setDefaultLocale(Locale.CHINA);
		return localeResolver;
	}
}
//spring 配置文件配置
spring:
  messages:

    #资源文件路径为classpath:i18n/messages.properties
    basename: i18n.messages
    cache-duration: 1
    encoding: utf-8
  mvc:
    pathmatch:
      matching-strategy: ant-path-matcher
#messages.properties文件实例
user.index.username=用户名
user.index.operate=操作
//html 使用样例
<table>
	<tr>
		<th><@spring.message code="user.index.username"/></th>
		<th><@spring.message code="user.index.operate"/></th>
	</tr>
</table>