阿里 EasyExcel 表头国际化

实体类字段使用EasyExcel提供的注解@ExcelProperty,value 值写成占位符形式 ,匹配 i18n 文件里面的编码。

如:

java 复制代码
/**
 * 仓库名称
 */
@ExcelProperty("{warehouse.record.warehouseName}")
private String warehouseName;

占位符解析器

java 复制代码
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PlaceholderResolver {

    /**
     * 默认前缀占位符
     */
    public static final String DEFAULT_PLACEHOLDER_PREFIX = "{";

    /**
     * 默认后缀占位符
     */
    public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";

    /**
     * 默认单例占位符解析器,即占位符前缀为"{", 后缀为"}"
     */
    @Getter
    private static final PlaceholderResolver defaultResolver = new PlaceholderResolver();

    /**
     * 占位符前缀
     */
    private String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;

    /**
     * 占位符后缀
     */
    private String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;

    /**
     * 根据替换规则来替换指定模板中的占位符值
     *
     * @param content 要解析的字符串
     * @param rule    解析规则回调
     */
    public String resolveByRule(String content, Function<String, String> rule) {
        int start = content.indexOf(this.placeholderPrefix);
        if (start == -1) {
            return content;
        }
        StringBuilder result = new StringBuilder(content);
        while (start != -1) {
            int end = result.indexOf(this.placeholderSuffix, start);
            //获取占位符属性值,如{id}, 即获取id
            String placeholder = result.substring(start + this.placeholderPrefix.length(), end);
            //替换整个占位符内容,即将{id}值替换为替换规则回调中的内容
            String replaceContent = placeholder.trim().isEmpty() ? "" : rule.apply(placeholder);
            result.replace(start, end + this.placeholderSuffix.length(), replaceContent);
            start = result.indexOf(this.placeholderPrefix, start + replaceContent.length());
        }
        return result.toString();
    }
}

自定义拦截器

java 复制代码
@RequiredArgsConstructor
public class I18nCellWriteHandler implements CellWriteHandler {

    @Resource
    private MessageSource messageSource;

    @Override
    public void beforeCellCreate(CellWriteHandlerContext context) {
        if (!context.getHead()) {
            return;
        }
        final List<String> originHeadNames = context.getHeadData().getHeadNameList();
        if (CollectionUtils.isEmpty(originHeadNames)) {
            return;
        }
        List<String> newHeadNames = originHeadNames.stream().
                map(headName -> PlaceholderResolver.getDefaultResolver()
                        .resolveByRule(headName, this::getMessage)).
                collect(Collectors.toList());
        context.getHeadData().setHeadNameList(newHeadNames);
    }

    public String getMessage(String code) {
        Locale locale = LocaleContextHolder.getLocale();
        return messageSource.getMessage(code, null, locale);
    }
}
相关推荐
geovindu2 分钟前
rust:N-Barrier Pattern
开发语言·后端·设计模式·rust·屏障模式·并发型模式
梦想出海-Phoebe5 分钟前
Gemini新增文件编辑功能:PDF、Word、Excel处理体验分享
pdf·word·excel
AI宇宙狂想家7 分钟前
开源 AI 视频工具进化史:从难用到好用,OpenMontage 改了什么
开发语言·前端·人工智能·开源·ai创业·openmontage
大鹏说大话21 分钟前
和小鹅通相近的知识付费小程序平台有哪些
开发语言
Dovis(誓平步青云)24 分钟前
启元 T1 Pro 视觉伺服实践:基于 HSV 阈值与针孔测距的启元机器人寻物实现
开发语言·人工智能·机器学习·二次开发
Ryan-Zhong36 分钟前
数字人口播视频 API 实战:Python 跑通模板合成、异步轮询与批量出片
开发语言·python·音视频
努力努力再努力wz40 分钟前
【Lua 入门系列】模块与包(一):从多文件组织到require 运行时加载机制、模块缓存、预加载搜索与 Lua 文件搜索机制
开发语言·junit·lua
fengci.44 分钟前
CTF2困难部分
开发语言·网络·python·安全·php
佳得动1 小时前
编译和链接的认识(C语言)
c语言·开发语言