简体   繁体   中英

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?

Thanks in Advance!

As far as I remember the Bundle class will lookup by default on the current ClassLoader to find your resource. 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.

for example if your file is at

/resource/basic_fr_CA.properties location, 

then change your java code to

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.class can be replaced with getClass() if it is not in static context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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