<fmt:setLocale> 标签

约 1927 字 更新于 2026-11-18 5iyo.com

<fmt:setLocale> 标签

<fmt:setLocale>标签用来将给定的区域存储在locale配置变量中。

语法格式

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

属性

<fmt:setLocale>标签有如下属性:

属性 描述 是否必要 默认值
value 指定ISO-639 语言码和ISO-3166 国家码 是 en_US
variant 特定浏览器变体 否 无
scope Locale配置变量的作用域 否 Page


程序示例

资源束包含区域特定对象。资源束包含键值对。当您的程序需要区域特定资源时,可以将所有的关键词对所有的locale共享,但是也可以为locale指定转换后的值。资源束可以帮助提供指定给locale的内容。

一个Java资源束文件包含一系列的键值对。我们所关注的方法涉及到创建继承自java.util.ListResourceBundle 类的已编译Java类。您必须编译这些类然后放在您的Web应用程序的CLASSPATH中。

让我们来定义一个默认的资源束:


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"},

  };

}

现在,再定义一个资源束,用于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"},

  };

}

编译以上文件至Examble.class和Examble_es_ES.class中,然后将它们放在Web应用程序的CLASSPATH中。现在可以使用JSTL标签来显示这三个数字,就像这样:


<%@ 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.runoob.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.runoob.Example">

   <fmt:message key="count.one"/><br/>

   <fmt:message key="count.two"/><br/>

   <fmt:message key="count.three"/><br/>

</fmt:bundle>



</body>

</html>

运行结果如下:


One 

Two 

Three

Uno

Dos

Tres

查看<fmt:bundle>和<setBundle>来获取更多信息。