SSM项目-医药采购-04 国际化读取资源文件

一、国际化 读取资源文件

private static final long serialVersionUID = -9217951614629363385L;

    /**
     * 系统语言环境,默认为中文zh
     */
    public static final String LANGUAGE = "zh";

    /**
     * 系统国家环境,默认为中国CN
     */
    public static final String COUNTRY = "CN";

    private static Locale getLocale() {
        Locale locale = new Locale(LANGUAGE, COUNTRY);
        return locale;
    } 

    /**
     * 根据语言、国家、资源文件名和key名字获取资源文件值
     * @param baseName 资源文件名
     * @param language 语言
     * @param country  国家
     * @param section  key名字
     * @return 值
     */
    private static String getProperties(String baseName, String section) {
        String retValue = "";
        try {
            Locale locale = getLocale();
            ResourceBundle rb = ResourceBundle.getBundle(baseName, locale);
            retValue = (String) rb.getObject(section);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retValue;
    }

    /**
     * 通过key从资源文件读取内容
     * @param fileName 资源文件名
     * @param key 索引
     * @return 索引对应的内容
     */
    public static String getValue(String fileName,String key) {
        return getProperties(fileName, key);
    }

    /**
     * 获取资源文件下所有的索引名
     * @param baseName 资源文件名
     * @return
     */
    public static List<String> getKeyList(String baseName) {
        Locale locale = getLocale();
        ResourceBundle rb = ResourceBundle.getBundle(baseName,locale);

        List<String> reslist = new ArrayList<String>();
        Set<String> keyset = rb.keySet();
        for (Iterator<String> it = keyset.iterator(); it.hasNext();) {
            String lkey = it.next();
            reslist.add(lkey);
        }

        return reslist;
    }

    /** 通过key从资源文件读取内容,并格式化
     * @param fileName 资源文件名
     * @param key  索引
     * @param objs格式化参数
     * @return 格式化后的内容*/
    public static String getValue(String fileName, String key, Object[] objs) {
        String pattern = getValue(fileName, key);
        String value = MessageFormat.format(pattern, objs);
        return value;
    }

    public static void main(String[] args) {
         System.out.println(getValue("resources.messages", "105", null));
         //信息格式化,如果资源中有{}的参数则需要使用MessageFormat格式化,Object[]为传递的参数,数量根据资源文件中的{}个数决定
        // System.out.println(getValue("resources.messages", "102", new Object[]{100,200}));
    }

对代码进行封装

// 原来都要这样抛出异常
throw new ExceptionResultInfo(resultInfo);
// 封装后一行代码
ResultUtil.throwExcepion(ResultUtil.createFail(Config.MESSAGE, 213, null));
   // 分析 - 1.首先是抛出异常的方法
   public static void throwExcepion(ResultInfo resultInfo) throws ExceptionResultInfo{
        throw new ExceptionResultInfo(resultInfo);
    }

// 2. 工具类 创建错误结果
public static ResultInfo createFail(String fileName,int messageCode,Object[] objs){
        String message=null;
        if(objs == null){
            message = ResourcesUtil.getValue(fileName, messageCode+"");
        }else{
            message = ResourcesUtil.getValue(fileName, messageCode+"",objs);
        }
        return new ResultInfo(ResultInfo.TYPE_RESULT_FAIL,messageCode,message);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值