Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts Users »

Re: fmt:message, fmt:setLocale and struts

Kris Schneider

2003-11-11

Replies:

The JSTL algorithm will only use the root resource (your
ApplicationResources.properties) as a last resort. It will walk through the set
of locales returned by ServletRequest.getLocales and try to find a match. If it
can't, JSTL implements the concept of a fallback locale that it will attempt to
match. If a match is still not found, only then will the root resource get
loaded. So, if your set of locales is:

en
de

And you've got the following resources:

ApplicationResources.properties
ApplicationResources_de.properties

Then JSTL will find a match for "de" and use it. If you add the resource:

ApplicationResources_en.properties

Then it'll match "en" first. If you just make the "en" resource an empty file,
it'll pick up all the entries from the root. Kooky, huh? If you're using a
Servlet 2.3 container, why not use a Filter to keep the Struts and JSTL locales
in sync? Something like this might work (untested):

import javax.servlet.jsp.jstl.core.Config;
...
HttpSession session = request.getSession(true);
Locale locale = (Locale)session.getAttribute(Globals.LOCALE_KEY);
if (locale == null) {
locale = request.getLocale();
}
Config.set(session, Config.FMT_LOCALE, locale);

Quoting Adam Hardy <ahardy.struts@(protected)>:

> Struts and JSTL seem to have different algorithms to establish the
> locale to use.
>
> I have JSPs which use <fmt:message> for titles, labels, comments etc and
> I have also just started to program a page which will include an
> underlying language page to incorporate big paragraphs of localized text.
>
> I thought I would use the struts locale setting in my session for this,
> but then I found that with my current browser settings, struts has
> chosen EN but JSTL has chosen DE.
>
> I have EN as my first locale preference in my browser, followed by DE,
> being in Germany.
>
> In my app I have a default ApplicationResources.properties and just now
> to create a test, I copied it to ApplicationResources_de.properties.
>
> Bug or feature, logically expected or are Struts and JSTL purposefully
> diverging?
>
>
> Adam
>
> PS if I rename ApplicationResources to ApplicationResources_en then JSTL
> and Struts agree again, on EN.
>
> PPS I can work-around with this:
> <c:set var="localeKey">
>   <%=org.apache.struts.Globals.LOCALE_KEY %>
> </c:set>
> <fmt:setLocale value="${sessionScope[localeKey]}"/>
>
> to make JSTL follow Struts.
>
> --
> struts 1.1 + tomcat 5.0.12 + java 1.4.2
> Linux 2.4.20 RH9

--
Kris Schneider <mailto:kris@(protected)>
D.O.Tech     <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@(protected)
For additional commands, e-mail: struts-user-help@(protected)



©2008 junlu.com - Jax Systems, LLC, U.S.A.