简体   繁体   English

尝试将 a.properties 文件加载到资源包中时出现错误“找不到基本名称的包”

[英]Trying to load a .properties file into Resource bundle getting error "Can't find bundle for base name"

I'm trying to load a .properties file into my ResourceBundle我正在尝试将.properties文件加载到我的ResourceBundle

public class Main extends Application {

    @Override
    public void start(Stage stage) throws Exception{

        ResourceBundle bundle = ResourceBundle.getBundle("/sample/resources/language_en.properties");

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/sample/layout/test_form.fxml"), bundle);
    }
}

But I'm getting the error:但我收到错误:

Caused by: java.util.MissingResourceException: Can't find bundle for base name /sample/resources/language_en.properties, locale en_US

在此处输入图像描述

Can't figure out why.不知道为什么。 I've checked the path multiple times /sample/resources/language_en.properties for spelling mistakes.我已经多次检查路径/sample/resources/language_en.properties是否存在拼写错误。 I've also tried rebuilding the project (Using IntelliJ).我也尝试过重建项目(使用 IntelliJ)。

Does anyone have an idea why?有谁知道为什么?

The method ResourceBundle.getBundle() takes the base name of the resource bundle, a fully qualified class name (as specified by the class documentation). ResourceBundle.getBundle()方法采用资源包的基本名称,一个完全限定的 class 名称(由 class 文档指定)。 You can load your bundle like this:你可以像这样加载你的包:

// this will load the base bundle: sample/resources/language.properties
ResourceBundle bundle = ResourceBundle.getBundle("sample.resources.language");

To use the English ResourceBundle , you can specify a Locale :要使用英文ResourceBundle ,您可以指定Locale

// this will load sample/resources/language_en.properties
ResourceBundle bundle = ResourceBundle.getBundle("sample.resources.language", Locale.ENGLISH);

Please refer to this tutorial for more information: Customizing Resource Bundle Loading更多信息请参考本教程: 自定义资源包加载

When loading resource bundle don't have .properties in basename extensions.加载资源包时,基本名称扩展名中没有.properties Also, remove the leading / for the basename.此外,删除基本名称的前导/

Use ResourceBundle bundle = ResourceBundle.getBundle("sample/resources/language_en");使用ResourceBundle bundle = ResourceBundle.getBundle("sample/resources/language_en");

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

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