fmt:setLocale tag
fmt:setLocaleThe tag is used to store a given locale in the locale configuration variable.
Syntax format
property
fmt:setLocaleThe tag has the following attributes:
| property | Description | Whether it is required | Default value |
|---|---|---|---|
| value | Specifies the ISO-639 language code and ISO-3166 country code | Is | en_US |
| variant | A specific browser variant | No | None |
| scope | The scope of the Locale configuration variable | No | Page |
Program Example
A resource bundle contains locale-specific objects. A resource bundle contains key-value pairs. When your program needs locale-specific resources, you can share all the keys across 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 approach 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's define a default resource bundle:
package com.runoob;
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.runoob;
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 web application's CLASSPATH. 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" %>
JSTL fmt:setLocale 标签
The running result is as follows:
One
Two
Three
Uno
Dos
Tres
Checkfmt:bundleand
JSP Standard Tag Library