SpringBoot国际化配置

项目结构:

SpringBoot国际化配置_第1张图片

 本次国际化【多语言】配置使用Session来实现

首先需要配置他的实现类I18nConfig和MyLocaleResolver

package com.narcos.seeu.config;

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.WebMvcConfigurationSupport;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

/**
 * @version V1.0
 * @ClassName:I18nConfig
 * @author: hbj
 * @CreateDate:2018/8/21 13:56
 */
@Configuration
public class I18nConfig extends WebMvcConfigurationSupport {
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
//        SessionLocaleResolver sessionLocaleResolver = new SessionLocaleResolver();
//        sessionLocaleResolver.setDefaultLocale(Locale.SIMPLIFIED_CHINESE);
//        return  sessionLocaleResolver;
    }

    //注册到拦截器中

    //@Bean
    //public LocaleChangeInterceptor localeChangeInterceptor(){
    //    LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
    //    localeChangeInterceptor.setParamName("lang");
    //    return localeChangeInterceptor;
    //}

    //@Override
    //protected void addInterceptors(InterceptorRegistry registry) {
    //    registry.addInterceptor(localeChangeInterceptor());
    //}
}

 

package com.narcos.seeu.config;

import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Locale;

/**
 * @version V1.0
 * @ClassName:MyLocaleResolver
 * @author: hbj
 * @CreateDate:2018/8/21 14:53
 */
public 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 (lang != null && 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) {

    }
}

 然后在项目的配置文件中加入对应国际化返回信息的文件【basename -> 拼接   message_en_US】

########################### international config ###########################
spring:
  messages:
    encoding: UTF-8
    basename: messages

 再配置对应的文件

############# user #############
phone.not.null.code=10001
phone.not.null.message=The phone number can not be empty
password.not.null.code=10002
password.not.null.message=The password can not be empty
phone.number.is.not.uptospecification.code=10003
phone.number.is.not.uptospecification.message=Mobile phone number is not up to specification
no.landing.information.code=10004
no.landing.information.message=No landing information
user.is.exist.code=10005
user.is.exist.message=The mobile phone number has been registered
register.failed.code=10006
register.failed.message=Register failed
register.success.code=10007
register.success.message=Register success
user.not.exist.code=10008
user.not.exist.message=User does not exist
password.wrong.code=10009
password.wrong.message=Wrong password. Please try again
login.success.code=10010
login.success.message=Landing success

 

############# user #############
phone.not.null.code=10001
phone.not.null.message=手机号不能为空
password.not.null.code=10002
password.not.null.message=密码不能为空
phone.number.is.not.uptospecification.code=10003
phone.number.is.not.uptospecification.message=手机号不符合规范
no.landing.information.code=10004
no.landing.information.message=暂无登陆的信息
user.is.exist.code=10005
user.is.exist.message=手机号已被注册
register.failed.code=10006
register.failed.message=注册失败
register.success.code=10007
register.success.message=注册成功
user.not.exist.code=10008
user.not.exist.message=用户不存在
password.wrong.code=10009
password.wrong.message=密码错误,请重试
login.success.code=10010
login.success.message=登陆成功

 然后是在项目中的具体使用:

    @Autowired
    private MessageSource messageSource;

    @GetMapping(value = "/category")
    public ResponseResult getMovieCategory(){
        Locale locale = LocaleContextHolder.getLocale();
        Map kindsMap = new HashMap<>(16);
              kindsMap.put(MovieTypeEnum.AFFECTIONAL_FILM.getCode(),messageSource.getMessage("affectional-film.message",null,locale));
        return new ResponseResult(Integer.parseInt(messageSource.getMessage("get.movie.kinds.success.code",null,locale)),true,messageSource.getMessage("get.movie.kinds.success.message",null,locale),kindsMap);
    
}

此处用POSTMAN模拟

 

SpringBoot国际化配置_第2张图片

 

你可能感兴趣的:(SpringBoot)