简体   繁体   English

ResourceBundle.getBundle(“ResourceFile”,新的Locale(“us”,“US”))在哪里查找文件?

[英]Where does ResourceBundle.getBundle(“ResourceFile”, new Locale(“us”, “US”)) look for the file?

I'm running Eclipse and trying to create a simple test program trying our ResourceBundle with a couple of different files. 我正在运行Eclipse并尝试创建一个简单的测试程序,尝试我们的ResourceBundle和几个不同的文件。 The file is properly named as ResourceFile_us_US.properties. 该文件正确命名为ResourceFile_us_US.properties。 But I'm getting an exception on the getBundle() call because it apparently can't find the file. 但我在getBundle()调用上遇到异常,因为它显然无法找到该文件。 Where should it be located so it can be found? 它应该放在哪里,以便找到它?

You know java is looking for a properties file in a specific locale. 您知道java正在寻找特定区域设置中的属性文件。 You may be baffled why java keeps complaining it can't find a properties file that is right there. 你可能会感到困惑,为什么java一直在抱怨它无法找到正确的属性文件。 A few things to keep in mind when debugging this type of errors: 调试此类错误时要记住以下几点:

  1. These resource properties files are loaded by classloader, similar to java classes. 这些资源属性文件由类加载器加载,类似于java类。 So you need to include them in your runtime classpath. 因此,您需要将它们包含在运行时类路径中。

  2. These resources have fully-qualified-resource-name, similar to a fully-qualified-class-name, excerpt you can't import a resource into your java source file. 这些资源具有完全限定资源名称,类似于完全限定类名称,摘录您无法将资源导入Java源文件。 Why? 为什么? because its name takes the form of a string. 因为它的名称采用字符串的形式。

  3. ResourceBundle.getBundle("config") tells the classloader to load a resource named "config" with default package (that is, no package). ResourceBundle.getBundle("config")告诉类加载器使用默认包(即没有包)加载名为"config"的资源。 It does NOT mean a resource in the current package that has the referencing class. 它并不意味着当前包中具有引用类的资源。

  4. ResourceBundle.getBundle("com.cheng.scrap.config") tells the classloader to load a resource named "config" with package "com.cheng.scrap." ResourceBundle.getBundle("com.cheng.scrap.config")告诉类加载器使用包"com.cheng.scrap."加载名为"config"的资源"com.cheng.scrap." Its fully-qualified-resource-name is "com.cheng.scrap.config" 它的完全限定资源名称是"com.cheng.scrap.config"

More : Can't find bundle for base name com...config, locale zh_CN 更多: 无法找到基本名称com ... config,locale zh_CN的包

Cheers. 干杯。

If you create a package resources and put the file hello_en_US.properties inside it, which has the content: 如果您创建一个包resources并将文件hello_en_US.properties放在其中,其中包含以下内容:

hello = Hello World!! 你好= Hello World !!

you can print the content of hello using the following code: 您可以使用以下代码打印hello的内容:

package localization;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleDemo {

public static void main(String[] args) {

    Locale en_US = new Locale("en", "US");
    ResourceBundle bundle = ResourceBundle.getBundle("resources.hello", en_US);

    // print the value of the key "hello"
    System.out.println("" + bundle.getString("hello"));

  }
}

我相信它只是在类路径中查找。

One of two: 两个中的一个:

  1. /src/resources Make sure you include locale in name of resource bundle file. / src / resources确保在资源包文件的名称中包含区域设置。 eg for Zimbabwe, it will be ResourceBundle_en_ZW.properties and you would load it as ResourceBundle resourceBundle = ResourceBundle.getBunndle("ResourceBundle", Locale.getDefault()); 例如,对于津巴布韦,它将是ResourceBundle_en_ZW.properties,您可以将其加载为ResourceBundle resourceBundle = ResourceBundle.getBunndle(“ResourceBundle”,Locale.getDefault());

  2. In your classpath. 在你的类路径中。 Make sure the calsspath environment variable is set 确保已设置calsspath环境变量

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

相关问题 无法使用 ResourceBundle.getBundle() 加载 ResourceBundle 找不到基本名称的包,区域设置 en_US - Unable to load ResourceBundle with ResourceBundle.getBundle() Can't find bundle for base name , locale en_US Java ResourceBundle.getBundle如何加载属性文件? - How does Java ResourceBundle.getBundle load properties file? 为什么ResourceBundle.getBundle(String,Locale)忽略了Locale? - Why is ResourceBundle.getBundle(String, Locale) ignoring the Locale? ResourceBundle.getBundle和Websphere - ResourceBundle.getBundle and Websphere ResourceBundle.getBundle(class,locale)没有获得默认捆绑 - ResourceBundle.getBundle(class,locale) don't get default Bundle java ResourceBundle.getBundle()不明确 - java ResourceBundle.getBundle() undeterministic ResourceBundle.getBundle() 访问属性文件的默认路径是什么 - What is the default path of ResourceBundle.getBundle() to access the properties file 如何忽略ResourceBundle.getBundle中的defaultLocale? - How to ignore defaultLocale in ResourceBundle.getBundle? Java ResourceBundle.getBundle缺少资源异常 - Java ResourceBundle.getBundle missing resource exception 为什么ResourceBundle.getBundle(String str)无法从Websphere中的默认属性文件中找到值 - Why ResourceBundle.getBundle(String str) cannot find values from default properties file in Websphere
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM