Problem with PropertyResourceBundle

K

kiran

Hi,
I have a File object with charset set to UTF-8. I am creating a
PropertyResourceBundle using a FileInputStream. The code snippet is

File myFile = new File(myPath);
FileInputStream myStream;
myStream = new FileInputStream(myFile);
PropertyResourceBundle myB = new PropertyResourceBundle(myStream);

The PropertyResourceBundle gets created but the locale on this bundle
is somehow always set to null. Could anyone please explain why it's
being set to null and if there is a way to have the locale set to
Locale.getDefault()

Thanks for the help.
-Kiran
 
J

John C. Bollinger

kiran said:
I have a File object with charset set to UTF-8.

The File class doesn't have anything to do with charsets. Do you mean
that the file it refers to is encoded in UTF-8?
I am creating a
PropertyResourceBundle using a FileInputStream.

Based on the aforementioned file? That would be wrong. Properties
files are specified to be encoded in ISO-8859-1, with Unicode escapes
(ala Java source code) where necessary. See the class-level API docs of
the Properties class; there's also a reference to these in the
PropertyResourceBundle docs.
The code snippet is

File myFile = new File(myPath);
FileInputStream myStream;
myStream = new FileInputStream(myFile);
PropertyResourceBundle myB = new PropertyResourceBundle(myStream);

That looks fine.
The PropertyResourceBundle gets created but the locale on this bundle
is somehow always set to null. Could anyone please explain why it's
being set to null and if there is a way to have the locale set to
Locale.getDefault()

I would have to read the source to tell you specifically why the Locale
is set to null, but it seems to be the right choice. The bundle doesn't
know from the properties file what locale it is actually for, so it
would be incorrect for the locale to be set (to anything) in the
constructor. I would guess that ResourceBundle and
PropertyResourceBundle have package-private constructors that accept a
Locale instance and use it to initialize the bundle's locale; these
would be used by the static ResourceBundle.getBundle methods. And that
makes a reasonable amount of sense, as when you construct a
ResourceBundle (of any kind) yourself, you know beforehand (or should)
what locale it applies to. I do think, however, that it's a little
paranoid to not expose such constructors.

So what can you do? Nothing, if you're stuck with the
PropertyResourceBundle class itself. Otherwise you could subclass it
and override getLocale() in a suitable way.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,218
Messages
2,571,124
Members
47,727
Latest member
smavolo

Latest Threads

Top