【问题标题】:Where to put properties files when dealing with internationalization?处理国际化时将属性文件放在哪里?
【发布时间】:2021-05-20 17:40:51
【问题描述】:

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


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 类放在同一个包中,所以我认为它应该可以工作。

【问题讨论】:

    标签: java internationalization


    【解决方案1】:

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

    否则改变:

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

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

    使其与类目录/包内的捆绑包一起使用。

    【讨论】:

    • 谢谢,我绝对需要阅读类路径,因为到目前为止我试图避免这个话题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 2015-01-31
    • 2016-09-20
    • 1970-01-01
    相关资源
    最近更新 更多