i18n: Fallback more than 1 locale?

Discussion in 'Java' started by =?ISO-8859-1?Q?Phil_H=FChn?=, Jun 3, 2004.

  1. 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
    =?ISO-8859-1?Q?Phil_H=FChn?=, Jun 3, 2004
    #1
    1. Advertising

  2. Phil Hühn wrote:
    > 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
    Thomas Weidenfeller, Jun 3, 2004
    #2
    1. Advertising

  3. Thomas Weidenfeller wrote:
    > Phil Hühn wrote:
    >> 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.


    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?=, Jun 3, 2004
    #3
  4. Phil Hühn wrote:
    > Thomas Weidenfeller wrote:
    >
    >> Phil Hühn wrote:
    >>
    >>> 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.

    >
    > 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...?
    =?ISO-8859-1?Q?Phil_H=FChn?=, Jun 6, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maurice Hulsman
    Replies:
    1
    Views:
    1,815
    Guus Bosman
    Jul 25, 2004
  2. Replies:
    4
    Views:
    979
  3. Gabriel Genellina
    Replies:
    0
    Views:
    675
    Gabriel Genellina
    Feb 18, 2009
  4. Steven D'Aprano
    Replies:
    0
    Views:
    75
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    69
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page