gpt4 book ai didi

java - ResourceBundle.getBundle(class,locale) 未获取默认 Bundle

转载 作者:行者123 更新时间:2023-12-01 12:21:53 33 4
gpt4 key购买 nike

我的机器(Windows 7 64 位)上的 Java7 和 Java8 上的 ResourceBundle.getBundle 都有一个非常奇怪的行为,我很困惑。

我尝试获取所选区域设置的 MyRessource Bundle(它扩展了 RessourceBundle)。这是简单的代码(缩短和简化):

默认的RessourceBundle类:

public class MyRessource extends ResourceBundle {

@Override
protected Object handleGetObject(final String key) {
if (key.equals("submit")) return "submit";
return null;
}
}

德语资源包类:

public class MyRessource_de extends MyRessource {

@Override
protected Object handleGetObject(final String key) {
if (key.equals("submit")) return "absenden";
return null;
}
}

然后我想检索德语包:

    final ResourceBundle bundle_DE = 
ResourceBundle.getBundle(MyRessource.class.getName(), Locale.GERMANY);
LOG.debug("Bundle {}", bundle_DE);
LOG.debug(bundle_DE.getString("submit"));

输出是:

Bundle com.example.MyRessource_de@6cf82c7d

absenden

现在与另一种语言相同,这应该返回默认包:

    final ResourceBundle bundle_EN =
ResourceBundle.getBundle(MyRessource.class.getName(), Locale.UK);
LOG.debug("Bundle {}", bundle_EN);
LOG.debug(bundle_EN.getString("submit"));

但是输出是:

Bundle com.example.MyRessource_de@6cf82c7d

absenden

返回了相同的缓存 bundle 。我尝试使用 ResourceBundle.clearCache() 清除缓存,但没有成功。

当我实现另一个语言包并要求它时,我得到了它,但我没有得到默认包类。

我不知道为什么 Java 会这样,并且很高兴得到任何答案。

在其他机器上,代码按预期工作。

顺便说一句:我的 Windows 系统语言是 de。

最佳答案

来自 ResourceBundle javadoc:

getBundle uses the base name, the specified locale, and the default locale (obtained from Locale.getDefault) to generate a sequence of candidate bundle names. If the specified locale's language, script, country, and variant are all empty strings, then the base name is the only candidate bundle name.

换句话说,由于它找不到 MyResource_UK 资源包,因此它会使用系统默认值生成候选包名称,然后再尝试仅使用基本名称(在您的情况下,成功找到德语包) .

关于java - ResourceBundle.getBundle(class,locale) 未获取默认 Bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26608248/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com