In JSTL, the <fmt:setLocale> tag is used for localization of the web application. We use this tag to set the locale specific information in the configuration variable. Each HTTP request has a header Accept-Language sent by the browser as part of the request. JSTL tags usually verify this header when formatting the data. If no header is provided in the HTTP request, we can set the default locale to use using the configuration variable. If locale configuration variable is not set, then the JVM's locale is used obtained from the container the JSP is running in.

We can override this behavior by setting the user's locale so that we can load locale specific resource bundle to show localized messages.

 

JSTL <fmt:setLocale> Syntax:

<fmt:setLocale value="<string>" variant="<string>" scope="<string>"/>

 

Attributes:

Name

Required

Type

Description

value

True

java.lang.String

String value to represent the locale. This String value must contain lower case two letter language letters and optionally upper case two letter country code. For example, en_US where en represents English and US represents United States.

variant

False

java.lang.String

Browser specific variant.

scope

False

java.lang.String

Scope of the locale configuration variable to store.

 

JSTL <fmt:setLocale> Example:

The below code sets fr_CA (fr for French and CA for Canada) as locale using the <fmt:setLocale> tag. Once we access the JSP which contains this code the locale specific resource bundle is loaded and we can see the messages are displayed in French language.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>&lt;fmt:setLocale&gt; Demo</title>
  </head>
  <body>
    <h1>&lt;fmt:setLocale&gt; Demo</h1>
    <fmt:setLocale value="fr_CA"/>
    <fmt:bundle basename="net.codejava.jstl.messages">
       <fmt:message key="count.one"/><br/>
       <fmt:message key="count.two"/><br/>
       <fmt:message key="count.three"/><br/>
    </fmt:bundle>
  </body>
</html>

Note that in the <fmt:bundle> tag we used the net.codejava.jstl.messages   as our resource bundle name. Since we set the locale to fr_CA, the actual resource bundle loaded is net.codejava.jstl.messages_fr_CA.propertiesprovided this bundle is available in the classpath. The keys are same in both resource bundles (English and French), however the values are different to display language specific messages. 

Also note that when we access the JSP, if we examine the HTTP request being sent, we can see that request header Content-Language is being set as fr-CA which is the locale we set.

setlocale-1

 

Output:

setlocale2

 

Recommended Usage of JSTL <fmt:setLocale> tag:

The <fmt:setLocale> is precisely useful in scenarios where web application needs to be localized. Sometimes we may need to show language specific messages in the midst of the web page. We can conveniently use this tag to set the locale and to load language specific messages.

 

Other JSTL Format Tags:

bundle  |  formatDate  |  formatNumber  |  message  |  param  |  parseDate  |  parseNumber  |  requestEncoding  |  setBundle  |  setTimeZone  |  timeZone


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment