171 lines
5.3 KiB
Java
171 lines
5.3 KiB
Java
package aiyh.utils;
|
||
|
||
import aiyh.utils.entity.LabelHtmlIndex;
|
||
import aiyh.utils.service.UtilService;
|
||
import weaver.systeminfo.SystemEnv;
|
||
|
||
import java.util.Map;
|
||
import java.util.regex.Matcher;
|
||
import java.util.regex.Pattern;
|
||
|
||
/**
|
||
* @author EBU7-dev1-ayh
|
||
* create 2021/12/13 0013 10:29
|
||
* 多语言工具类
|
||
*/
|
||
|
||
|
||
public class LabelHtmlUtils {
|
||
|
||
private final UtilService utilService = new UtilService();
|
||
private Map<String, Object> htmlLabel = null;
|
||
|
||
public LabelHtmlUtils(String prefix) {
|
||
if (!this.init(prefix)) {
|
||
throw new RuntimeException("配置文件异常,请检查配置文件结构是否符合要求!");
|
||
}
|
||
}
|
||
|
||
public LabelHtmlUtils() {
|
||
}
|
||
|
||
/**
|
||
* 初始化配置信息
|
||
*
|
||
* @param prefix 前缀
|
||
* @return 是否初始化成功
|
||
*/
|
||
public synchronized boolean init(String prefix) {
|
||
if (this.htmlLabel != null) {
|
||
return true;
|
||
}
|
||
try {
|
||
this.htmlLabel = Util.readProperties2Map("htmlLabelIndex", prefix);
|
||
return this.htmlLabel != null;
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取指定语言id的自定义标签
|
||
*
|
||
* @param id 自定义标签id
|
||
* @param languageId 语言id
|
||
* @param defaultStr 默认字符串
|
||
* @return 自定义标签对应的语言字符串,或者默认字符串
|
||
*/
|
||
public String getHtmlLabelName(int id, int languageId, String defaultStr) {
|
||
String htmlLabelName = SystemEnv.getHtmlLabelName(id, languageId);
|
||
return htmlLabelName == null ? defaultStr : htmlLabelName;
|
||
}
|
||
|
||
/**
|
||
* 获取指定语言id的自定义标签
|
||
*
|
||
* @param id 标签id
|
||
* @param languageId 语言id
|
||
* @return 自定义标签的语言字符串
|
||
*/
|
||
public String getHtmlLabelName(int id, int languageId) {
|
||
return SystemEnv.getHtmlLabelName(id, languageId);
|
||
}
|
||
|
||
|
||
/**
|
||
* 通过语言id截取多语言字符串
|
||
*
|
||
* @param languageId 语言id
|
||
* @param languageStr 多语言字符串
|
||
* @return 截取后的字符串
|
||
*/
|
||
public String getHtmlLabelNameByStr(int languageId, String languageStr) {
|
||
String pattern = "(`~`" + languageId + " )(?<label>(\\w*|\\W*|[\\u4e00-\\u9fa5]*))(`~`)";
|
||
Pattern compile = Pattern.compile(pattern);
|
||
Matcher matcher = compile.matcher(languageStr);
|
||
if (matcher.find()) {
|
||
return matcher.group("label");
|
||
}
|
||
return languageStr;
|
||
}
|
||
|
||
/**
|
||
* 获取配置多余元文件详细
|
||
*
|
||
* @param key 键
|
||
*/
|
||
public Map<String, Object> getHtmlLabelMap(String key) {
|
||
if (this.htmlLabel == null) {
|
||
throw new RuntimeException("请初始化以读取配置信息,调用方法init(String prefix)");
|
||
}
|
||
Map<String, Object> map;
|
||
try {
|
||
map = (Map<String, Object>) this.htmlLabel.get(key);
|
||
} catch (Exception e) {
|
||
throw new RuntimeException("配置文件异常,请检查配置文件结构是否符合要求!");
|
||
}
|
||
return map;
|
||
}
|
||
|
||
/**
|
||
* 读取配置文件中的多语言配置
|
||
*
|
||
* @param key 多语言配置中的前缀
|
||
* @return 多语言配置Map
|
||
*/
|
||
public Map<String, String> getHtmlLabel(String key) {
|
||
if (this.htmlLabel == null) {
|
||
throw new RuntimeException("请初始化以读取配置信息,调用方法init(String prefix)");
|
||
}
|
||
Map<String, String> map;
|
||
try {
|
||
map = (Map<String, String>) this.htmlLabel.get(key);
|
||
} catch (Exception e) {
|
||
throw new RuntimeException("配置文件异常,请检查配置文件结构是否符合要求!");
|
||
}
|
||
return map;
|
||
}
|
||
|
||
/**
|
||
* 读取配置文件中的多语言配置
|
||
*
|
||
* @param key 多语言配置中的前缀
|
||
* @return 多语言配置对象
|
||
*/
|
||
public LabelHtmlIndex getLabelHtmlIndex(String key) {
|
||
if (this.htmlLabel == null) {
|
||
throw new RuntimeException("请初始化以读取配置信息,调用方法init(String prefix)");
|
||
}
|
||
Map<String, Object> map;
|
||
LabelHtmlIndex labelHtmlIndex;
|
||
try {
|
||
map = (Map<String, Object>) this.htmlLabel.get(key);
|
||
labelHtmlIndex = Util.mapToObject(map, LabelHtmlIndex.class);
|
||
} catch (Exception e) {
|
||
throw new RuntimeException("配置文件异常,请检查配置文件结构是否符合要求!");
|
||
}
|
||
return labelHtmlIndex;
|
||
}
|
||
|
||
|
||
/**
|
||
* 读取配置文件中的多语言配置
|
||
*
|
||
* @param map 多语言配置中的对象
|
||
* @return 多语言配置对象
|
||
*/
|
||
public LabelHtmlIndex getLabelHtmlIndex(Map<String, Object> map, String key) {
|
||
LabelHtmlIndex labelHtmlIndex;
|
||
Map<String, Object> resultMap;
|
||
try {
|
||
resultMap = (Map<String, Object>) map.get(key);
|
||
labelHtmlIndex = Util.mapToObject(resultMap, LabelHtmlIndex.class);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
throw new RuntimeException("map转换异常!");
|
||
}
|
||
return labelHtmlIndex;
|
||
}
|
||
}
|