You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by duke <du...@users.sourceforge.net> on 2005/05/11 23:47:44 UTC

[Resources] RFE: Support for multiple Locales in Messages.getMessage

Currently the Messages.getMessage methods only support a single Locale.
 So if you had an application that provided English (default) and French
resources and a user preferred to read German, French, and English (in
that order), a call to Messages.getMessage(<German>, <key>) would return
the message from the English resource when the message from the French
resource would be preferable.

This RFE calls for the Messages.getMessage methods to accept an array of
Locales where they currently accept a single Locale.  The implementation
would then search for the specified key in each of the resources for the
specified Locales in element order.

Using the example above, a call to Messages.getMessage(<German, French,
English>, <key>) would then return the message from the French resource.

Any other thoughts on solving this problem?

duke

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Resources] RFE: Support for multiple Locales in Messages.getMessage

Posted by Mattias J <ma...@expertsystem.se>.
At 2005-05-13 23:03, you wrote:
>Mattias J wrote:
> > At 2005-05-11 23:47, you wrote:
> >
> >> Currently the Messages.getMessage methods only support a single Locale.
> >>  So if you had an application that provided English (default) and French
> >> resources and a user preferred to read German, French, and English (in
> >> that order), a call to Messages.getMessage(<German>, <key>) would return
> >> the message from the English resource when the message from the French
> >> resource would be preferable.
> >>
> >> This RFE calls for the Messages.getMessage methods to accept an array of
> >> Locales where they currently accept a single Locale.  The implementation
> >> would then search for the specified key in each of the resources for the
> >> specified Locales in element order.
> >
> >
> > I don't even think this is possible, since Commons Resources (AFAIK)
> > uses java.utils.ResourceBundle for one of its implementations, and
> > ResourceBundle returns the resouce for the default locale, if the
> > requested locale is not found. There would therefore be no way for
> > Commons Resources to know whether the resource was found using the
> > primary locale or the default locale, and would therefore not be able to
> > tell when the secondary locale should be used.
>
>I believe that you can tell if you have a ResourceBundle for the
>requested Locale or the default Locale by calling
>ResourceBundle.getLocale.  You can then compare the returned Locale with
>the requested Locale and if they don't match you can try to get another
>ResourceBundle with the next Locale in the list.

That is right. I confused it with the fact that if you get a ResourceBundle 
for the requested Locale, but that bundle does not contain the requested 
key, the ResourceBundle class will look for the key in the parent bundle 
recursively, up until the default Locale.

So we might be talking about two different scenarios here:
- The primary Locale does not exist, and a secondary Locale should be used
- The requested key does not exist for the primary Locale; the Locale used 
instead cannot be determined

The case I was talking about may be a non-issue, since most localizations 
should include all the keys.

   /Mattias Jiderhamn



   Mattias Jiderhamn
   Expert Systems

   Mail: 
<ma...@expertsystem.se>mattias.jiderhamn@expertsystem.se
   Web: <http://www.expertsystem.se/>www.expertsystem.se
   Skype: <callto://mattiasj78>mattiasj78

Re: [Resources] RFE: Support for multiple Locales in Messages.getMessage

Posted by duke <du...@users.sourceforge.net>.
Mattias J wrote:
> At 2005-05-11 23:47, you wrote:
> 
>> Currently the Messages.getMessage methods only support a single Locale.
>>  So if you had an application that provided English (default) and French
>> resources and a user preferred to read German, French, and English (in
>> that order), a call to Messages.getMessage(<German>, <key>) would return
>> the message from the English resource when the message from the French
>> resource would be preferable.
>>
>> This RFE calls for the Messages.getMessage methods to accept an array of
>> Locales where they currently accept a single Locale.  The implementation
>> would then search for the specified key in each of the resources for the
>> specified Locales in element order.
> 
> 
> I don't even think this is possible, since Commons Resources (AFAIK)
> uses java.utils.ResourceBundle for one of its implementations, and
> ResourceBundle returns the resouce for the default locale, if the
> requested locale is not found. There would therefore be no way for
> Commons Resources to know whether the resource was found using the
> primary locale or the default locale, and would therefore not be able to
> tell when the secondary locale should be used.

I believe that you can tell if you have a ResourceBundle for the
requested Locale or the default Locale by calling
ResourceBundle.getLocale.  You can then compare the returned Locale with
the requested Locale and if they don't match you can try to get another
ResourceBundle with the next Locale in the list.

duke

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Resources] RFE: Support for multiple Locales in Messages.getMessage

Posted by Mattias J <mj...@expertsystem.se>.
At 2005-05-11 23:47, you wrote:
>Currently the Messages.getMessage methods only support a single Locale.
>  So if you had an application that provided English (default) and French
>resources and a user preferred to read German, French, and English (in
>that order), a call to Messages.getMessage(<German>, <key>) would return
>the message from the English resource when the message from the French
>resource would be preferable.
>
>This RFE calls for the Messages.getMessage methods to accept an array of
>Locales where they currently accept a single Locale.  The implementation
>would then search for the specified key in each of the resources for the
>specified Locales in element order.

