简体   繁体   English

如何使用ResourceBundle

[英]How to use ResourceBundle

I'm trying to grasp internalization of java applications as shown here . 我想,如图掌握的Java应用程序内这里 I can't though. 我不能 I have created a class that extends ListResourceBundle like stated and tried to retrieve the keys. 我创建了一个扩展ListResourceBundle的类,并尝试检索键。 I keep getting an exception though. 不过,我一直都在例外。 If you check out the tutorial, it says to use .class files. 如果您查看了本教程,则说明要使用.class文件。 This can't be right, can it? 这是不对的,可以吗?

[PROJECT TREE] [项目树]

在此处输入图片说明

[Source code] [源代码]

Here are the two classes I'm using, just one of the MainWindow_xx_XX.java files since they're basically the same. 这是我正在使用的两个类,只是MainWindow_xx_XX.java文件之一,因为它们基本上是相同的。 First the ListResourceBundle: 首先是ListResourceBundle:

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 I use to load 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 keep getting the following exception. 我不断收到以下异常。 I know I can do it through property files but it bugs me beyond reason the fact that I can't get Oracle's extremely simple tutorial to work. 我知道我可以通过属性文件来做到这一点,但是它使我感到烦恼,这是因为我无法使Oracle的极其简单的教程起作用。

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)

您需要使用完全限定的基本名称:

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

You can't name a file MainWindow_es_ES . 您不能将文件命名为MainWindow_es_ES To solve your problem rename your file to MainWindow es . 要解决您的问题,请将文件重命名为MainWindow es

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

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

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