发布于 2015-08-17 14:47:17 | 103 次阅读 | 评论: 0 | 来源: 网络整理

<fmt:setLocale> 标签用于给定语言环境存储在区域配置变量。

属性:

<fmt:setLocale>一样标签具有以下属性:

属性 描述 必须 默认
value Specifies a two-part code that represents the ISO-639 language code and an ISO-3166 country code. Yes en_US
variant Browser-specific variant No None
scope Scope of the locale configuration variable No Page

例子:

资源包包含特定于语言环境的对象。资源包包含键/值对。当程序需要特定于语言环境的资源,把所有的共同所有的语言环境中的密钥,但可以有特定的语言环境转换的值。资源包有助于提供内容具体的语言环境。

一个Java资源包文件包含一系列关键到字符串的映射。我们专注于该方法涉及到创建编译的Java类来扩展java.util.ListResourceBundle类。你必须编译这些类文件,并提供给Web应用程序的classpath中可用。

让我们定义一个默认的资源包,如下所示:


package com.yiibai;

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

现在,让我们定义我们将使用西班牙语语言环境多一个资源包:


package com.yiibai;

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

让我们来编译上面的类Example.class和Example_es_ES.class,让他们在Web应用程序的类路径中。现在可以使用下面的JSTL标记来显示三个号码如下:


<%@ 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.yiibai.Example">
   <fmt:message key="count.one"/><br/>
   <fmt:message key="count.two"/><br/>
   <fmt:message key="count.three"/><br/>
</fmt:bundle>

<!-- Change the Locale -->
<fmt:setLocale value="es_ES"/>
<fmt:bundle basename="com.yiibai.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> 标签,了解完整的概念。

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务