使用jstl标签使应用程序国际化

时间:2011-05-06 06:18:49

标签: internationalization jstl

我正在尝试使用JSP中的JSTL标记使我的应用程序国际化。我的问题是,属性文件的内容在UI中不可见。

JSP CODE

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
This Example demonstrates the basic JSTL formating tags:
<br/><br/>
Locale from client:

<b><c:out value="${pageContext.request.locale}"/></b><br/>

<fmt:setBundle basename="ApplicationResources" var="mybundle"/>
<fmt:message key="welcome.message" bundle="${mybundle}">
   <fmt:param value="${param.uname}"></fmt:param>
</fmt:message>
<b>Now testing &lt;fmt:setLocale&gt;tag:</b><br>
Creating a ResourceBundle with client locale and setting it to <i>mybundle1</i>
variable.<br>
<fmt:setBundle basename="ApplicationResources" var="mybundle1"/>
Setting the locale to<i>it</i>(italian).<br/>
<fmt:setLocale value="it"/>
Creating a ResourceBundle with <i>it</i>(italian) locale and setting it to <i>mybundle2</i> variable.<br><br>
<fmt:setBundle basename="ApplicationResources" var="mybundle2"/>
<b>Message using <i>mybundle1</i>:</b><br>
<pre>
 <fmt:message bundle="${mybundle1}" key="welcome.message">

 </fmt:message>
</pre>
<br>
<b>Message using <i>mybundle2</i>:</b><br>
<pre>
<fmt:message bundle="${mybundle2}" key="welcome.message">

</fmt:message>
</pre>
</body>
</html>

渲染的输出为:

This Example demonstrates the basic JSTL formating tags:

Locale from client: en_US
???welcome.message??? Now testing <fmt:setLocale>tag:
Creating a ResourceBundle with client locale and setting it to mybundle1 variable.
Setting the locale toit(italian).
Creating a ResourceBundle with it(italian) locale and setting it to mybundle2 variable.

Message using mybundle1:

 ???welcome.message???


Message using mybundle2:

???welcome.message???

1 个答案:

答案 0 :(得分:1)

我相信这是我们需要开始的。创建两个名为:

的文件
  1. ApplicationResources.properties
  2. ApplicationResources_it.properties
  3. 两者都应包含(至少)类似的条目:

    welcome.message=Hello,{0}
    

    删除这些不必要的声明:

    <fmt:setBundle basename="ApplicationResources" var="mybundle1"/>
    <fmt:setBundle basename="ApplicationResources" var="mybundle2"/>
    

    <fmt:setLocale value="it"/>移至页面顶部。它会以任何方式设置整个页面的区域设置,我认为没有办法混合语言(感谢上帝)。