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

This commit is contained in:
疯狂的狮子Li 2023-02-18 23:40:57 +08:00
parent c6746ed788
commit a49ed727b6
7 changed files with 15 additions and 14 deletions

View File

@ -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);

View File

@ -5,13 +5,14 @@ package com.ruoyi.common.translation.core;
* *
* @author Lion Li * @author Lion Li
*/ */
public interface TranslationInterface { public interface TranslationInterface<T> {
/** /**
* 翻译 * 翻译
* *
* @param key 需要被翻译的键(不为空) * @param key 需要被翻译的键(不为空)
* @param other 其他参数
* @return 返回键对应的值 * @return 返回键对应的值
*/ */
String translation(Object key, String other); T translation(Object key, String other);
} }

View File

@ -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);
} }

View File

@ -15,7 +15,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> {
private final DeptService deptService; private final DeptService deptService;

View File

@ -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;

View File

@ -15,7 +15,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> {
private final OssService ossService; private final OssService ossService;

View File

@ -15,7 +15,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> {
private final UserService userService; private final UserService userService;