I don't even think this is possible, since Commons Resources (AFAIK) uses 
java.utils.ResourceBundle for one of its implementations, and 
ResourceBundle returns the resouce for the default locale, if the requested 
locale is not found. There would therefore be no way for Commons Resources 
to know whether the resource was found using the primary locale or the 
default locale, and would therefore not be able to tell when the secondary 
locale should be used.

   / Mattias J 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Resources] RFE: Support for multiple Locales in Messages.getMessage

Posted by duke <du...@users.sourceforge.net>.
I reviewed the API docs and the source code and I haven't found a
reliable (and efficient) way to determine if the String returned by
Messages.getMessage is from the Locale that I requested.  It doesn't
appear that the returned String will ever be null, but instead will be
the message from the default Locale or an error message if that key is
not found in the requested or default Locales.

I agree that Messages.getMessage is already heavily overloaded, but IMHO
adding methods that accept an array of Locales or modifying the existing
methods to accept an array of Locales would be acceptable in the context
of a framework that is designed to aid internationalization.

duke


James Mitchell wrote:
> Why not implement the preferred order in your own code?
> 
> ...
> ...
> myMsg = Messages.getMessage(<German>, <key>);
> if (myMsg == null) myMsg = Messages.getMessage(<French>, <key>);
> if (myMsg == null) myMsg = Messages.getMessage(<English>, <key>);
> ...
> ...
> 
> 
> This way Commons-Resources doesn't have to be convoluted with a bunch of
> switches for something very few users are likely to need.
> 
> 
> 
> -- 
> James Mitchell
> Software Engineer / Open Source Evangelist
> Consulting / Mentoring / Freelance
> EdgeTech, Inc.
> http://www.edgetechservices.net/
> 678.910.8017
> AIM:   jmitchtx
> Yahoo: jmitchtx
> MSN:   jmitchell@apache.org
> 
> 
> 
> 
> ----- Original Message ----- From: "duke" <du...@users.sourceforge.net>
> To: <co...@jakarta.apache.org>
> Sent: Wednesday, May 11, 2005 5:47 PM
> Subject: [Resources] RFE: Support for multiple Locales in
> Messages.getMessage
> 
> 
>> Currently the Messages.getMessage methods only support a single Locale.
>> So if you had an application that provided English (default) and French
>> resources and a user preferred to read German, French, and English (in
>> that order), a call to Messages.getMessage(<German>, <key>) would return
>> the message from the English resource when the message from the French
>> resource would be preferable.
>>
>> This RFE calls for the Messages.getMessage methods to accept an array of
>> Locales where they currently accept a single Locale.  The implementation
>> would then search for the specified key in each of the resources for the
>> specified Locales in element order.
>>
>> Using the example above, a call to Messages.getMessage(<German, French,
>> English>, <key>) would then return the message from the French resource.
>>
>> Any other thoughts on solving this problem?
>>
>> duke
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>>
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Re: [Resources] RFE: Support for multiple Locales in Messages.getMessage

Posted by James Mitchell <jm...@apache.org>.
Why not implement the preferred order in your own code?

...
...
myMsg = Messages.getMessage(<German>, <key>);
if (myMsg == null) myMsg = Messages.getMessage(<French>, <key>);
if (myMsg == null) myMsg = Messages.getMessage(<English>, <key>);
...
...


This way Commons-Resources doesn't have to be convoluted with a bunch of 
switches for something very few users are likely to need.



--
James Mitchell
Software Engineer / Open Source Evangelist
Consulting / Mentoring / Freelance
EdgeTech, Inc.
http://www.edgetechservices.net/
678.910.8017
AIM:   jmitchtx
Yahoo: jmitchtx
MSN:   jmitchell@apache.org




----- Original Message ----- 
From: "duke" <du...@users.sourceforge.net>
To: <co...@jakarta.apache.org>
Sent: Wednesday, May 11, 2005 5:47 PM
Subject: [Resources] RFE: Support for multiple Locales in 
Messages.getMessage


> Currently the Messages.getMessage methods only support a single Locale.
> So if you had an application that provided English (default) and French
> resources and a user preferred to read German, French, and English (in
> that order), a call to Messages.getMessage(<German>, <key>) would return
> the message from the English resource when the message from the French
> resource would be preferable.
>
> This RFE calls for the Messages.getMessage methods to accept an array of
> Locales where they currently accept a single Locale.  The implementation
> would then search for the specified key in each of the resources for the
> specified Locales in element order.
>
> Using the example above, a call to Messages.getMessage(<German, French,
> English>, <key>) would then return the message from the French resource.
>
> Any other thoughts on solving this problem?
>
> duke
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org