add 新增 ruoyi-common-translation 通用转换模块(雏形)

feature/model
疯狂的狮子li 2 years ago
parent 5057628b52
commit a5835aa0c2

@ -28,6 +28,7 @@
<module>ruoyi-common-security</module>
<module>ruoyi-common-sms</module>
<module>ruoyi-common-web</module>
<module>ruoyi-common-translation</module>
</modules>
<artifactId>ruoyi-common</artifactId>

@ -130,6 +130,14 @@
<artifactId>ruoyi-common-web</artifactId>
<version>${revision}</version>
</dependency>
<!-- 翻译模块 -->
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-translation</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</dependencyManagement>

@ -0,0 +1,28 @@
<?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>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ruoyi-common-translation</artifactId>
<description>
ruoyi-common-translation 通用翻译功能
</description>
<dependencies>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-core</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,32 @@
package com.ruoyi.common.translation.annotation;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.ruoyi.common.translation.core.handler.TranslationHandler;
import java.lang.annotation.*;
/**
*
*
* @author Lion Li
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@Documented
@JacksonAnnotationsInside
@JsonSerialize(using = TranslationHandler.class)
public @interface Translation {
/**
* ( {@link com.ruoyi.common.translation.annotation.TranslationType} type)
*/
String type();
/**
* Key : type(sys_user_sex)
*/
String key() default "";
}

@ -0,0 +1,21 @@
package com.ruoyi.common.translation.annotation;
import java.lang.annotation.*;
/**
* ({@link com.ruoyi.common.translation.core.TranslationInterface} )
*
* @author Lion Li
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
public @interface TranslationType {
/**
*
*/
String type();
}

@ -0,0 +1,41 @@
package com.ruoyi.common.translation.config;
import com.ruoyi.common.translation.annotation.TranslationType;
import com.ruoyi.common.translation.core.TranslationInterface;
import com.ruoyi.common.translation.core.handler.TranslationHandler;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*
* @author Lion Li
*/
@Slf4j
@AutoConfiguration
public class TranslationConfig {
@Autowired
private List<TranslationInterface> list;
@PostConstruct
public void init() {
Map<String, TranslationInterface> map = new HashMap<>(list.size());
for (TranslationInterface trans : list) {
if (trans.getClass().isAnnotationPresent(TranslationType.class)) {
TranslationType annotation = trans.getClass().getAnnotation(TranslationType.class);
map.put(annotation.type(), trans);
} else {
log.warn(trans.getClass().getName() + " 翻译实现类未标注 TranslationType 注解!");
}
}
TranslationHandler.TRANSLATION_MAPPER.putAll(map);
}
}

@ -0,0 +1,22 @@
package com.ruoyi.common.translation.constant;
/**
*
*
* @author Lion Li
*/
public interface TransConstant {
/**
*
*/
String USER_ID_TO_NAME = "userIdToName";
/**
*
*/
String DICT_TYPE_TO_LABEL = "dictTypeToLabel";
String OSS_ID_TO_URL = "ossIdToUrl";
}

@ -0,0 +1,17 @@
package com.ruoyi.common.translation.core;
/**
* ( {@link com.ruoyi.common.translation.annotation.TranslationType} )
*
* @author Lion Li
*/
public interface TranslationInterface {
/**
*
*
* @param key
* @return
*/
String translation(Object key);
}

@ -0,0 +1,61 @@
package com.ruoyi.common.translation.core.handler;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.translation.annotation.Translation;
import com.ruoyi.common.translation.core.TranslationInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
*
*
* @author Lion Li
*/
@Slf4j
public class TranslationHandler extends JsonSerializer<Object> implements ContextualSerializer {
/**
*
*/
public static final Map<String, TranslationInterface> TRANSLATION_MAPPER = new ConcurrentHashMap<>();
private Translation translation;
@Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
try {
TranslationInterface trans = TRANSLATION_MAPPER.get(translation.type());
if (ObjectUtil.isNotNull(trans)) {
String result = trans.translation(StringUtils.isBlank(translation.key()) ? value : translation.key());
gen.writeString(StringUtils.isNotBlank(result) ? result : value.toString());
} else {
gen.writeString(value.toString());
}
} catch (BeansException e) {
log.error("字典数据未查到, 采用默认处理 => {}", e.getMessage());
gen.writeString(value.toString());
}
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
Translation translation = property.getAnnotation(Translation.class);
if (Objects.nonNull(translation)) {
this.translation = translation;
return this;
}
return prov.findValueSerializer(property.getType(), property);
}
}

@ -0,0 +1,29 @@
package com.ruoyi.common.translation.core.impl;
import com.ruoyi.common.translation.annotation.TranslationType;
import com.ruoyi.common.translation.constant.TransConstant;
import com.ruoyi.common.translation.core.TranslationInterface;
import org.springframework.stereotype.Component;
/**
* ( {@link com.ruoyi.common.translation.annotation.TranslationType} )
*
* @author Lion Li
*/
@Component
@TranslationType(type = TransConstant.DICT_TYPE_TO_LABEL)
public class DictTranslationImpl implements TranslationInterface {
/**
*
*
* @param key
* @return
*/
public String translation(Object key) {
if (key instanceof String dictType) {
return "dict";
}
return null;
}
}

@ -0,0 +1,29 @@
package com.ruoyi.common.translation.core.impl;
import com.ruoyi.common.translation.annotation.TranslationType;
import com.ruoyi.common.translation.constant.TransConstant;
import com.ruoyi.common.translation.core.TranslationInterface;
import org.springframework.stereotype.Component;
/**
* ( {@link com.ruoyi.common.translation.annotation.TranslationType} )
*
* @author Lion Li
*/
@Component
@TranslationType(type = TransConstant.USER_ID_TO_NAME)
public class UserNameTranslationImpl implements TranslationInterface {
/**
*
*
* @param key
* @return
*/
public String translation(Object key) {
if (key instanceof Long id) {
return "admin";
}
return null;
}
}

@ -73,6 +73,11 @@
<artifactId>ruoyi-common-ratelimiter</artifactId>
</dependency>
<dependency>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-common-translation</artifactId>
</dependency>
<!-- 短信 用哪个导入哪个依赖 -->
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->

@ -2,6 +2,8 @@ package com.ruoyi.demo.domain.vo;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.ruoyi.common.translation.annotation.Translation;
import com.ruoyi.common.translation.constant.TransConstant;
import lombok.Data;
import java.io.Serial;
@ -67,6 +69,7 @@ public class TestDemoVo implements Serializable {
/**
*
*/
@Translation(type = TransConstant.USER_ID_TO_NAME)
@ExcelProperty(value = "创建人")
private Long createBy;
@ -79,6 +82,7 @@ public class TestDemoVo implements Serializable {
/**
*
*/
@Translation(type = TransConstant.USER_ID_TO_NAME)
@ExcelProperty(value = "更新人")
private Long updateBy;

Loading…
Cancel
Save