日期:2014-05-20  浏览次数:20700 次

java国际化问题ResourceBundle
private static Locale defaultLocale = new Locale("zh", CN);
public static String getMessage(String msg,Object...objects) {
ResourceBundle resourceBundle = ResourceBundle.getBundle("MessageResource", defaultLocale);
MessageFormat messageFormat = new MessageFormat(resourceBundle.getString(msg));
String i18nMsg = messageFormat.format(objects);
return i18nMsg;
}
如何将国际化MessageResource_zh_CN.properties文件放到其它目录(不在项目目录),通过路径读取国际化文件?
或者有其它国际化的方法也可以。只要可以自定义国际化文件路径。
求解...
先谢了!!!


------解决方案--------------------
Java code

Properties prop = new Properties();
prop.load(new FileInputStream("xxxx.properties"));

------解决方案--------------------
不再项目目录,楼上这个
Properties prop = new Properties();
prop.load(new FileInputStream("xxxx.properties"));
要用绝对路径了把,这样移植性不高哦,最好是放在src下,它也是源代码的一部分吗。
------解决方案--------------------
放在classpath能找到的地方,然后相对于根写相对路径,这样比较好,也便于发布,比如放在某个包里。
------解决方案--------------------

Properties prop = new Properties();
prop.load(new FileInputStream("xxxx.properties"));

这个如果不在项目目录下,可以用ls这种方法:
测试了一下,可以用。properties文件在项目的根目录下。
Java code

public class MyUserDetailService {
    
    public static void main(String[] args){
        InputStream in = MyUserDetailService.class.getClassLoader().getResourceAsStream("..\\db_config.properties");
        Properties pro = new Properties();
        try {
            pro.load(in);
            System.out.println(pro.getProperty("driver"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

------解决方案--------------------
探讨

可是国际化文件properties中,有时有带参数的国际化,用Properties prop = new Properties();
prop.load(new FileInputStream("xxxx.properties"));可能没法做到...
求解》。。