mirror of
https://gitee.com/dromara/RuoYi-Vue-Plus.git
synced 2026-09-20 02:08:15 +08:00
update 翻译功能支持多值映射
This commit is contained in:
parent
1abd2e7d7e
commit
73329e545a
@ -2,6 +2,7 @@ package org.dromara.common.translation.annotation;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
|
||||||
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
|
import org.dromara.common.translation.core.TranslationInterface;
|
||||||
import org.dromara.common.translation.core.handler.TranslationHandler;
|
import org.dromara.common.translation.core.handler.TranslationHandler;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
import java.lang.annotation.*;
|
||||||
@ -27,9 +28,11 @@ public @interface Translation {
|
|||||||
String type();
|
String type();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 映射字段 (如果不为空则取此字段的值)
|
* 映射字段 (如果不为空则取此字段的值,支持多值映射)
|
||||||
|
* <p>
|
||||||
|
* 多值映射模式下 {@link TranslationInterface#translation(Object key, String other)} 方法的第一个参数 key 类型为 {@link cn.hutool.core.lang.Dict}
|
||||||
*/
|
*/
|
||||||
String mapper() default "";
|
String[] mapper() default "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 其他条件 例如: 字典type(sys_user_sex)
|
* 其他条件 例如: 字典type(sys_user_sex)
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package org.dromara.common.translation.core.handler;
|
package org.dromara.common.translation.core.handler;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.lang.Dict;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import com.fasterxml.jackson.core.JsonGenerator;
|
import com.fasterxml.jackson.core.JsonGenerator;
|
||||||
import com.fasterxml.jackson.databind.BeanProperty;
|
import com.fasterxml.jackson.databind.BeanProperty;
|
||||||
@ -14,6 +16,7 @@ import org.dromara.common.translation.core.TranslationInterface;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
@ -38,9 +41,21 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
|
|||||||
TranslationInterface<?> trans = TRANSLATION_MAPPER.get(translation.type());
|
TranslationInterface<?> trans = TRANSLATION_MAPPER.get(translation.type());
|
||||||
if (ObjectUtil.isNotNull(trans)) {
|
if (ObjectUtil.isNotNull(trans)) {
|
||||||
// 如果映射字段不为空 则取映射字段的值
|
// 如果映射字段不为空 则取映射字段的值
|
||||||
if (StringUtils.isNotBlank(translation.mapper())) {
|
List<String> filterMappers = filterMappers();
|
||||||
value = ReflectUtils.invokeGetter(gen.getCurrentValue(), translation.mapper());
|
if (CollUtil.isNotEmpty(filterMappers)) {
|
||||||
|
// 适配原有的单值映射
|
||||||
|
if (filterMappers.size() == 1) {
|
||||||
|
value = ReflectUtils.invokeGetter(gen.getCurrentValue(), CollUtil.getFirst(filterMappers));
|
||||||
|
} else {
|
||||||
|
// 多值映射
|
||||||
|
Dict values = Dict.create();
|
||||||
|
for (String mapper : filterMappers) {
|
||||||
|
values.put(mapper, ReflectUtils.invokeGetter(gen.getCurrentValue(), mapper));
|
||||||
|
}
|
||||||
|
value = values;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果为 null 直接写出
|
// 如果为 null 直接写出
|
||||||
if (ObjectUtil.isNull(value)) {
|
if (ObjectUtil.isNull(value)) {
|
||||||
gen.writeNull();
|
gen.writeNull();
|
||||||
@ -53,6 +68,16 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 处理映射字段列表
|
||||||
|
*/
|
||||||
|
private List<String> filterMappers() {
|
||||||
|
return CollUtil.newHashSet(translation.mapper())
|
||||||
|
.stream()
|
||||||
|
.filter(StringUtils::isNotBlank)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
|
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
|
||||||
Translation translation = property.getAnnotation(Translation.class);
|
Translation translation = property.getAnnotation(Translation.class);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user