Servlet与Jsp学习笔记--10、Localized
Jsp
<%@ page contentType="text/html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
<%--
Set the locale to the selected one, if any. Otherwise, let the
<fmt:setBundle> action pick the best one based on the Accept-Language
header.
--%>
<c:if test="${param.language == 'en'}">
<fmt:setLocale value="en" scope="session" />
</c:if>
<c:if test="${param.language == 'sv'}">
<fmt:setLocale value="sv" scope="session" />
</c:if>
<c:if test="${param.language == 'de'}">
<fmt:setLocale value="de" scope="session" />
</c:if>
<fmt:setBundle basename="pages" var="pagesBundle" />
<fmt:setBundle basename="labels" scope="session" />
<html>
<head>
<title>
<fmt:message key="title" />
</title>
</head>
<body bgcolor="white">
<h1>
<fmt:message key="title" />
</h1>
<fmt:message key="select_language" />:
<form action="poll.jsp">
<p>
<c:set var="currLang" value="${pagesBundle.locale.language}" />
<input type="radio" name="language" value="en"
<c:if test="${currLang == 'en'}">checked</c:if>>
<fmt:message key="english" /><br>
<input type="radio" name="language" value="sv"
<c:if test="${currLang == 'sv'}">checked</c:if>>
<fmt:message key="swedish" /><br>
<input type="radio" name="language" value="de"
<c:if test="${currLang == 'de'}">checked</c:if>>
<fmt:message key="german" /><br>
<p>
<input type="submit"
value="<fmt:message key="new_language" />">
</form>
<a href="<fmt:message key="details_page" bundle="${pagesBundle}" />">
<fmt:message key="question" />
</a>
<form action="calculate.jsp" method="post">
<input type="radio" name="answerId" value="1" checked>
<fmt:message key="answer1" />
<br>
<input type="radio" name="answerId" value="2">
<fmt:message key="answer2" />
<br>
<input type="radio" name="answerId" value="3">
<fmt:message key="answer3" />
<p>
<input type="submit"
value="<fmt:message key="submit" />">
</form>
</body>
</html>
Properties
pages_en.properties:
details_page=details_en.jsp
pages_de.properties:
details_page=details_de.jsp
labels_en.properties:
title=Industry Trends
select_language=Select your preferred language
new_language=New Language
english=English
swedish=Swedish
german=German
question=What's the longest development time you dare to plan with?
answer1=One year
answer2=Six months
answer3=Less than six months
result1=One year {0, number, integer}% ({1, number, integer})
result2=Six months {0, number, integer}% ({1, number, integer})
result3=Less than six months {0, number, integer}% ({1, number, integer})
submit=Vote
number_of_votes=Totalt number of votes
result=Poll result
labels_de.properties:
title=Industrietendenzen
select_language=W鋒len Sie die gew黱schte Sprache
new_language=Neue Sprache
english=Englisch
swedish=Schwedisch
german=Deutsche
question=Mit welcher Entwicklungszeit wagen Sie maximal zu planen?
answer1=Ein Jahr
answer2=Sechs Monate
answer3=Weniger als sechs Monate
result1=Ein Jahr {0, number, integer}% ({1, number, integer})
result2=Sechs Monate {0, number, integer}% ({1, number, integer})
result3=Weniger als sechs Monate {0, number, integer}% ({1, number, integer})
submit=Abstimmen
number_of_votes=Gesamtzahl von Stimmen
result=Umfrageergebnis
Formatting
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%--include this taglib for i18n related actions --%>
<%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<html>
<head><title>Formatting numbers and dates</title></head>
<body>
<h2>Dates and numbers in Swiss and US style formats</h2>
<%-- create an object representing the current date --%>
<jsp:useBean id="now" class="java.util.Date"/>
<%-- set the locale to German language, Swiss country code --%>
<fmt:setLocale value="de_CH"/>
<strong>Swiss-style date:</strong>
<%-- output the date --%>
<fmt:formatDate type=
"both" value="${now}" dateStyle="full" timeStyle="short" />
<br />
<strong>Swiss-style number:</strong>
<%-- output the equivalent of java.util.Date.getTime( ) to show how numbers are formatted
--%>
<fmt:formatNumber value="${now.time}" />
<br /><br />
<%-- reset the locale to English language, US country code --%>
<fmt:setLocale value="en_US"/>
<strong>US-style date:</strong>
<%-- output the date --%>
<fmt:formatDate type="both" value="${now}" dateStyle=
"full" timeStyle="short" />
<br />
<strong>US-style number:</strong>
<fmt:formatNumber value="${now.time}" />
<br /><br />
</body>
</html>
目前维护的开源产品:https://gitee.com/475660