springboot—国际化

原创
2020/04/27 18:52
阅读数 253

1.编写国际化配置文件,抽取页面需要显示的国际化信息

2.SpringBoot自动配置好了国际化资源文件的组件

1.配置文件可以放在类路径下,取名message.properties
2.可以自己指定一个包名+基础名(如上图,三个配置文件,基础名就为login),如果没有指定会在类路径的根目录下查找
spring.messages.basename=i18n.login

3. 页面点击更换区域信息(中英文切换)

//WebMvcAutoConfiguration.class
@Bean
@ConditionalOnMissingBean  //没有Bean放入容器时,才会启用默认的
@ConditionalOnProperty( prefix = "spring.mvc",name = {"locale"} )
//获取区域信息对象
public LocaleResolver localeResolver() {
	if (this.mvcProperties.getLocaleResolver() == org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties.LocaleResolver.FIXED) {
		//如果指定用固定的,就用这个固定的FixedLocaleResolver
		return new FixedLocaleResolver(this.mvcProperties.getLocale());
	} else {
		//否则AcceptHeaderLocaleResolver
			AcceptHeaderLocaleResolver localeResolver = new AcceptHeaderLocaleResolver();
			localeResolver.setDefaultLocale(this.mvcProperties.getLocale());
			return localeResolver;
		}
 }

//AcceptHeaderLocaleResolver
//没有指定用固定的
public Locale resolveLocale(HttpServletRequest request) {
        Locale defaultLocale = this.getDefaultLocale();
        if (defaultLocale != null && request.getHeader("Accept-Language") == null) {
            return defaultLocale;
        } else {
			//从请求头获取取域信息
            Locale requestLocale = request.getLocale();
            List<Locale> supportedLocales = this.getSupportedLocales();
            if (!supportedLocales.isEmpty() && !supportedLocales.contains(requestLocale)) {
                Locale supportedLocale = this.findSupportedLocale(request, supportedLocales);
                if (supportedLocale != null) {
                    return supportedLocale;
                } else {
                    return defaultLocale != null ? defaultLocale : requestLocale;
                }
            } else {
                return requestLocale;
            }
        }
    }
自己编写配置类,将MyLocaleResolver加入容器
//MyMvcConfig
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
        registry.addViewController("/main.html").setViewName("dashboard");
    }


    //将MyLocaleResolver加到容器
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}

//MyLocaleResolver
/**
* 区域信息解析器,用来解析区域信息,需要进行注册
* SpringBoot为我们自动配置了LocaleResolver组件,实现国际化我们需要自定义LocaleResolver才能起作用
*/
public class MyLocaleResolver implements LocaleResolver {
    Logger logger = LoggerFactory.getLogger(MyLocaleResolver.class);
    @Override
    public Locale resolveLocale(HttpServletRequest httpServletRequest) {
        String l = httpServletRequest.getParameter("l");
        //区域信息对象
        Locale locale=Locale.getDefault();
        if (!StringUtils.isEmpty(l)){
            String[] split = l.split("_");
            logger.info(split[0]+"_"+split[1]);
            locale = new Locale(split[0], split[1]);
        }
        return locale;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部