How to use ResourceBundle

I am trying to understand the internalization of java applications as shown here . I can not though. I created a class that extends ListResourceBundle as indicated, and tried to extract the keys. However, I still get an exception. If you look at the tutorial, it says use .class files. It may be wrong, right?

[DESIGN TREE]

enter image description here

[Source]

Here are the two classes that I use, only one of the MainWindow_xx_XX.java files, since they are basically the same. ListResourceBundle first:

 public class MainWindow_en_US extends ListResourceBundle { @Override protected Object[][] getContents() { return contents; } private Object[][] contents = { {"fileLabel", "File"}, {"newSessionLabel", "New session..."}, {"openSessionLabel", "Open session..."}, {"saveLabel", "Save"}, {"exitLabel", "Exit"}, {"editLabel", "Edit"}, {"toolsLabel", "Tools"}, {"helpLabel", "Help"} }; } 

And now the method that I use to download it:

  private static final int DEFAULT_LOCALE = 0; private ResourceBundle bundle; public static Locale locale; public static final Locale[] supportedLocales = { new Locale("en", "US"), new Locale("es", "ES") }; public MainWindow() { for (Locale i : supportedLocales) { if (i.getLanguage().equals(Locale.getDefault().getLanguage())) { locale = i; break; } else { locale = supportedLocales[DEFAULT_LOCALE]; } } bundle = ResourceBundle.getBundle("MainWindow", locale); //EXCEPTION POINTS HERE!!! initComponents(); } 

I get the following exception. I know that I can do this through property files, but this fuzzily explains me the fact that I cannot get Oracle to use an extremely simple tutorial.

 Exception in thread "AWT-EventQueue-0" java.util.MissingResourceException: Can't find bundle for base name MainWindow, locale es_ES at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1499) at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1322) at java.util.ResourceBundle.getBundle(ResourceBundle.java:796) at -.-.-.-.-.<init>(MainWindow.java:37) at -.-.-.-.MainWindow$2.run(MainWindow.java:145) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701) at java.awt.EventQueue.access$000(EventQueue.java:102) at java.awt.EventQueue$3.run(EventQueue.java:662) at java.awt.EventQueue$3.run(EventQueue.java:660) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:671) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) 
+7
source share
2 answers

You need to use the full base name:

 bundle = ResourceBundle.getBundle("pkg.subpkg.resources.MainWindow", locale); 
+11
source

You cannot name the file MainWindow_es_ES . To solve the problem, rename the file to MainWindow es .

 Locale locale = new Locale("es"); ResourceBundle rb = ResourceBundle.getBundle("yourPackageName.MainWindow", locale); System.out.print(rb.getObject("YourKey")); 
+2
source

All Articles