To demonstrate the problem first create two resource bundle properties files as follows: -- labels_en.properties: hello=hello goodbye=goodbye -- labels_es.properties: hello=hola goodbye=adios -- put them in a jar and place the jar in the WEB-INF/lib dir of a webapp now place the following code into a .jsp file and put it in the main dir of the same webapp where you put the resource bundle jar -- <%-- possible tomcat bug when using JSTL fmt:setLocale --%> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %> <html> <body> <c:if test="${param.locale != null}"> <fmt:setLocale value="${param.locale}" scope="session" /> </c:if> <a href="?locale=en">English</a> - <a href="?locale=es">Spanish</a> - <br /><br /> <jsp:useBean id="now" class="java.util.Date" /> The date functions seem to work correctly with regard to locale.<br /> Date: <fmt:formatDate value="${now}" dateStyle="full" /><br /> Time: <fmt:formatDate value="${now}" type="time" /><br /> <br /> <%-- create a jar with the resource bundles .class and/or .properties files that contain the key=value pairs: e.g.: labels_en.properties, labels_es.properties and place it in the WEB-INF/lib dir of your webapp --%> <fmt:setBundle basename="labels" var="labelsBundle" scope="session"/> <fmt:bundle basename="labels"> The bundle functions do not seem to work correctly with regard to locale<br /> unless we specifically do a <fmt:setLocale value="hard_coded_string" />.<br /> It seems as though the fmt:setLocale is interpreted at compile time not runtime.<br /> Hello: <fmt:message key="hello" /><br /> Goodbye: <fmt:message key="goodbye" /> </fmt:bundle> </body> </html> -- bring up browser and access the .jsp file http://localhost:8080/thewebapp/thefile.jsp
Your jsp source-code seems a mistake. To the <fmt:bundle> tags in the jsp you wrote, please specify the "bundle" attribute given the "labelsBundle" EL value. For examples, ............................................................ ............................................................ <fmt:setBundle basename="labels" var="labelsBundle" scope="session"/> Hello: <fmt:message key="hello" bundle="${labelsBundle}" /><br /> Goodbye: <fmt:message key="goodbye" bundle="${labelsBundle}" /> Regards, Kan Ogawa
Yes, you can do it this way by declaring the bundle in every single fmt: statement, but it should not be necessary according to the specification and therefore this is a bug. From Section 9.2 of the JSR-52 JavaServer Pages™ Standard Tag Library 1.0 (JSTL 1.0) specification: -- <fmt:bundle> action If a formatting action is nested inside a <fmt:bundle> action (see Section 8.6), the locale of the i18n localization context of the enclosing <fmt:bundle> action is used as the formatting locale. ... --
When I try to use fmt:setLocale, supplying a scope attribute, the server throws a RuntimeException. Example: <fmt:setLocale value="ja" scope="session" /> Error message: Illegal scope attribute without var in "fmt:setLocale" tag Exception: java.lang.RuntimeException: org.apache.jasper.JasperException: <h3>Validation error messages from tag library fmt</h3>Illegal scope attribute without var in "fmt:setLocale" tag. The JSTL spec (version 1.0) for fmt:setLocale is: <fmt:setLocale value="locale" [variant="variant"] [scope="{page|request|session|application}"]/> therefore I cannot supply a var attribute, nor would it make much sense to do so. Is there another way to set the Locale at the session scope for use with the other fmt tags? This was tested on standard taglib release 1.0.2, servlet engine: JBoss v3.0.4 Regards, Brad.
I've just committed a new sample JSP page to test the behavior described in this bug report. I have tested it with tomcat 4.1.24 and everything ran according to spec. Please get a copy of the nightly of the 1.0 branch (STANDARD_1_0_BRANCH) and run that sample JSP page (called 'Demo', under the 'formatting and I18N' category).