i18n: Fallback more than 1 locale?

  • Thread starter =?ISO-8859-1?Q?Phil_H=FChn?=
  • Start date
?

=?ISO-8859-1?Q?Phil_H=FChn?=

Hi, I'm starting a new project which needs to support i18n.

I also have the situation where a user needs the ability to change the
text on some buttons, graphics on others, etc. It occured to me that
this is a great use for a resource bundle (RB). I.e if a particular
resource is in the users RB, that gets used, otherwise we fall back to
the default.
This is ok if we treat the users RB as if it belongs with a special
'locale' for that user, and if a particular setting isn't found then
fall back to the setting in the default locale.

HOWEVER, what happens if our default locale is english, but we have
changed locale to german, and we have a user RB on top of that? I.e, to
get a setting:
get setting from users RB
if setting not found, get setting from german RB
if setting not found, return setting from default (english) RB.

Is it possible to cascade the resource bundles like this?
Will it work, and is this a sensible approach, or is there a better way?

It's occurred to me that I could manually (at runtime) create a new
locale("de", "DE", "MyUserLocale") and change the resource bundle to
match... similarly if they switch to french then change it to
locale("fr", "FR", "MyUserLocale"), etc, but it gets a bit messy when
there's a "fr-FR" and "fr-CA" and "fr-CH"!!

Thanks,
Phil
 
T

Thomas Weidenfeller

Phil said:
I also have the situation where a user needs the ability to change the
text on some buttons, graphics on others, etc. It occured to me that
this is a great use for a resource bundle (RB). I.e if a particular
resource is in the users RB, that gets used, otherwise we fall back to
the default.
This is ok if we treat the users RB as if it belongs with a special
'locale' for that user, and if a particular setting isn't found then
fall back to the setting in the default locale.

You have two issues:

1) Finding a resource bundle,

2) Chaining the lookup of values
HOWEVER, what happens if our default locale is english, but we have
changed locale to german, and we have a user RB on top of that?

1) The default behavior of resource bundles is that they are searched
for in a specific order, from the most specific
(language/country/variant) version to the least specific. And, they are
searched along the class path. The search consists basically of three
nested loops (the third is the one looking for classes or files). The
first found bundle is used.

To ensure a user's bundle is found at all with the default
implementation, you would have to place a user's directory on the class
path. Which is probably not a good idea.

So you better don't use the default search mechanism for the user's
resource bundle. Instead, you just load the user's bundle with a
PropertyResourceBundle.

And I would strongly suggest you do not mess with any artificially
introduced locale name. Locales are used in all sorts of unexpected
places in Java, and you better have a valid (existing) locale.
I.e, to
get a setting:
get setting from users RB
if setting not found, get setting from german RB
if setting not found, return setting from default (english) RB.

Is it possible to cascade the resource bundles like this?

2) Yes, did you read the API documentation?

Load the user's resource bundle (separately with a
PropertyResourceBundle), load the bundle corresponding to the current
locale (via getBundle(String)), load the default bundle (via
getBundle(String, Locale ...)), and chain them.


/Thomas
 
?

=?ISO-8859-1?Q?Phil_H=FChn?=

Thomas said:
2) Yes, did you read the API documentation?

Load the user's resource bundle (separately with a
PropertyResourceBundle), load the bundle corresponding to the current
locale (via getBundle(String)), load the default bundle (via
getBundle(String, Locale ...)), and chain them.

Yes, have read the API, but I'm unclear on a couple of things. As I
understood it, under a normal situation if you have a default 'locale'
and switch to 'locale_de_DE', then if a particular resource is not found
in the '...de_DE' file then the java *automatically* falls back to
returning the resource from the default locale (assuming we don't get a
missing resource exception). Similarly if the locale is set to
'...de_DE_WIN', then the search for a resource starts there, falls back
to the '...de_DE' locale if not found, then back to the default locale
bundle if not found there either.

Twice above you refer to 'chaining' the bundles - is this something you
suggest I do manually or is there some built-in mechanism for supporting
this? (I had assumed it was done automatically if we are using standard
locales.)

Thanks
 
?

=?ISO-8859-1?Q?Phil_H=FChn?=

Phil said:
Yes, have read the API, but I'm unclear on a couple of things. As I
understood it, under a normal situation if you have a default 'locale'
and switch to 'locale_de_DE', then if a particular resource is not found
in the '...de_DE' file then the java *automatically* falls back to
returning the resource from the default locale (assuming we don't get a
missing resource exception). Similarly if the locale is set to
'...de_DE_WIN', then the search for a resource starts there, falls back
to the '...de_DE' locale if not found, then back to the default locale
bundle if not found there either.

Twice above you refer to 'chaining' the bundles - is this something you
suggest I do manually or is there some built-in mechanism for supporting
this? (I had assumed it was done automatically if we are using standard
locales.)

Thanks

Doesn't anyone reading out there use i18n? Or is it simply that my
questions are inane...?
 

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,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top