实战:第二十二章:i18n国际化(已实现中文,英文,波兰文)
创建一个maven模块
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>common</artifactId>
<groupId>com.liao</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>common-i18n</artifactId>
<properties>
<poi.version>3.9</poi.version>
<spring.version>5.0.11.RELEASE</spring.version>
<javax.servlet.version>4.0.1</javax.servlet.version>
<spring.data.version>2.0.9.RELEASE</spring.data.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>${spring.data.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
</dependency>
</dependencies>
</project>
创建配置文件: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";
}
创建工具类:LocaleUtils
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);
测试是否可用:
波兰文:
英文:
中文: