cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Engelhart <>
Subject Re: XML Resource File?
Date Fri, 04 Feb 2000 21:14:06 GMT
Timm, Sean wrote:

> I'm going to make an assumption about localization and HTTP requests that I
> don't necessarily know is true.  I assume that Cocoon can detect the locale
> of the requesting browser.

Cocoon can't but the underlying servlet engine can.  I highly recommend
using a JSDK 2.2 compliant servlet engine as it will have built in support
for accessing the Locale from the HttpServletRequest object.  Just do this:

Locale loc = request.getLocale();

> Based on this, I see two ways resource handling could occur within Cocoon:
> (1) When a request came into Cocoon for a certain processor or producer,
> Cocoon could set the resource information for the specified
> producer/processor (this could be specified in the file)
> for the locale of the requesting browser as the working resource
> information.  Then you could call to SomeCocoonClass.getResourceString, and
> get the appropriate string.

You're making it too complicated.  You can access ResourceBundles from
anywhere. You don't need to tell your application where they are (other than
have them in the correct package)

What i've done is write a class called ResourceLoader that automatically
loads all the needed ResourceBundles for my application on startup from
Properties files (you could do this in a startup servlet or do what I did
and patch Cocoon to allow objects to be loaded into Cocoon for later access)
this isn't strictly necessary since the ResourceBundle will look for and
load in the proper resource the first time it's called - I just wanted to
save a few nano-seconds.

anyway, once these are loaded, you just do this anywhere in your code.
Locale loc = request.getLocale();

or if there are lot you can grab a reference and just use that:
ResourceBundle bundle =
ResourceBundle.getBundle("com.yourpackage.Processor1", loc);


Actually there I'm waiting for Ricardo to get back because for some reason
trying to access a ResourceBundle from within an XSP throws an exception.  I
think it has to do with the custom classloader but I'm not sure. For now I
just wrote a simple static method getResourceString() which effectively does
the same thing.

> (2) You could define a XSP resource handling taglib and just kick out the
> xsp tags wherever you need to load a string from a resource file.
> It seems like Cocoon preloads all of the processors available to it (or
> something like that...I know I get an error if I don't have the class
> installed for a processor mentioned in the file).  At this
> time, Cocoon could load up the appropriate resource file(s) based on the
> available locale(s).

Again, don't make it too complicated. You don't have to worry about
"available" locales.  ResourceBundles gracefully fall back to the default
locale.   Look at the JDK documenation - it has a ton of samples to describe
just this process.

> Basically, I just like an XML format better than the following:
> s1=blah
> s2=etc

Me too but XML wasn't really in general use when they wrote the JDK 1.1.x
class libraries.  Maybe in a future release they'll have this as the
properties format but even if they do, you only have to change your
properties files, not your application.


View raw message