代码初始化
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
package com.vetti.common.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.vetti.common.enums.InternationalLangTypeEnum;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 国际化配置属性
|
||||
*
|
||||
* @author wangxiangshun
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "vetti.international")
|
||||
public class InternationalProperties {
|
||||
|
||||
/**
|
||||
* 是否启用,默认为False
|
||||
*/
|
||||
private boolean enable = true;
|
||||
|
||||
/**
|
||||
* 请求语言参数位置(HEADER/PARAM),默认为HEADER
|
||||
*/
|
||||
private RequestLangParamPosition paramPosition = RequestLangParamPosition.HEADER;
|
||||
|
||||
/**
|
||||
* 请求语言参数名称,默认为lang
|
||||
*/
|
||||
private String paramName = "lang";
|
||||
|
||||
/**
|
||||
* 获取请求语言
|
||||
*
|
||||
* @return 请求语言
|
||||
*/
|
||||
public String getLang() {
|
||||
HttpServletRequest request = null;
|
||||
try {
|
||||
request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
||||
} catch (Exception ex) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
if (null == request) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String lang;
|
||||
if (paramPosition == RequestLangParamPosition.HEADER) {
|
||||
lang = request.getHeader(paramName);
|
||||
} else {
|
||||
lang = request.getParameter(paramName);
|
||||
}
|
||||
//语言类型特殊转换
|
||||
if(StrUtil.isNotEmpty(lang)){
|
||||
lang = lang.replaceAll("-Hans-","_").replaceAll("-Hant-","_").
|
||||
replaceAll("_Hans_","_").replaceAll("_Hant_","_").replaceAll("-","_");
|
||||
if(lang.contains("en") || lang.contains("EN")){
|
||||
lang = InternationalLangTypeEnum.INTERNATIONAL_LANG_TYPE_ENUM_EN.getCode();
|
||||
}else if(lang.contains("zh") || lang.contains("ZH")){
|
||||
lang = InternationalLangTypeEnum.INTERNATIONAL_LANG_TYPE_ENUM_ZH.getCode();
|
||||
}
|
||||
}else {
|
||||
lang = InternationalLangTypeEnum.INTERNATIONAL_LANG_TYPE_ENUM_EN.getCode();
|
||||
}
|
||||
|
||||
return lang;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求语言参数位置
|
||||
*/
|
||||
public enum RequestLangParamPosition {
|
||||
|
||||
HEADER, PARAM
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user