English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
<fmt:setLocale> tag is used to store the given locale in the locale configuration variable.
<fmt:setLocale value="<string>" variant="<string>" scope="<string>"/>
<fmt:setLocale> tag has the following attributes:
Attribute | Description | Is Necessary | Default Value |
---|---|---|---|
value | Specify ISO-639 Language Code and ISO-3166 Country Code | Yes | en_US |
variant | Specific Browser Variant | No | None |
scope | Scope of Locale Configuration Variables | No | Page |
Resource bundles contain locale-specific objects. Resource bundles contain key-value pairs. When your program needs locale-specific resources, you can share all the key-value pairs for all locales, but you can also specify the translated values for the locale. Resource bundles can help provide content specified for the locale.
A Java resource bundle file contains a series of key-value pairs. The methods we are concerned with involve 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.w3codebox; 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.w3codebox; 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, and then place them in the CLASSPATH of the web application. 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.w3codebox.Example"> <fmt:message key="count.one">/><br/> <fmt:message key="count.two">/><br/> <fmt:message key="count.three">/><br/> </fmt:bundle> <!-- Modify region--> <fmt:setLocale value="es_ES">/> <fmt:bundle basename="com.w3codebox.Example"> <fmt:message key="count.one">/><br/> <fmt:message key="count.two">/><br/> <fmt:message key="count.three">/><br/> </fmt:bundle> </body> </html>
The running result is as follows:
One Two Three One Two Three
View<fmt:bundle>and<setBundle>for more information.