ResourceBundle.getBundle() Question/Possible Bug?

I am trying to understand why ResourceBundle.getBundle() is working the way it is under a particular scenario. It looks like a bug to me, but I wanted to see if anyone else can shed some light on this.

My sample program sets up the default locale asen_US and then constructs a locale ofen_UK. I have defined the following resource bundle property files:

MessagesBundle_en_US.properties:

greetings = Hello.

farewell = Goodbye.

inquiry = How are you?

MessagesBundle.properties:

greetings = Hiya.

farewell = Seeya.

inquiry = How you doing?

When I run my sample program (and it attempts to get a resource bundle for en_UK - which does not exist), instead it ends up retrieving the 'root' bundle. I would have expected it to find the en_US bundle (since it is the default locale). In looking through the ResourceBundle.java source code I noticed that getBundleImpl() contains a break statement if the bundle names 'intersect' and this is why the root bundle is used.

Here is my sample program:

RBSample .java:

import java.util.*;

publicclass RBSample{

staticpublicvoid main(String[] args){

String language;

String country;

Locale.setDefault(new Locale("en","US"));

Locale currentLocale;

ResourceBundle messages;

currentLocale = Locale.UK;

System.out.println("currentLocale = " + currentLocale.getDisplayName());

messages =

ResourceBundle.getBundle("MessagesBundle",currentLocale);

Locale messagesLocale = messages.getLocale();

System.out.println("messagesLocale = " + messagesLocale.getDisplayName());

System.out.println(messages.getString("greetings"));

System.out.println(messages.getString("inquiry"));

System.out.println(messages.getString("farewell"));

}

}

Sample Output:

currentLocale = English (United Kingdom)

messagesLocale =

Hiya.

How you doing?

Seeya.

Am I missing something here? Why is it behaving this way?

[2613 byte] By [jschuman72] at [2007-9-30 20:23:39]
# 1
Sure it returns null? The Javadoc for the getDisplayName() says that if the language, country, and variant fields are all empty, this function returns the empty string. That is what it seems like you are printing out.-s
shelzib at 2007-7-7 1:08:17 > top of Java-index,Java Essentials,Java Programming...
# 2

Right, but why is the messages locale set up like that. Why is it not retrieving the messages bundle for the default locale (i.e., en_US)?

For example, if you change line 15 of my example program to:

currentLocale = Locale.KOREA;

then the output looks like this:

currentLocale = Korean (South Korea)

messagesLocale = English (United States)

Hello.

How are you?

Goodbye.

In this case the getBundle() method selected the default locale and default locale resource bundle (MessagesBundle_en_US.properties).

So why the different behavior? It appears that if I send in a locale that has the same language as the default locale, then the root bundle is selected, while if I send in a locale that has a different language then the default locale bundle is selected.

Does that help?

jschuman72 at 2007-7-7 1:08:17 > top of Java-index,Java Essentials,Java Programming...
# 3

I agree, this looks like a bug. This code

if (names.contains(bundleName)) {

//the fallback branch intersects the main branch so we can stop now.

break;

}

in getBundleImpl() causes the en_US bundle not to be loaded. It stops too early.

Probably it should continue rather than break.

I wrote a little class to find out which bundles the ResourceBundle class tries to load:

class CL extends ClassLoader {

public CL() {

Locale.setDefault(Locale.US);

test(Locale.UK);

test(Locale.KOREA);

}

void test(Locale l){

System.out.println(" "+l+", default: "+l.getDefault());

try {

ResourceBundle.getBundle("Messages", l, this);

} catch(Exception ex) {

}

}

public URL getResource(String s){

System.out.println("getResource("+s+")");

return(getClass().getClassLoader().getResource(s) );

}

}

output:

en_GB, default: en_US

getResource(Messages.properties)

getResource(Messages_en.properties)

getResource(Messages_en_GB.properties)

ko_KR, default: en_US

getResource(Messages_ko.properties)

getResource(Messages_ko_KR.properties)

getResource(Messages_en_US.properties)

BickelT at 2007-7-7 1:08:17 > top of Java-index,Java Essentials,Java Programming...
# 4
Yes, I would agree (a bug). I think it should be continue instead of break as well.I guess I'm just a little surprised to find this, since ResourceBundle has been around for a while. Should I go ahead and report this as a bug?
jschuman72 at 2007-7-7 1:08:17 > top of Java-index,Java Essentials,Java Programming...
# 5
> Should I go ahead and report this as a bug?> Sure, I don't think this bug has yet been reported.
BickelT at 2007-7-7 1:08:18 > top of Java-index,Java Essentials,Java Programming...