English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Biblioteca de etiquetas estándar de JSP
La etiqueta <fmt:setLocale> se utiliza para almacenar el área dado en la variable de configuración locale.
<fmt:setLocale value="<string>" variant="<string>" scope="<string>"/>
<fmt:setLocale> tag has the following attributes:
Attribute | Description | Is it 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 with all locales, but you can also specify the converted values for the locale. Resource bundles can help provide content for the specified 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, 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 etiqueta</<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> <!-- Modificar la región--> <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>
Los resultados de ejecución son los siguientes:
One Two Three Uno Dos Tres
Ver<fmt:bundle>y<setBundle>para obtener más información。