EasyPoi实现多语言国际化

以下简单建立一个实体类,增长easypoi的@Excel注解,注解中name的值,便是导出的时候表头的信息,如今以逗号分隔三种语言的文字,根据传入语言标识,从新设置对应的文字。

方法一:

@Data
public class StaOrgPost {

    @Excel(name = "岗位名称,崗位名稱,Post Name", width = 40)
    private String postName;

    @Excel(name = "岗位人数,崗位人數,Post Count", width = 10)
    private Integer userCnt;

}

建立一个导出语言工具类,利用反射,从新赋予@Excel中name字段的值code

public class ExcelLangUtils {

    private static final int VALUE_SIZE = 3;

    private ExcelLangUtils() {
    }

    public static Class chooseLang(Class<?> pojoClass, String lang)
            throws NoSuchFieldException, IllegalAccessException {

        //获取实体类中全部字段
        Field[] fields = pojoClass.getDeclaredFields();

        for (Field field : fields) {
            // 获取字段上的注解
            Excel anoExcel = field.getAnnotation(Excel.class);
            if (anoExcel != null) {
                // 获取代理处理器
                InvocationHandler invocationHandler = Proxy.getInvocationHandler(anoExcel);
                // 获取私有 memberValues 属性
                Field f = invocationHandler.getClass().getDeclaredField("memberValues");
                f.setAccessible(true);
                // 获取实例的属性map
                Map<String, Object> memberValues = (Map<String, Object>) f.get(invocationHandler);
                // 获取属性值
                String excelValue = (String) memberValues.get("name");

                if (StringUtils.isNotBlank(excelValue)) {
                    //根据传入的语言标识,从新设置属性值
                    List<String> valueList = Arrays.asList(excelValue.split(","));
                    if (valueList.size() == VALUE_SIZE) {
                        if (RptConstants.LANGUAGE_ZH.equals(lang)) {
                            memberValues.put("name", valueList.get(0));
                        }
                        if (RptConstants.LANGUAGE_TW.equals(lang)) {
                            memberValues.put("name", valueList.get(1));
                        }
                        if (RptConstants.LANGUAGE_EN.equals(lang)) {
                            memberValues.put("name", valueList.get(2));
                        }
                    }
                }
            }
        }
        return pojoClass;
    }
}

使用:

workbook = ExcelExportUtil.exportBigExcel(exportParams,    ExcelLangUtils.chooseLang(StaOrgPost.class, RptConstants.LANGUAGE_TW), staOrgPostList);

方法二:

@Data
public class StaOrgPost {

    @Excel(name = "system.i18n.postName", width = 40)
    private String postName;

    @Excel(name = "system.i18n.userCnt", width = 10)
    private Integer userCnt;

}

建立一个导出语言工具类,利用反射,从新赋予@Excel中name字段的值code

public class ExcelPoiLangUtils {

    private ExcelPoiLangUtils() {
    }

    public static Class chooseLang(Class<?> pojoClass) {
        try{
            //获取实体类中全部字段
            Field[] fields = pojoClass.getDeclaredFields();

            for (Field field : fields) {
                // 获取字段上的注解
                Excel anoExcel = field.getAnnotation(Excel.class);
                if (anoExcel != null) {
                    // 获取代理处理器
                    InvocationHandler invocationHandler = Proxy.getInvocationHandler(anoExcel);
                    // 获取私有 memberValues 属性
                    Field f = invocationHandler.getClass().getDeclaredField("memberValues");
                    f.setAccessible(true);
                    // 获取实例的属性map
                    Map<String, Object> memberValues = (Map<String, Object>) f.get(invocationHandler);
                    // 获取属性值
                    String excelValue = (String) memberValues.get("name");

                    if (StringUtils.isNotBlank(excelValue)) {

                        //根据配置的语言标识,重新新设置属性值
                        //MegUtils.getMessage("media.web.video.plan.header.data.1")
                        String  value = MegUtils.getMessage(excelValue);
                        if(StringUtils.isNotBlank(value)){
                            memberValues.put("name", value);
                        }else{
                            memberValues.put("name", excelValue);
                        }
                    }
                }
            }
        }catch (NoSuchFieldException e){
            log.error(e.getMessage(), e);
        }catch (IllegalAccessException e){
            log.error(e.getMessage(), e);
        }

        return pojoClass;
    }
}

使用:

workbook = ExcelExportUtil.exportBigExcel(exportParams,    ExcelLangUtils.chooseLang(StaOrgPost.class), staOrgPostList);

读取国际化资源的方法:

/**
 * 读取国际化资源
 */
public class MegUtils {
    public static MessageSource messageSource = ApplicationContextUtil.getBean(MessageSource.class);

    /**
     * 根据参数获取国际化资源,委托给Spring的MessageSource
     * @param code
     * @param args
     * @return
     */
    public static final String getMessage(String code,Object ...args){
        return  messageSource.getMessage(code,args, LocaleContextHolder.getLocale());
    }
}

@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        MediaApplicationContextUtil.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> requiredType){
        return applicationContext.getBean(requiredType);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值