处理国际化时将属性文件放在哪里?

Where to put properties files when dealing with internationalization?

我正在尝试在我的程序中进行国际化,我发现了这个简单的示例如何使用它:


package tests;

import java.util.*;

public class I18NSample {

    static public void main(String[] args) {

        String language;
        String country;

        if (args.length != 2) {
            language = new String("en");
            country = new String("US");
        } else {
            language = new String(args[0]);
            country = new String(args[1]);
        }

        Locale currentLocale;
        ResourceBundle messages;

        currentLocale = new Locale(language, country);

        messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);
        System.out.println(messages.getString("greetings"));
        System.out.println(messages.getString("inquiry"));
        System.out.println(messages.getString("farewell"));
    }
}

但每次我编译代码时都会遇到这个异常:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name messages, locale en_US

我的文件名为 MessagesBundle_en_US.properties 和 MessagesBundle_es_ES.properties,并且与 I18NSample class 放在同一个包中,所以我认为它应该可以工作。

它应该在 class 路径的根目录中,因为它默认使用非包。

否则更改:

messages = ResourceBundle.getBundle("MessagesBundle", currentLocale);

messages = ResourceBundle.getBundle("tests.MessagesBundle", currentLocale);

使其与 class 的 directory/package 内的捆绑包一起使用。