修改翻译切面

This commit is contained in:
梁琦涛
2024-08-07 15:17:05 +08:00
parent 29edb773d5
commit 3b595f90b0
3 changed files with 111 additions and 2 deletions
@@ -1,5 +1,6 @@
package com.jero.common.aspect;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
@@ -14,6 +15,7 @@ import com.jero.common.aspect.annotation.Dict;
import com.jero.common.constant.CacheConstant;
import com.jero.common.constant.CommonConstant;
import com.jero.common.system.vo.DictModel;
import com.jero.common.system.vo.SysDictItemCore;
import com.jero.common.util.oConvertUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
@@ -24,6 +26,7 @@ import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
@@ -129,11 +132,17 @@ public class DictAspect {
}
private void putItems(List<JSONObject> items, List<Field> dictFieldList, Map<String, List<DictModel>> translText) {
List<SysDictItemCore> listDict = commonAPI.getDictItemAll();
if (CollectionUtils.isEmpty(listDict)) {
return;
}
for (JSONObject obj : items) {
for (Field field : dictFieldList) {
String code = field.getAnnotation(Dict.class).dicCode();
String text = field.getAnnotation(Dict.class).dicText();
String table = field.getAnnotation(Dict.class).dictTable();
String enText = field.getAnnotation(Dict.class).dicEnText();
String key = String.valueOf(obj.get(field.getName()));
String fieldDictCode = code;
if (!StringUtils.isEmpty(table)) {
@@ -158,11 +167,56 @@ public class DictAspect {
log.debug(" ---- dictModels: " + JSON.toJSONString(dictModels));
obj.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
String enValue = translateDictValueByEn(code, enText, table, key, listDict);
obj.put(field.getName() + CommonConstant.DICT_TEXT_EN_SUFFIX, enValue);
}
}
}
}
/**
* 翻译字典文本
*
* @param code 编码
* @param text 值
* @param table 表名
* @param key 键
* @param listDict 字典集合
*/
private String translateDictValueByEn(String code, String text, String table, String key, List<SysDictItemCore> listDict) {
if (oConvertUtils.isEmpty(key)) {
return null;
}
StringBuilder textValue = new StringBuilder();
String[] keys = key.split(",");
for (String k : keys) {
String tmpValue = null;
if (k.trim().length() == 0) {
continue; //跳过循环
}
if (!StringUtils.isEmpty(table) && StrUtil.isNotBlank(text)) {
tmpValue = commonAPI.queryTableDictTextByKey(table, text, code, k.trim());
} else {
List<SysDictItemCore> listNew = listDict.stream()
.filter(o -> Objects.equals(o.getDictCode(), code) &&
Objects.equals(o.getItemValue(), k.trim()))
.collect(Collectors.toList());
if (!CollectionUtils.isEmpty(listNew)) {
tmpValue = listNew.get(0).getEnName();
}
}
if (tmpValue != null) {
if (!"".equals(textValue.toString())) {
textValue.append(",");
}
textValue.append(tmpValue);
}
}
return textValue.toString();
}
private void addItems(Result<IPage<T>> result, List<JSONObject> items, List<Field> dictFieldList, Map<String, List<String>> dataListMap) {
for (Object obj : result.getResult().getRecords()) {
ObjectMapper mapper = new ObjectMapper();
@@ -410,7 +464,7 @@ public class DictAspect {
}
private String getTmpValue(String code, String text, String table, String k, String tmpValue) {
if (!StringUtils.isEmpty(table)){
if (!StringUtils.isEmpty(table) && !StringUtils.isEmpty(text)){
log.info("--DictAspect------dicTable="+ table+" ,dicText= "+text+" ,dicCode="+code);
String keyString = String.format("sys:cache:dictTable::SimpleKey [%s,%s,%s,%s]",table,text,code,k.trim());
if (Boolean.TRUE.equals(redisTemplate.hasKey(keyString))){
@@ -485,6 +539,7 @@ public class DictAspect {
String code = field.getAnnotation(Dict.class).dicCode();
String text = field.getAnnotation(Dict.class).dicText();
String table = field.getAnnotation(Dict.class).dictTable();
String enText = field.getAnnotation(Dict.class).dicEnText();
String key = String.valueOf(item.get(field.getName()));
//翻译字典值对应的txt
@@ -493,6 +548,8 @@ public class DictAspect {
log.debug(" 字典Val : " + textValue);
log.debug(" __翻译字典字段__ " + field.getName() + CommonConstant.DICT_TEXT_SUFFIX + " " + textValue);
item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
String enValue = translateDictValue(code, enText, table, key);
item.put(field.getName() + CommonConstant.DICT_TEXT_EN_SUFFIX, enValue);
}
//date类型默认转换string格式化日期
if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
@@ -212,7 +212,7 @@ public class TranslationAspect {
String enText = field.getAnnotation(Dict.class).dicEnText();
String key = String.valueOf(item.get(field.getName()));
String textValue = translateDictValue(code, text, table, key, listDict);
String enValue = translateDictValue(code, enText, table, key, listDict);
String enValue = translateDictValueByEn(code, enText, table, key, listDict);
item.put(field.getName() + CommonConstant.DICT_TEXT_SUFFIX, textValue);
item.put(field.getName() + CommonConstant.DICT_TEXT_EN_SUFFIX, enValue);
}
@@ -279,4 +279,47 @@ public class TranslationAspect {
}
return textValue.toString();
}
/**
* 翻译字典文本
*
* @param code 编码
* @param text 值
* @param table 表名
* @param key 键
* @param listDict 字典集合
*/
private String translateDictValueByEn(String code, String text, String table, String key, List<SysDictItemCore> listDict) {
if (oConvertUtils.isEmpty(key)) {
return null;
}
StringBuilder textValue = new StringBuilder();
String[] keys = key.split(",");
for (String k : keys) {
String tmpValue = null;
if (k.trim().length() == 0) {
continue; //跳过循环
}
if (!StringUtils.isEmpty(table) && StrUtil.isNotBlank(text)) {
tmpValue = commonAPI.queryTableDictTextByKey(table, text, code, k.trim());
} else {
List<SysDictItemCore> listNew = listDict.stream()
.filter(o -> Objects.equals(o.getDictCode(), code) &&
Objects.equals(o.getItemValue(), k.trim()))
.collect(Collectors.toList());
if (!CollectionUtils.isEmpty(listNew)) {
tmpValue = listNew.get(0).getEnName();
}
}
if (tmpValue != null) {
if (!"".equals(textValue.toString())) {
textValue.append(",");
}
textValue.append(tmpValue);
}
}
return textValue.toString();
}
}
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.jero.common.aspect.annotation.Dict;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
@@ -62,6 +63,14 @@ public class SysDictItemCore implements Serializable {
private Integer sortOrder;
/**
* 字典英文名称
*/
@Excel(name = "英文名称", width = 15)
@ApiModelProperty(value = "字典英文名称")
private String enName;
/**
* 状态(1启用 0不启用)
*/