mirror of
https://github.com/dromara/RuoYi-Vue-Plus.git
synced 2026-06-09 15:26:09 +00:00
update 增加多个翻译方法的注释以提升代码可读性
This commit is contained in:
@@ -26,12 +26,26 @@ import java.util.Set;
|
||||
@Order(0)
|
||||
public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
|
||||
/**
|
||||
* 响应增强上下文中保存待批量翻译数据的属性键。
|
||||
*/
|
||||
private static final String ATTR_BATCHES = TranslationJsonFieldProcessor.class.getName() + ".batches";
|
||||
|
||||
/**
|
||||
* 响应增强上下文中保存批量翻译结果的属性键。
|
||||
*/
|
||||
private static final String ATTR_RESULTS = TranslationJsonFieldProcessor.class.getName() + ".results";
|
||||
|
||||
/**
|
||||
* 翻译类型与翻译实现映射。
|
||||
*/
|
||||
private final Map<String, TranslationInterface<?>> translationMap;
|
||||
|
||||
/**
|
||||
* 构造翻译响应处理器。
|
||||
*
|
||||
* @param translations 翻译实现列表
|
||||
*/
|
||||
public TranslationJsonFieldProcessor(List<TranslationInterface<?>> translations) {
|
||||
Map<String, TranslationInterface<?>> map = new LinkedHashMap<>(translations.size());
|
||||
for (TranslationInterface<?> t : translations) {
|
||||
@@ -43,6 +57,12 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
this.translationMap = Collections.unmodifiableMap(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收集字段上的翻译注解和原始值,供后续批量翻译使用。
|
||||
*
|
||||
* @param fieldContext 字段上下文
|
||||
* @param context 响应增强上下文
|
||||
*/
|
||||
@Override
|
||||
public void collect(JsonFieldContext fieldContext, JsonEnhancementContext context) {
|
||||
Translation translation = fieldContext.getAnnotation(Translation.class);
|
||||
@@ -58,6 +78,11 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
.add(sourceValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据收集到的原始值执行批量翻译,并将翻译结果写入响应增强上下文。
|
||||
*
|
||||
* @param context 响应增强上下文
|
||||
*/
|
||||
@Override
|
||||
public void prepare(JsonEnhancementContext context) {
|
||||
Map<TranslationBatchKey, Set<Object>> batches = context.getAttribute(ATTR_BATCHES);
|
||||
@@ -80,6 +105,14 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
context.setAttribute(ATTR_RESULTS, results);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理单个字段的翻译值,优先使用批量翻译结果,缺失时回退到单值翻译。
|
||||
*
|
||||
* @param fieldContext 字段上下文
|
||||
* @param value 当前字段值
|
||||
* @param context 响应增强上下文
|
||||
* @return 翻译后的字段值
|
||||
*/
|
||||
@Override
|
||||
public Object process(JsonFieldContext fieldContext, Object value, JsonEnhancementContext context) {
|
||||
Translation translation = fieldContext.getAnnotation(Translation.class);
|
||||
@@ -110,6 +143,12 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取或创建待批量翻译数据映射。
|
||||
*
|
||||
* @param context 响应增强上下文
|
||||
* @return 待批量翻译数据映射
|
||||
*/
|
||||
private Map<TranslationBatchKey, Set<Object>> getOrCreateBatches(JsonEnhancementContext context) {
|
||||
Map<TranslationBatchKey, Set<Object>> batches = context.getAttribute(ATTR_BATCHES);
|
||||
if (batches == null) {
|
||||
@@ -119,6 +158,13 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
return batches;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析翻译原始值。
|
||||
*
|
||||
* @param fieldContext 字段上下文
|
||||
* @param translation 翻译注解
|
||||
* @return 翻译原始值
|
||||
*/
|
||||
private Object resolveSourceValue(JsonFieldContext fieldContext, Translation translation) {
|
||||
if (StringUtils.isNotBlank(translation.mapper())) {
|
||||
return ReflectUtils.invokeGetter(fieldContext.owner(), translation.mapper());
|
||||
@@ -126,10 +172,22 @@ public class TranslationJsonFieldProcessor implements JsonFieldProcessor {
|
||||
return fieldContext.value();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据翻译类型获取翻译实现。
|
||||
*
|
||||
* @param type 翻译类型
|
||||
* @return 翻译实现
|
||||
*/
|
||||
private TranslationInterface<?> getTranslation(String type) {
|
||||
return translationMap.get(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量翻译分组键。
|
||||
*
|
||||
* @param type 翻译类型
|
||||
* @param other 额外参数
|
||||
*/
|
||||
private record TranslationBatchKey(String type, String other) {
|
||||
}
|
||||
|
||||
|
||||
@@ -39,6 +39,13 @@ public class DeptNameTranslationImpl implements TranslationInterface<String> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将部门 ID 翻译为部门名称。
|
||||
*
|
||||
* @param keys 部门 ID 集合
|
||||
* @param other 额外参数
|
||||
* @return 部门 ID 与部门名称映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
Set<Long> deptIds = collectLongIds(keys);
|
||||
@@ -53,6 +60,13 @@ public class DeptNameTranslationImpl implements TranslationInterface<String> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始键构建部门名称翻译值。
|
||||
*
|
||||
* @param source 原始键
|
||||
* @param deptNames 部门 ID 与部门名称映射
|
||||
* @return 部门名称
|
||||
*/
|
||||
private String buildValue(Object source, Map<Long, String> deptNames) {
|
||||
if (source instanceof String ids) {
|
||||
return joinMappedValues(ids, deptNames::get);
|
||||
|
||||
@@ -39,6 +39,13 @@ public class DictTypeTranslationImpl implements TranslationInterface<String> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量根据字典类型和字典值翻译显示标签。
|
||||
*
|
||||
* @param keys 字典值集合
|
||||
* @param other 字典类型
|
||||
* @return 字典值与字典标签映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
if (keys.isEmpty() || StringUtils.isBlank(other)) {
|
||||
|
||||
@@ -39,6 +39,13 @@ public class NicknameTranslationImpl implements TranslationInterface<String> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将用户 ID 翻译为用户昵称。
|
||||
*
|
||||
* @param keys 用户 ID 集合
|
||||
* @param other 额外参数
|
||||
* @return 用户 ID 与用户昵称映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
Set<Long> userIds = collectLongIds(keys);
|
||||
@@ -53,6 +60,13 @@ public class NicknameTranslationImpl implements TranslationInterface<String> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始键构建用户昵称翻译值。
|
||||
*
|
||||
* @param source 原始键
|
||||
* @param userNames 用户 ID 与用户昵称映射
|
||||
* @return 用户昵称
|
||||
*/
|
||||
private String buildValue(Object source, Map<Long, String> userNames) {
|
||||
if (source instanceof String ids) {
|
||||
return joinMappedValues(ids, userNames::get);
|
||||
|
||||
@@ -42,6 +42,13 @@ public class OssUrlTranslationImpl implements TranslationInterface<String> {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将 OSS ID 翻译为访问地址。
|
||||
*
|
||||
* @param keys OSS ID 集合
|
||||
* @param other 额外参数
|
||||
* @return OSS ID 与访问地址映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
Set<Long> ossIds = collectLongIds(keys);
|
||||
@@ -57,6 +64,13 @@ public class OssUrlTranslationImpl implements TranslationInterface<String> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始键构建 OSS 地址翻译值。
|
||||
*
|
||||
* @param source 原始键
|
||||
* @param ossUrls OSS ID 与访问地址映射
|
||||
* @return OSS 访问地址
|
||||
*/
|
||||
private String buildValue(Object source, Map<Long, String> ossUrls) {
|
||||
if (source instanceof String ids) {
|
||||
return joinMappedValues(ids, ossUrls::get);
|
||||
|
||||
@@ -36,6 +36,13 @@ public class UserNameTranslationImpl implements TranslationInterface<String> {
|
||||
return userService.selectUserNameById(Convert.toLong(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将用户 ID 翻译为用户名。
|
||||
*
|
||||
* @param keys 用户 ID 集合
|
||||
* @param other 额外参数
|
||||
* @return 用户 ID 与用户名映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
Set<Long> userIds = collectLongIds(keys);
|
||||
@@ -50,6 +57,13 @@ public class UserNameTranslationImpl implements TranslationInterface<String> {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始键构建用户名翻译值。
|
||||
*
|
||||
* @param source 原始键
|
||||
* @param userNames 用户 ID 与用户名映射
|
||||
* @return 用户名
|
||||
*/
|
||||
private String buildValue(Object source, Map<Long, String> userNames) {
|
||||
if (source instanceof String ids) {
|
||||
return joinMappedValues(ids, userNames::get);
|
||||
|
||||
@@ -29,11 +29,25 @@ public class CategoryNameTranslationImpl implements TranslationInterface<String>
|
||||
|
||||
private final IFlwCategoryService flwCategoryService;
|
||||
|
||||
/**
|
||||
* 将流程分类 ID 翻译为分类名称。
|
||||
*
|
||||
* @param key 流程分类 ID
|
||||
* @param other 额外参数
|
||||
* @return 流程分类名称
|
||||
*/
|
||||
@Override
|
||||
public String translation(Object key, String other) {
|
||||
return flwCategoryService.selectCategoryNameById(Convert.toLong(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将流程分类 ID 翻译为分类名称。
|
||||
*
|
||||
* @param keys 流程分类 ID 集合
|
||||
* @param other 额外参数
|
||||
* @return 流程分类 ID 与分类名称映射
|
||||
*/
|
||||
@Override
|
||||
public Map<Object, String> translationBatch(Set<Object> keys, String other) {
|
||||
Set<Long> categoryIds = collectLongIds(keys);
|
||||
@@ -48,6 +62,13 @@ public class CategoryNameTranslationImpl implements TranslationInterface<String>
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据原始键构建流程分类名称翻译值。
|
||||
*
|
||||
* @param source 原始键
|
||||
* @param categoryNames 流程分类 ID 与分类名称映射
|
||||
* @return 流程分类名称
|
||||
*/
|
||||
private String buildValue(Object source, Map<Long, String> categoryNames) {
|
||||
if (source instanceof String ids) {
|
||||
return joinMappedValues(ids, categoryNames::get);
|
||||
|
||||
Reference in New Issue
Block a user