" variant="" scope=""/>"> " variant="" scope=""/>" />
Easy Tutorial

<fmt:setLocale> Tag

JSP Standard Tag Library

The <fmt:setLocale> tag is used to store the given locale in the locale configuration variable.

Syntax

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

Attributes

The <fmt:setLocale> tag has the following attributes:

Attribute Description Required Default Value
value Specifies the ISO-639 language code and ISO-3166 country code Yes en_US
variant Specific browser variant No None
scope Scope of the locale configuration variable No Page

Example Program

Resource bundles contain locale-specific objects. Resource bundles contain key-value pairs. When your program needs locale-specific resources, you can share all key pairs for all locales, but you can also specify translated values for a locale. Resource bundles help provide content specific to a locale.

A Java resource bundle file contains a series of key-value pairs. The method we are concerned with involves creating a compiled Java class that inherits from java.util.ListResourceBundle. You must compile these classes and place them in your Web application's CLASSPATH.

Let's define a default resource bundle:

package com.tutorialpro;

import java.util.ListResourceBundle;

public class Example_En extends ListResourceBundle {
  public Object[][] getContents() {
    return contents;
  }
  static final Object[][] contents = {
  {"count.one", "One"},
  {"count.two", "Two"},
  {"count.three", "Three"},
  };
}

Now, let's define a resource bundle for the Spanish locale:

package com.tutorialpro;

import java.util.ListResourceBundle;

public class Example_es_ES extends ListResourceBundle {
  public Object[][] getContents() {
    return contents;
  }
  static final Object[][] contents = {
  {"count.one", "Uno"},
  {"count.two", "Dos"},
  {"count.three", "Tres"},
  };
}

Compile the above files into Example.class and Example_es_ES.class, and then place them in your Web application's CLASSPATH. Now you can use the JSTL tags to display these three numbers like this:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
<title>JSTL fmt:setLocale Tag</title>
</head>
<body>

<fmt:bundle basename="com.tutorialpro.Example">
   <fmt:message key="count.one"/><br/>
   <fmt:message key="count.two"/><br/>
   <fmt:message key="count.three"/><br/>
</fmt:bundle>

<!-- Change locale -->
<fmt:setLocale value="es_ES"/>
<fmt:bundle basename="com.tutorialpro.Example">
   <fmt:message key="count.one"/><br/>
   <fmt:message key="count.two"/><br/>
   <fmt:message key="count.three"/><br/>
</fmt:bundle>

</body>
One 
Two 
Three
Uno
Dos
Tres

See <fmt:bundle> and <setBundle> for more information.


JSP Standard Tag Library