Laurie Harper wrote:
Craig McClanahan wrote:
On 11/26/05, Laurie Harper <[EMAIL PROTECTED]> wrote:
Craig McClanahan wrote:
Using the following tag:
<f:loadBundle var="messages" basename="
com.mycompany.mypackage.MyBundle
"/>
exposes the contents of the named resource bundle as a Map in request
scope
under the specified key. Now you can bind to it ... for example, to
create
the localized label for the username field on a login screen, you might
say:
<h:outputText value="#{messages['usename.prompt']}"/>
where "username.prompt" is the message key.
Yeah, I get that bit; that's what led me down the route of trying to
build the menu items in the JSP in the first place :-) The question is
how, if I should be building them in the backing bean, do I get the
localized label values there?
I guess I can lookup the message bundle in the request (using the
equivealent of request.getAttribute("messages"), but then there's a
fragile dependency between the page and the bean on the key specified in
loadBundle.
So, the goal is build a localized list of select items, right? In the
backing bean, you'd use the normal ResourceBundle APIs -- the only
thing you
need to know from the page is what locale to use. Let's do a simple case
where you know the list of message keys needed to look up the
descriptions,
and you use the message keys themselves as the values (lots of other
things
are possible, of course -- this just makes the example simple):
private String categoryKeys[] = { "category.all", "category.java", "
category.music" };
public SelectItem[] getCategories() {
// What locale are we using for this request?
Locale locale = FacesContext.getCurrentInstance
().getViewRoot().getLocale();
// Look up the corresponding resource bundle
ResourceBundle bundle =
ResourceBundle.getBundle("com.mycompany.mypackage.MyBundle",
locale);
// Assemble an array of SelectItem instances
SelectItem results[] = new SelectItem[categoryKeys.length];
for (int i = 0; i < categoryKeys.length; i++) {
results[i] = new SelectItem(categoryKeys[i], bundle.getString
(categoryKeys[i]);
}
// Return the localized set of category descriptions
return results;
}
A more robust implementation would probably cache these arrays in a Map
(keyed by Locale) the first time you created one, to avoid the processing
cost of dynamically building the list every time.
Yeah, it's trivial to do it that way; but then you're loading the
resource bundle twice: once in the page via f:loadBundle and once in the
backing bean via getBundle(). Appart from the minor inefficiency,
there's now an implicit dependency; if you change the bundle in one
place you need to know to change it in the other to maintain consistency.
Maybe I'm over thinking things, but I was hoping I could somehow access
the bundle that was loaded in the page rather than explicitly loading
one in the bean.
Heh, never mind, I finally figured out the obvious: I can just make the
bundle name a property on the backing bean and reference that in the
loadBundle tag. If I make it a managed property, all the better as it's
then a simple config change to switch in a different bundle.
I guess I'm still too used to striving to keep presentation details out
of my Java code ;-)
L.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]