add 新增 ruoyi-common-translation 通用转换模块(雏形)
This commit is contained in:
parent
5057628b52
commit
a5835aa0c2
@ -28,6 +28,7 @@
|
|||||||
<module>ruoyi-common-security</module>
|
<module>ruoyi-common-security</module>
|
||||||
<module>ruoyi-common-sms</module>
|
<module>ruoyi-common-sms</module>
|
||||||
<module>ruoyi-common-web</module>
|
<module>ruoyi-common-web</module>
|
||||||
|
<module>ruoyi-common-translation</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<artifactId>ruoyi-common</artifactId>
|
<artifactId>ruoyi-common</artifactId>
|
||||||
|
@ -130,6 +130,14 @@
|
|||||||
<artifactId>ruoyi-common-web</artifactId>
|
<artifactId>ruoyi-common-web</artifactId>
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 翻译模块 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common-translation</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
28
ruoyi-common/ruoyi-common-translation/pom.xml
Normal file
28
ruoyi-common/ruoyi-common-translation/pom.xml
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1 @@
|
|||||||
|
com.ruoyi.common.translation.config.TranslationConfig
|
@ -73,6 +73,11 @@
|
|||||||
<artifactId>ruoyi-common-ratelimiter</artifactId>
|
<artifactId>ruoyi-common-ratelimiter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ruoyi</groupId>
|
||||||
|
<artifactId>ruoyi-common-translation</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 短信 用哪个导入哪个依赖 -->
|
<!-- 短信 用哪个导入哪个依赖 -->
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>com.aliyun</groupId>-->
|
<!-- <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.ExcelIgnoreUnannotated;
|
||||||
import com.alibaba.excel.annotation.ExcelProperty;
|
import com.alibaba.excel.annotation.ExcelProperty;
|
||||||
|
import com.ruoyi.common.translation.annotation.Translation;
|
||||||
|
import com.ruoyi.common.translation.constant.TransConstant;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
import java.io.Serial;
|
import java.io.Serial;
|
||||||
@ -67,6 +69,7 @@ public class TestDemoVo implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* 创建人
|
* 创建人
|
||||||
*/
|
*/
|
||||||
|
@Translation(type = TransConstant.USER_ID_TO_NAME)
|
||||||
@ExcelProperty(value = "创建人")
|
@ExcelProperty(value = "创建人")
|
||||||
private Long createBy;
|
private Long createBy;
|
||||||
|
|
||||||
@ -79,6 +82,7 @@ public class TestDemoVo implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* 更新人
|
* 更新人
|
||||||
*/
|
*/
|
||||||
|
@Translation(type = TransConstant.USER_ID_TO_NAME)
|
||||||
@ExcelProperty(value = "更新人")
|
@ExcelProperty(value = "更新人")
|
||||||
private Long updateBy;
|
private Long updateBy;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user