⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resbundle.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
字号:
package cn.js.fan.util;

import java.util.*;

import org.apache.log4j.Logger;
import cn.js.fan.web.Global;

public class ResBundle {
    // 默认字符集为简体中文
    Logger logger = Logger.getLogger(this.getClass().getName());
    Locale locale;
    ResourceBundle bundle;
    String encode = "";

    public ResBundle(String resName, Locale locale) {
        if (locale == null) {
            try {
                locale = Global.locale;
            } catch (Exception e) {
                logger.error("ResBundle1:" + e.getMessage());
                locale = Locale.getDefault();
            }
        }
        this.locale = locale;

        if (locale.getLanguage().equals("zh")) {
            if (locale.getCountry().equals("TW"))
                this.encode = "BIG5";
            else if (locale.getCountry().equals("CN"))
                this.encode = "gb2312";
        }
        this.resName = resName;
        try {
            bundle = ResourceBundle.getBundle(resName, locale);
        } catch (MissingResourceException e) {
            logger.error("ResBundle2:" + e.getMessage());
            // bundle = ResourceBundle.getBundle(resName, Locale.CHINA);
        }
    }

    public String get(String key) {
        String str = bundle.getString(key);
        if (str==null)
            return "";
        if (!encode.equals("")) {
            try {
                // gb2312的内容可以通过如下方式处理,但big5却不行
                // str = new String(str.getBytes("ISO8859-1"), encode);

                // 对于gb2312,资源文件不需要转成utf-8编码,转了反而会变成乱码,而big5的资源文件需转换为utf-8编码
                // 晕,在本机区域改为台湾时,gb2312资源文件不能转换为utf8编码,而当本机区域改回中国即简体时,gb2312的资源文件需转成utf8编码
                str = new String(str.getBytes("ISO8859-1"), "utf-8");
            } catch (java.io.UnsupportedEncodingException ex) {
                ex.printStackTrace();
            }
        }
        return str;
    }

    public void setResName(String resName) {
        this.resName = resName;
    }

    public String getResName() {
        return resName;
    }

    private String resName;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -