<fmt:setLocale> Tag
The <fmt:setLocale> tag is used to store the given locale in the locale configuration variable.
Syntax Format
<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 | Browser-specific variant | no | None |
| scope | Scope of the locale configuration variable | no | Page |
Program Example
Resource bundles contain locale-specific objects. Resource bundles contain key-value pairs. When your program needs locale-specific resources, you can share all key-value pairs across all locales, but you can also specify translated values for a locale. Resource bundles help provide locale-specific content.
A Java resource bundle file contains a series of key-value pairs. The method we are concerned with involves creating compiled Java classes that inherit from the java.util.ListResourceBundle class. You must compile these classes and place them in the CLASSPATH of your Web application.
Let us define a default resource bundle:
package com.example;
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, define another resource bundle for the Spanish Locale:
package com.example;
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 Examble.class and Examble_es_ES.class, then place them in the CLASSPATH of the Web application. Now you can 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.example.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.example.Example">
<fmt:message key="count.one"/><br/>
<fmt:message key="count.two"/><br/>
<fmt:message key="count.three"/><br/>
</fmt:bundle>
</body>
</html>
The output is as follows:
One Two Three Uno Dos Tres
See<fmt:bundle>and<setBundle>for more information.
Other Extensions
JSP Standard Tag Library