[英]Unable to load ResourceBundle with ResourceBundle.getBundle() Can't find bundle for base name , locale en_US
I see the following exception when I try to load a properties file:当我尝试加载属性文件时,我看到以下异常:
Caused by: java.util.MissingResourceException: Can't find bundle for base name /fontawesome/fontawesome, locale en_US
I'm using a maven project and my properties file is located at src\\main\\resources\\fontawesome\\fontawesome.properties
我正在使用一个 Maven 项目,我的属性文件位于src\\main\\resources\\fontawesome\\fontawesome.properties
I'm using the below code to load this file from JavaFX8 main class:我正在使用以下代码从 JavaFX8 主类加载此文件:
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setResources(ResourceBundle.getBundle("/fontawesome/fontawesome.properties"));
Trying the absolute path fails, as does renaming the file to fontawesome_en_US.properties
or fontawesome_en.properties
(as suggested in other SO posts).尝试绝对路径失败,将文件重命名为fontawesome_en_US.properties
或fontawesome_en.properties
(如其他 SO 帖子中建议的那样)也是如此。
Had to include .properties
files in the pom.xml
:必须在pom.xml
包含.properties
文件:
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.fxml</include>
<include>**/*.css</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
Do not include the .properties extension.不要包含 .properties 扩展名。
The ResourceBundle try to load the properties file from the current classpath. ResourceBundle 尝试从当前类路径加载属性文件。
If the properties are stored in subdirectory , use "."如果属性存储在子目录中,请使用“.”。 instead of "/".代替 ”/”。
ResourceBundle.getBundle("fontawesome.fontawesome")
您的语言属性文件应以_en 结尾,因此在您的情况下fontawesome_en.properties
应使用ResourceBundle.getBundle("fontawesome.fontawesome")
加载它。
Here's my experience: If you put a "."这是我的经验:如果你把一个“。” in the name then the ResourceBundle.getBundle() will look for a class instead of properties file.在名称中,ResourceBundle.getBundle() 将寻找一个类而不是属性文件。 That class will look something like this:该类将如下所示:
import java.util.ListResourceBundle;导入 java.util.ListResourceBundle;
public class DataEncryptionStrings extends ListResourceBundle {
@Override
protected Object[][] getContents() {
return new Object[][] {
{"ErrorCreateKeystoreInstance", "an exception occurred creating the keystore instance"},
{"ErrorLoadInitialKeystore", "an exception occurred loading the initial keystore"},
{"ErrorLoadKeystore", "an exception occurred loading the keystore"},
It follows the naming convention:它遵循命名约定:
{classname}.class is the default class {classname}.class 是默认类
{classname}_en_US.class is english US class {classname}_en_US.class 是美国英语课
{classname}_fr_FR.class in the french class {classname}_fr_FR.class 法语课
if you don't have any "."如果你没有任何“.” in the name but have a "/" in it, then it'll look for property files instead.在名称中但有一个“/”,然后它将查找属性文件。
If you don't have either a "."如果您没有“。” of "/" in the name, I think either property files or ListResourceBundle can be made available.如果名称中包含“/”,我认为属性文件或 ListResourceBundle 都可以使用。 It'll find whatever you've provided.它会找到您提供的任何内容。 I'm not sure though if that behavior is same for all VMs.我不确定所有虚拟机的行为是否相同。
The problem I had that got me interested in searching for a solution was that I had a filename with "."'s in it.使我对寻找解决方案感兴趣的问题是我的文件名中包含“.”。 I'm pretty sure that my problem was that I implement properties file and not ListResourceBundle classes, and with a file name containing a "."我很确定我的问题是我实现了属性文件而不是 ListResourceBundle 类,并且文件名包含“。” in it, ResourceBundle.getBundle(name) looked for ListResourceBundle classes.在其中,ResourceBundle.getBundle(name) 寻找 ListResourceBundle 类。 When I replaced the "."当我替换“。” in the filename with a "_", I was then able to find the default ResourceBundle properties file.在带有“_”的文件名中,我能够找到默认的 ResourceBundle 属性文件。
User68883 seems to be using an absolute address "/fontawesome/fontawesome.properties" instead of a relative address "fontawesome/fontawesome.properties". User68883 似乎使用绝对地址“/fontawesome/fontawesome.properties”而不是相对地址“fontawesome/fontawesome.properties”。 Also, on the ResourceBundle.getBundle, never include the files extension in the name nor the Locale.此外,在 ResourceBundle.getBundle 上,永远不要在名称或区域设置中包含文件扩展名。 That's what ResouceBundle automagically does for you as it goes through a resolution process to get the best resource you have in the resource directory for the devices Locale.这就是 ResouceBundle 在经过解析过程以获取设备区域设置的资源目录中拥有的最佳资源时自动为您做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.