创建一个maven模块
pom.xml:
创建一个maven模块 在这里插入图片描述 pom.xml: <?xml
创建配置文件:LocaleConfiguration
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; /** * 语言区域配置 * @author zhiwei Liao */ @Configuration public class LocaleConfiguration { private final String LOCAL_HEAD_NAME = "Locale"; @Bean public WebMvcConfigurer localeInterceptor() { return new WebMvcConfigurer() { @Override public void addInterceptors(InterceptorRegistry registry) { LocaleChangeInterceptor localeInterceptor = new LocaleChangeInterceptor(); localeInterceptor.setParamName(LOCAL_HEAD_NAME); registry.addInterceptor(localeInterceptor); } }; } }
创建常量:LanguageConstant
/** * @author zhiwei Liao * @version 1.0 * @Description 语言 * @Date 2021/7/19 18:31 */ public class LanguageConstant { //波兰文 public static final String PL = "pl_pl"; //英文 public static final String US = "en_us"; //中文 public static final String CN = "zh_cn"; //请求头中设置语言 public static final String ACCEPT_LANGUAGE = "accept-language"; }
import com.liao.common.i18n.constant.LanguageConstant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.security.auth.login.LoginContext; import javax.servlet.http.HttpServletRequest; import java.util.Enumeration; import java.util.Locale; /** * 国际化转换工具 * @author zhiwei Liao */ @Component public class LocaleUtils { private static final Logger log = LoggerFactory.getLogger(LocaleUtils.class); private static MessageSource messageSource; public LocaleUtils(MessageSource messageSource) { LocaleUtils.messageSource = messageSource; } public static String getMessage(String msgKey) { return getMessage(msgKey, null); } public static String getMessage(String msgKey,Object[] args) { try { return messageSource.getMessage(msgKey, args, LocaleContextHolder.getLocale()); } catch (Exception e) { return msgKey; } } /** * @Description 获取当前语言 * @MethodParameterTypes [] * @MethodParameters [] * @MethodReturnType java.util.Locale * @Author zhiwei Liao * @Date 2021/7/19 18:43 **/ public static Locale getCurrentLocale() { String language = null; RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); if(LanguageConstant.ACCEPT_LANGUAGE.equals(name)){ Enumeration<String> values = request.getHeaders(name); while (values.hasMoreElements()) { String value = values.nextElement(); if(LanguageConstant.PL.equals(value)){ language = value; }else if(LanguageConstant.US.equals(value)){ language = value; }else if(LanguageConstant.CN.equals(value)){ language = value; } } } } } log.info("======getCurrentLocale.language:" + language); Locale locale = null; if(LanguageConstant.PL.equals(language)){ //波兰文 locale = new Locale("pl","PL"); // locale = new Locale("pl_PL");//本地有效,服务器上无效 }else if(LanguageConstant.US.equals(language)){ //英文 locale = Locale.US; }else if(LanguageConstant.CN.equals(language)){ //简体中文 locale = Locale.SIMPLIFIED_CHINESE; }else { //请求头中没有语言设置时,默认给英文 locale = Locale.US; } log.info("=========locale:" + locale); return locale; } }
其他模块使用国际化
引用国际化模块的依赖,创建多语言文件
错误提示国际化返回:
log.error(UserinfoConstant.LOGIN_FAILED); String message = messageSource.getMessage("user.login.failed", null, LocaleUtils.getCurrentLocale()); return ResultData.error(UserCodeConstant.LOGIN_FAILED, message);
测试是否可用:
波兰文:
英文:
中文: