下面列出了怎么用java.util.ResourceBundle.Control的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
public String getLocalizedProperty(String property, Locale locale)
{
Control control = Control.getControl(Control.FORMAT_PROPERTIES);
String value = null;
// we're not looking at the fallback locale to be consistent with JRResourcesUtil.loadResourceBundle
List<Locale> locales = control.getCandidateLocales(property, locale);
for (Locale candidate : locales)
{
String candidateString = candidate.toString();
String candidateProperty = candidateString.isEmpty() ? property : (property + "_" + candidateString);
String candidateValue = getProperty(candidateProperty);
if (candidateValue != null)// test for empty?
{
value = candidateValue;
break;
}
}
return value;
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
private String getTranslatedText(String resourceName, String key, Locale locale) {
try {
// Modify the search order so that the following applies:
// 1.) baseName + "_" + language + "_" + country
// 2.) baseName + "_" + language
// 3.) baseName
// 4.) null -> leads to a default text
// Not using the default fallback strategy helps that not the default locale
// search order is applied between 2.) and 3.).
ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceName, locale, this.resourceClassLoader,
Control.getNoFallbackControl(Control.FORMAT_PROPERTIES));
if (resourceBundle != null) {
return resourceBundle.getString(key);
}
} catch (Exception ex) {
// nothing to do
}
return null;
}
/**
* Determines the semantic entity type of an item, i.e. a sub-type of Location, Equipment or Point.
*
* @param item the item to get the semantic type for
* @return a sub-type of Location, Equipment or Point
*/
public static @Nullable Class<? extends Tag> getSemanticType(Item item) {
Set<String> tags = item.getTags();
for (String tag : tags) {
Class<? extends Tag> type = getById(tag);
if (type != null && !Property.class.isAssignableFrom(type)) {
return type;
}
}
// we haven't found any type as a tag, but if there is a Property tag, we can conclude that it is a Point
if (getProperty(item) != null) {
StateDescription stateDescription = item.getStateDescription();
if (stateDescription != null && stateDescription.isReadOnly()) {
return Measurement.class;
} else {
return org.openhab.core.semantics.model.point.Control.class;
}
} else {
return null;
}
}
private ValueMap<? extends LanguageTermDefinition> readLangs(Locale locale) {
ArrayList<LangTermValue> langs = new ArrayList<LangTermValue>();
// to read properties file in UTF-8 use PropertyResourceBundle(Reader)
Control control = ResourceBundle.Control.getNoFallbackControl(ResourceBundle.Control.FORMAT_PROPERTIES);
ResourceBundle rb = ResourceBundle.getBundle(BundleName.LANGUAGES_ISO639_2.toString(), locale, control);
for (String key : rb.keySet()) {
LangTermValue lt = new LangTermValue();
lt.setAuthority("iso639-2b");
lt.setType(CodeOrText.CODE);
lt.setValue(key);
lt.setTitle(rb.getString(key));
langs.add(lt);
}
Collections.sort(langs, new LangComparator(locale));
return new ValueMap<LangTermValue>(BundleName.LANGUAGES_ISO639_2.getValueMapId(), langs);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
/**
* @bug 4108126
*/
public void TestGetLocale() {
// try to find TestResource_fr_CH. Should get fr_CH as its locale
ResourceBundle test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "CH", ""));
Locale locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("CH")))
errln("Actual locale for TestResource_fr_CH should have been fr_CH, got " + locale);
// try to find TestResource_fr_BE, which doesn't exist. Should get fr as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("fr", "BE", ""));
locale = test.getLocale();
if (!(locale.getLanguage().equals("fr")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_fr_BE should have been fr, got " + locale);
// try to find TestResource_iw_IL, which doesn't exist. Should get root locale
// as its locale
test = ResourceBundle.getBundle("TestResource",
new Locale("iw", "IL", ""),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
locale = test.getLocale();
if (!(locale.getLanguage().equals("")) || !(locale.getCountry().equals("")))
errln("Actual locale for TestResource_iw_IL should have been the root locale, got "
+ locale);
}
private String getTranslatedText(String resourceName, String key, Locale locale) {
try {
// Modify the search order so that the following applies:
// 1.) baseName + "_" + language + "_" + country
// 2.) baseName + "_" + language
// 3.) baseName
// 4.) null -> leads to a default text
// Not using the default fallback strategy helps that not the default locale
// search order is applied between 2.) and 3.).
ResourceBundle resourceBundle = ResourceBundle.getBundle(resourceName, locale, this.resourceClassLoader,
Control.getNoFallbackControl(Control.FORMAT_PROPERTIES));
if (resourceBundle != null) {
return resourceBundle.getString(key);
}
} catch (Exception ex) {
// nothing to do
}
return null;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l);
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Returns a list of candidate locales for service look up.
* @param locale the input locale
* @return the list of candidate locales for the given locale
*/
static List<Locale> getLookupLocales(Locale locale) {
// Note: We currently use the default implementation of
// ResourceBundle.Control.getCandidateLocales. The result
// returned by getCandidateLocales are already normalized
// (no extensions) for service look up.
List<Locale> lookupLocales = Control.getNoFallbackControl(Control.FORMAT_DEFAULT)
.getCandidateLocales("", locale);
return lookupLocales;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l,
ClassLoader.getSystemClassLoader());
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
public void TestListResourceBundle() {
// load up the resource and check to make sure we got the right class
// (we don't define be_BY or be, so we fall back on the root default)
ResourceBundle bundle = ResourceBundle.getBundle("TestResource",
new Locale("be", "BY"),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
if (!bundle.getClass().getName().equals("TestResource"))
errln("Expected TestResource, got " + bundle.getClass().getName());
doListResourceBundleTest(bundle);
}
/**
* Returns a list of candidate locales for service look up.
* @param locale the input locale
* @return the list of candidate locales for the given locale
*/
static List<Locale> getLookupLocales(Locale locale) {
// Note: We currently use the default implementation of
// ResourceBundle.Control.getCandidateLocales. The result
// returned by getCandidateLocales are already normalized
// (no extensions) for service look up.
List<Locale> lookupLocales = Control.getNoFallbackControl(Control.FORMAT_DEFAULT)
.getCandidateLocales("", locale);
return lookupLocales;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l,
ClassLoader.getSystemClassLoader());
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
public void TestListResourceBundle() {
// load up the resource and check to make sure we got the right class
// (we don't define be_BY or be, so we fall back on the root default)
ResourceBundle bundle = ResourceBundle.getBundle("TestResource",
new Locale("be", "BY"),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
if (!bundle.getClass().getName().equals("TestResource"))
errln("Expected TestResource, got " + bundle.getClass().getName());
doListResourceBundleTest(bundle);
}
/**
* Returns a list of candidate locales for service look up.
* @param locale the input locale
* @return the list of candidate locales for the given locale
*/
static List<Locale> getLookupLocales(Locale locale) {
// Note: We currently use the default implementation of
// ResourceBundle.Control.getCandidateLocales. The result
// returned by getCandidateLocales are already normalized
// (no extensions) for service look up.
List<Locale> lookupLocales = Control.getNoFallbackControl(Control.FORMAT_DEFAULT)
.getCandidateLocales("", locale);
return lookupLocales;
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l);
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
public void TestListResourceBundle() {
// load up the resource and check to make sure we got the right class
// (we don't define be_BY or be, so we fall back on the root default)
ResourceBundle bundle = ResourceBundle.getBundle("TestResource",
new Locale("be", "BY"),
Control.getNoFallbackControl(Control.FORMAT_DEFAULT));
if (!bundle.getClass().getName().equals("TestResource"))
errln("Expected TestResource, got " + bundle.getClass().getName());
doListResourceBundleTest(bundle);
}
/**
* Returns a Map of the known resources for the given locale.
*/
private Map<String, Object> getResourceCache(Locale l) {
Map<String, Object> values = resourceCache.get(l);
if (values == null) {
values = new TextAndMnemonicHashMap();
for (int i=resourceBundles.size()-1; i >= 0; i--) {
String bundleName = resourceBundles.get(i);
try {
Control c = CoreResourceBundleControl.getRBControlInstance(bundleName);
ResourceBundle b;
if (c != null) {
b = ResourceBundle.getBundle(bundleName, l, c);
} else {
b = ResourceBundle.getBundle(bundleName, l,
ClassLoader.getSystemClassLoader());
}
Enumeration keys = b.getKeys();
while (keys.hasMoreElements()) {
String key = (String)keys.nextElement();
if (values.get(key) == null) {
Object value = b.getObject(key);
values.put(key, value);
}
}
} catch( MissingResourceException mre ) {
// Keep looking
}
}
resourceCache.put(l, values);
}
return values;
}
/**
* Returns a list of candidate locales for service look up.
* @param locale the input locale
* @return the list of candidate locales for the given locale
*/
static List<Locale> getLookupLocales(Locale locale) {
// Note: We currently use the default implementation of
// ResourceBundle.Control.getCandidateLocales. The result
// returned by getCandidateLocales are already normalized
// (no extensions) for service look up.
List<Locale> lookupLocales = Control.getNoFallbackControl(Control.FORMAT_DEFAULT)
.getCandidateLocales("", locale);
return lookupLocales;
}