跟杨春娟学SpringBoot笔记:如何在url上直接换语言和多语言文件中参数设置?
完成:第一遍
1.如何在url上直接换语言比如localhost:8080/m/show?lang=zh_CN?
步骤一:新建LocaleConfig.java
LocaleConfig.java
package com.springboot.demo.SpringBootDemoProject.i18n; import java.util.Locale; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.LocaleResolver; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.i18n.SessionLocaleResolver; @Configuration public class LocaleConfig {@Beanpublic LocaleResolver localeResolver() {SessionLocaleResolver localeResolver = new SessionLocaleResolver();localeResolver.setDefaultLocale(Locale.US);return localeResolver;}//修改语言可以通过拦截器来做@Beanpublic WebMvcConfigurer localeInterceptor() {return new WebMvcConfigurer() {@Overridepublic void addInterceptors(InterceptorRegistry registry) {LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();localeChangeInterceptor.setParamName("lang");registry.addInterceptor(localeChangeInterceptor);}};} }
123456789101112131415161718192021222324252627282930313233343536373839