Latest web development tutorials

<Fmt: setLocale> tag

JSP Standard Tag Library JSP Standard Tag Library

<Fmt: setLocale> tag is used in a given area stores locale configuration variable.

Syntax

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

Attributes

<Fmt: setLocale> tag has the following attributes:

Attributes description If necessary Defaults
value Specifies the ISO-639 language code and ISO-3166 country code Yes en_US
variant Browser-specific variants no no
scope Scope Locale configuration variables no Page


Program Example

Resource bundles contain locale-specific objects. Resource bundles contain key-value pairs. When your program needs to region-specific resources, we can all share the locale for all keywords, but you can also specify the converted value for the locale. It can help provide resource bundle locale assigned to the content.

A Java resource bundle file contains a list of key-value pairs. The method we are concerned involves creating a class inherits from java.util.ListResourceBundle compiled Java classes. You must compile these classes and then placed in CLASSPATH your Web application program.

Let's define a default resource bundle:

package com.w3big;

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 Spanish Locale:

package com.w3big;

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 documents to Examble.class and Examble_es_ES.class, and then put them in CLASSPATH Web application. You can now use 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 标签</title>
</head>
<body>

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

<!-- 修改地区-->
<fmt:setLocale value="es_ES"/>
<fmt:bundle basename="com.w3big.Example">
   <fmt:message key="count.one"/><br/>
   <fmt:message key="count.two"/><br/>
   <fmt:message key="count.three"/><br/>
</fmt:bundle>

</body>
</html>

Results are as follows:

One 
Two 
Three
Uno
Dos
Tres

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


JSP Standard Tag Library JSP Standard Tag Library