2011-05-06 56 views
0

我想在JSP中使用JSTL標籤國際化我的應用程序。我的問題是,屬性文件的內容在UI中不可見。使用jstl標籤製作應用國際化

JSP代碼

<%@ 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??? 
+0

你會如此善待花時間閱讀[如何問這裏的問題](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question。 ASPX)?你的問題有嚴重的格式問題,並不明顯你要求什麼。你剛纔說你有問題。 – 2011-05-06 07:32:45

+0

[How to internationalize a java web application。](http://stackoverflow.com/questions/4276061/how-to-internationalize-a-java-web-application)可能的重複 – BalusC 2011-05-06 20:55:53

回答

1

我相信這是我們需要開始。創建一個名爲兩個文件:

  1. ApplicationResources.properties
  2. ApplicationResources_it.properties

兩者應該都會有(至少)類似的條目:

welcome.message=Hello,{0} 

刪除這些不必要的聲明:

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

<fmt:setLocale value="it"/>移動到頁面頂部。它會以任何方式設置整個頁面的語言環境,我不認爲有混合語言的方式(感謝上帝)。