update 优化 翻译组件 支持返回值泛型 支持多种类型数据翻译(例如: 根据主键翻译成对象)
| | |
| | | public class TranslationConfig { |
| | | |
| | | @Autowired |
| | | private List<TranslationInterface> list; |
| | | private List<TranslationInterface<?>> list; |
| | | |
| | | @Autowired |
| | | private ObjectMapper objectMapper; |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | | Map<String, TranslationInterface> map = new HashMap<>(list.size()); |
| | | for (TranslationInterface trans : list) { |
| | | 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); |
| | |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface TranslationInterface { |
| | | public interface TranslationInterface<T> { |
| | | |
| | | /** |
| | | * 翻译 |
| | | * |
| | | * @param key 需要被翻译的键(不为空) |
| | | * @param key 需要被翻译的键(不为空) |
| | | * @param other 其他参数 |
| | | * @return 返回键对应的值 |
| | | */ |
| | | String translation(Object key, String other); |
| | | T translation(Object key, String other); |
| | | } |
| | |
| | | /** |
| | | * 全局翻译实现类映射器 |
| | | */ |
| | | public static final Map<String, TranslationInterface> TRANSLATION_MAPPER = new ConcurrentHashMap<>(); |
| | | 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 { |
| | | TranslationInterface trans = TRANSLATION_MAPPER.get(translation.type()); |
| | | TranslationInterface<?> trans = TRANSLATION_MAPPER.get(translation.type()); |
| | | if (ObjectUtil.isNotNull(trans)) { |
| | | // 如果映射字段不为空 则取映射字段的值 |
| | | if (StringUtils.isNotBlank(translation.mapper())) { |
| | |
| | | gen.writeNull(); |
| | | return; |
| | | } |
| | | String result = trans.translation(value, translation.other()); |
| | | gen.writeString(result); |
| | | Object result = trans.translation(value, translation.other()); |
| | | gen.writeObject(result); |
| | | } else { |
| | | gen.writeObject(value); |
| | | } |
| | |
| | | @Component |
| | | @AllArgsConstructor |
| | | @TranslationType(type = TransConstant.DEPT_ID_TO_NAME) |
| | | public class DeptNameTranslationImpl implements TranslationInterface { |
| | | public class DeptNameTranslationImpl implements TranslationInterface<String> { |
| | | |
| | | private final DeptService deptService; |
| | | |
| | |
| | | @Component |
| | | @AllArgsConstructor |
| | | @TranslationType(type = TransConstant.DICT_TYPE_TO_LABEL) |
| | | public class DictTypeTranslationImpl implements TranslationInterface { |
| | | public class DictTypeTranslationImpl implements TranslationInterface<String> { |
| | | |
| | | private final DictService dictService; |
| | | |
| | |
| | | @Component |
| | | @AllArgsConstructor |
| | | @TranslationType(type = TransConstant.OSS_ID_TO_URL) |
| | | public class OssUrlTranslationImpl implements TranslationInterface { |
| | | public class OssUrlTranslationImpl implements TranslationInterface<String> { |
| | | |
| | | private final OssService ossService; |
| | | |
| | |
| | | @Component |
| | | @AllArgsConstructor |
| | | @TranslationType(type = TransConstant.USER_ID_TO_NAME) |
| | | public class UserNameTranslationImpl implements TranslationInterface { |
| | | public class UserNameTranslationImpl implements TranslationInterface<String> { |
| | | |
| | | private final UserService userService; |
| | | |