ba0708
ba0708

Reputation: 10599

JSTL formatDate ignoring locale

I want to localize dates with JSTL, and I am trying to do it like below.

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<fmt:setLocale value="da_DK" scope="application" />
<fmt:formatDate value="${some.date}" dateStyle="FULL" />

some.date is a java.util.Date instance. I have played around with various different locales, but nothing is working. I get the following output no matter which locale I choose: Tuesday, January 13, 2015. I also tried the following, after removing the above call to setLocale():

<jsp:useBean id="now" class="java.util.Date" />

<fmt:setLocale value="en_US" />
<fmt:formatDate value="${now}" dateStyle="FULL" />

<fmt:setLocale value="fr_FR" />
<fmt:formatDate value="${now}" dateStyle="FULL" />

The above outputs Tuesday, January 13, 2015 two times, and the same happens for every locale that I tried. The only thing I have configured related to locale is the following (Spring MVC project):

@Bean
public CookieLocaleResolver localeResolver() {
    Locale locale = new Locale("dk");

    CookieLocaleResolver localeResolver = new CookieLocaleResolver();
    localeResolver.setDefaultLocale(locale);

    return localeResolver;
}

Shouldn't the text be translated into the language defined by the locale, or the format changed at the very least? Any ideas what I am doing wrong here? I tried every example that I could find online, but the result is the same.

Upvotes: 4

Views: 6392

Answers (1)

Ashutosh Srivastav
Ashutosh Srivastav

Reputation: 669

I would just change the scope to session as below

<fmt:setLocale value="fr_FR" scope="session"/>

Date in France:
<fmt:formatDate value="${now}" dateStyle="full"/> <br/>

<fmt:setLocale value="en_US" scope="session"/>
 Date in US: 
<fmt:formatDate value="${now}" dateStyle="full" /> <br/>

Upvotes: 4

Related Questions