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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20223214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 00:24:31  来源:igfitidea点击:

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

javainternationalization

提问by user2999888

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.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.run(Login.java:104)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.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!

提前致谢!

回答by benjamin.d

As far as I remember the Bundleclass will lookup by default on the current ClassLoaderto find your resource. If you want to look a file on the filesystem, please use that instead:

据我所知,Bundle该类将默认在当前ClassLoader查找您的资源。如果要查看文件系统上的文件,请改用它:

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);

回答by Santosh Joshi

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"));