简体   繁体   English

如何从java中的目录中获取资源包文件?

[英]How to get resource bundle file from a directory in java?

I have a code to internationalize an application.我有一个国际化应用程序的代码。 I need to load the bundle file, go back twice from the running location and load it.我需要加载捆绑文件,从运行位置返回两次并加载它。

My code is,我的代码是,

bundle = ResourceBundle.getBundle("../../resources/basic",new Locale("fr", "CA"));
lblUsername.setText(bundle.getString("username"));
lblPassword.setText(bundle.getString("password"));
btnLogin.setText(bundle.getString("login"));

I got the following error.我收到以下错误。

java.util.MissingResourceException: Can't find bundle for base name ../../resources/basic, locale fr_CA
at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
at java.util.ResourceBundle.getBundleImpl(Unknown Source)
at java.util.ResourceBundle.getBundle(Unknown Source)
at com.daycare.ui.user.Login$4.itemStateChanged(Login.java:248)
at javax.swing.JComboBox.fireItemStateChanged(Unknown Source)
at javax.swing.JComboBox.selectedItemChanged(Unknown Source)
at javax.swing.JComboBox.contentsChanged(Unknown Source)
at javax.swing.AbstractListModel.fireContentsChanged(Unknown Source)
at javax.swing.DefaultComboBoxModel.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at com.daycare.ui.user.Login.<init>(Login.java:372)
at com.daycare.ui.user.Login$1.run(Login.java:104)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

How can I give the correct path of budle file?我怎样才能给出budle文件的正确路径?

Thanks in Advance!提前致谢!

As far as I remember the Bundle class will lookup by default on the current ClassLoader to find your resource.据我所知,默认情况下Bundle类将在当前ClassLoader上查找以查找您的资源。 If you want to look a file on the filesystem, please use that instead:如果要查看文件系统上的文件,请改用它:

File file = new File("the path of the folder containing the bundles");
URL[] urls = new URL[]{file.toURI().toURL()};
ClassLoader loader = new URLClassLoader(urls);
ResourceBundle rb = ResourceBundle.getBundle("the bundle name", your_locale, loader);
ResourceBundle.getBundle("../../resources/basic",new Locale("fr", "CA")); 

change the first argument to fully qualified class name and make sure that basic_fr_CA.properties exists.将第一个参数更改为完全限定的类名,并确保 basic_fr_CA.properties 存在。

for example if your file is at例如,如果您的文件位于

/resource/basic_fr_CA.properties location, 

then change your java code to然后将您的java代码更改为

ResourceBundle.getBundle("resources.basic",new Locale("fr", "CA"));

If your resource is in resources folder, you can use this way, which is better in that you do not have to deal with checked exceptions:如果您的资源在资源文件夹中,您可以使用这种方式,这样更好,您不必处理已检查的异常:

ClassLoader loader = new URLClassLoader(new URL[]{ I18NManager.class.getResource("path to folder containing bundles you want to load")});
ResourceBundle rb = ResourceBundle.getBundle("the bundle name", locale);

where I18NManager represents class which is loading resource.其中I18NManager代表正在加载资源的类。

I18NManager.class can be replaced with getClass() if it is not in static context. I18NManager.class不在静态上下文中,可以用getClass()替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM