Spring国际化配置心得

1.首先配置好三个.properties文件

一个是默认语言文件,一个是US,一个是CN
在这里插入图片描述

2.在yml中配置刚刚写的properties bundle 的位置

spring:
  messages:
    encoding: UTF-8
    basename: messages

3.注入一个LocaleResolver的Bean

这里我们使用的是SessionLocalResolver,因为这个Bean是别人写好的,我们手写实现类容易出现屏蔽请求参数的情况,所以,不要盲目地百度cv!!!

踩坑,错误示例:

使用这个版本的配置会出现返回类型为application/xmlbug,标签类型的对象。

    class MyLocaleResolver implements LocaleResolver {
        private static final String LANG = "lang";
        private static final String LANG_SESSION = "lang_session";

        @Override
        public Locale resolveLocale(HttpServletRequest request) {
            String lang = request.getHeader(LANG);
            Locale locale = Locale.getDefault();
            if (!StringUtils.isEmpty(lang)){
                String[] langueage = lang.split("_");
                locale = new Locale(langueage[0],langueage[1]);

                HttpSession session = request.getSession();
                session.setAttribute(LANG_SESSION,locale);
            }else{
                HttpSession session = request.getSession();
                Locale localeInSession = (Locale) session.getAttribute(LANG_SESSION);
                if (localeInSession != null){
                    locale = localeInSession;
                }
            }
            return locale;
        }

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

        }
    }
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
 
    /**
     * 国际化
     * @return
     */
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }
}
正确示例:

注入一个SessionLocaleResolver,可以手动设置其local,timezone等等的参数,这里我们设置默认的CHINA地区,之后可修改



import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
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.SessionLocaleResolver;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

   @Autowired
   private LocaleResolver getSessionLocaleResolver() {
      SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
      sessionLocaleResolver.setDefaultLocale(Locale.CHINA);
      return sessionLocaleResolver;
   }
}

5.使用localResolver的方法

先注入一个messageSource对象

	@Autowired
	private MessageSource messageSource;
	String csvName = messageSource.getMessage("billing.statistics.count.user",null,LocaleContextHolder.getLocale()) + ".csv";
	// messageSource.getMessage("billing.statistics.count.user",null,LocaleContextHolder.getLocale()) 就是获取key-val
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值