velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mike Kienenberger <>
Subject Re: Locale attribute for html
Date Wed, 08 Dec 2004 19:50:08 GMT
"Nadesan, Binu" <> wrote:
> How do I set the locale attribute for HTML element in velocity.

Two problems.

> #set($locale='$org.apache.struts.action.LOCALE')

First, you need to get this out of the request (or is it the session?) 
context.  I think request/session/application attributes are normally dumped 
in the context, but because "." has a special meaning in velocity (method 
call), you can't have an attribute with dots in the name.

Probably something like the following might work (exact syntax not 

#set($locale=$request.getAttribute('org.apache.struts.action.LOCALE') )

However, I'd recommend you use $msg.getLocale() instead of getting it 
yourself.  This assumes that you have the MessageTool installed in the 
context as $msg.

This will automatically call the right code for you:

        Locale locale = null;

        if (session != null)
            locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
        if (locale == null)
            locale = request.getLocale();
        return locale;

And it'll be updated in future versions as struts changes, so you won't have 
to redo this when you upgrade struts.

> <html lang=$locale>

Second, It seems unlikely to me that Locale.toString() is going to return 
what you're looking for.

I don't know what value that lang=x expects, but it's probably something 
like $locale.getCountry() or $locale.getDisplayLanguage()


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message