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

发布于 2024-08-22 23:12:32 字数 158 浏览 3 评论 0原文

我正在运行 Eclipse 并尝试创建一个简单的测试程序,尝试使用几个不同的文件来尝试我们的 ResourceBundle。该文件正确命名为 ResourceFile_us_US.properties。但我在 getBundle() 调用上遇到异常,因为它显然找不到该文件。它应该位于哪里以便可以找到?

I'm running Eclipse and trying to create a simple test program trying our ResourceBundle with a couple of different files. The file is properly named as ResourceFile_us_US.properties. But I'm getting an exception on the getBundle() call because it apparently can't find the file. Where should it be located so it can be found?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

断念 2024-08-29 23:12:32

您知道 java 正在寻找特定语言环境中的属性文件。您可能会感到困惑,为什么 java 一直抱怨它找不到那里的属性文件。调试此类错误时需要记住以下几点:

  1. 这些资源属性文件由类加载器加载,类似于 java 类。因此,您需要将它们包含在运行时类路径中。

  2. 这些资源具有完全限定资源名称,类似于完全限定类名称,摘录您无法将资源导入到 java 源文件中。为什么?因为它的名称采用字符串的形式。

  3. ResourceBundle.getBundle("config") 告诉类加载器使用默认包(即无包)加载名为 "config" 的资源。它并不意味着当前包中具有引用类的资源。

  4. ResourceBundle.getBundle("com. Cheng.scrap.config") 告诉类加载器加载一个名为 "config" 的资源,其包为 "com. heng.scrap." 其完全限定资源名称是 "com. Cheng.scrap.config"

更多:找不到基础包名称 com...config,区域设置 zh_CN

干杯。

You know java is looking for a properties file in a specific locale. You may be baffled why java keeps complaining it can't find a properties file that is right there. 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. 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. 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). 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." Its fully-qualified-resource-name is "com.cheng.scrap.config"

More : Can't find bundle for base name com...config, locale zh_CN

Cheers.

故人如初 2024-08-29 23:12:32

如果您创建一个包 resources 并放入文件 hello_en_US.properties
里面有内容:

你好=你好世界!!

您可以使用以下代码打印 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"));

  }
}

If you create a package resources and put the file hello_en_US.properties
inside it, which has the content:

hello = Hello World!!

you can print the content of hello using the following code:

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"));

  }
}
衣神在巴黎 2024-08-29 23:12:32

两者之一:

  1. /src/resources
    确保在资源包文件的名称中包含区域设置。例如,对于津巴布韦,它将是 ResourceBundle_en_ZW.properties 并且您可以将其加载为 ResourceBundle resourceBundle = ResourceBundle.getBunndle("ResourceBundle", Locale.getDefault());

  2. 在你的类路径中。确保设置了类路径环境变量

One of two:

  1. /src/resources
    Make sure you include locale in name of resource bundle file. e.g for Zimbabwe, it will be ResourceBundle_en_ZW.properties and you would load it as ResourceBundle resourceBundle = ResourceBundle.getBunndle("ResourceBundle", Locale.getDefault());

  2. In your classpath. Make sure the classpath environment variable is set

追我者格杀勿论 2024-08-29 23:12:32

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

I believe it just looks in the classpath.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文