Web | This Site

Basic JSP Tutorials
Advanced JSP Tutorials
JSP Useful References
JSP Useful Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# JSTL Core <fmt:setLocale> Tag

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

## Attribute:

The <fmt:setLocale> tag has following attributes:

Attribute | Description | Required | Default
--- | --- | --- | ---
value | Specifies a two-part code that represents the ISO-639 language code and an ISO-3166 country code. | Yes | en_US
variant | Browser-specific variant | No | None
scope | Scope of the locale configuration variable | No | Page

## Example:

Resource bundles contain locale-specific objects. Resource bundles contain key/value pairs. When your program needs a locale-specific resource, you keep all the keys common to all the locale but you can have translated values specific to locale. Resource bundles helps in providing content specific to locale.

A Java resource bundle file contains a series of key-to-string mappings. The method that we focus on involves creating compiled Java classes that extend the java.util.ListResourceBundle class. You must compile these class files and make them available to the classpath of your Web application.

Let us define a default resource bundle as follows:

    package com.tutorialspoint;

    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 us define one more resource bundle which we will use for Spanish Locale:

    package com.tutorialspoint;

    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"},
      };
    }

Let us compile above classes Example.class and Example_es_ES.class and make them available in the CLASSPATH of your Web application. Now you can use the following JSTL tags to display the three numbers as follows:

    <%@ 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.tutorialspoint.Example">
       <fmt:message key="count.one"/><br/>
       <fmt:message key="count.two"/><br/>
       <fmt:message key="count.three"/><br/>
    </fmt:bundle>

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

    </body>
    </html>

This would produce following result:

    One
    Two
    Three
    Uno
    Dos
    Tres

Check <fmt:bundle> and <setBundle> tags to understand complete concept.

Advertisement

Online Image Processing

Indian Baby Names