update 优化 翻译组件 支持返回值泛型 支持多种类型数据翻译(例如: 根据主键翻译成对象)

2.X
疯狂的狮子Li 2 years ago
parent baa00ba452
commit 331238a057

@ -24,15 +24,15 @@ import java.util.Map;
public class TranslationConfig { public class TranslationConfig {
@Autowired @Autowired
private List<TranslationInterface> list; private List<TranslationInterface<?>> list;
@Autowired @Autowired
private ObjectMapper objectMapper; private ObjectMapper objectMapper;
@PostConstruct @PostConstruct
public void init() { public void init() {
Map<String, TranslationInterface> map = new HashMap<>(list.size()); Map<String, TranslationInterface<?>> map = new HashMap<>(list.size());
for (TranslationInterface trans : list) { for (TranslationInterface<?> trans : list) {
if (trans.getClass().isAnnotationPresent(TranslationType.class)) { if (trans.getClass().isAnnotationPresent(TranslationType.class)) {
TranslationType annotation = trans.getClass().getAnnotation(TranslationType.class); TranslationType annotation = trans.getClass().getAnnotation(TranslationType.class);
map.put(annotation.type(), trans); map.put(annotation.type(), trans);

@ -5,7 +5,7 @@ package com.ruoyi.common.translation.core;
* *
* @author Lion Li * @author Lion Li
*/ */
public interface TranslationInterface { public interface TranslationInterface<T> {
/** /**
* *
@ -13,5 +13,5 @@ public interface TranslationInterface {
* @param key () * @param key ()
* @return * @return
*/ */
String translation(Object key, String other); T translation(Object key, String other);
} }

@ -29,13 +29,13 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
/** /**
* *
*/ */
public static final Map<String, TranslationInterface> TRANSLATION_MAPPER = new ConcurrentHashMap<>(); public static final Map<String, TranslationInterface<?>> TRANSLATION_MAPPER = new ConcurrentHashMap<>();
private Translation translation; private Translation translation;
@Override @Override
public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException { public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
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())) { if (StringUtils.isNotBlank(translation.mapper())) {
@ -46,8 +46,8 @@ public class TranslationHandler extends JsonSerializer<Object> implements Contex
gen.writeNull(); gen.writeNull();
return; return;
} }
String result = trans.translation(value, translation.other()); Object result = trans.translation(value, translation.other());
gen.writeString(result); gen.writeObject(result);
} else { } else {
gen.writeObject(value); gen.writeObject(value);
} }

@ -16,7 +16,7 @@ import org.springframework.stereotype.Component;
@Component @Component
@AllArgsConstructor @AllArgsConstructor
@TranslationType(type = TransConstant.DEPT_ID_TO_NAME) @TranslationType(type = TransConstant.DEPT_ID_TO_NAME)
public class DeptNameTranslationImpl implements TranslationInterface { public class DeptNameTranslationImpl implements TranslationInterface<String> {
@DubboReference @DubboReference
private RemoteDeptService remoteDeptService; private RemoteDeptService remoteDeptService;

@ -16,7 +16,7 @@ import org.springframework.stereotype.Component;
@Component @Component
@AllArgsConstructor @AllArgsConstructor
@TranslationType(type = TransConstant.DICT_TYPE_TO_LABEL) @TranslationType(type = TransConstant.DICT_TYPE_TO_LABEL)
public class DictTypeTranslationImpl implements TranslationInterface { public class DictTypeTranslationImpl implements TranslationInterface<String> {
private final DictService dictService; private final DictService dictService;

@ -16,7 +16,7 @@ import org.springframework.stereotype.Component;
@Component @Component
@AllArgsConstructor @AllArgsConstructor
@TranslationType(type = TransConstant.OSS_ID_TO_URL) @TranslationType(type = TransConstant.OSS_ID_TO_URL)
public class OssUrlTranslationImpl implements TranslationInterface { public class OssUrlTranslationImpl implements TranslationInterface<String> {
@DubboReference @DubboReference
private RemoteFileService ossService; private RemoteFileService ossService;

@ -16,7 +16,7 @@ import org.springframework.stereotype.Component;
@Component @Component
@AllArgsConstructor @AllArgsConstructor
@TranslationType(type = TransConstant.USER_ID_TO_NAME) @TranslationType(type = TransConstant.USER_ID_TO_NAME)
public class UserNameTranslationImpl implements TranslationInterface { public class UserNameTranslationImpl implements TranslationInterface<String> {
@DubboReference @DubboReference
private RemoteUserService remoteUserService; private RemoteUserService remoteUserService;

Loading…
Cancel
Save