Thursday, January 3, 2013

JSP internationalization - localization with jstl

Demo cài đặt tính năng hỗ trợ quốc tế hóa và cục bộ hóa cho JSP

Trang tiếng việt và định dạng theo Việt Nam
jsp_1

Chọn ngôn ngữ khác trong submit lại và đổi giao diện
jsp_2

[sourcecode language="html"]
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<script>
function ChangeValue()
{
document.frm.submit();
}
</script>

<style>
#grp
{
width: 400px;
}
</style>

<c:if test="${not empty param.language}">
<fmt:setLocale value="${param.language}" scope="session"/>
<c:set var= "currLang" value="${param.language}" />
</c:if>

<c:if test="${empty param.language}">
<fmt:setLocale value="<%=request.getLocale().getLanguage()%>" scope="session"/>
<c:set var= "currLang" value="<%=request.getLocale().getLanguage()%>" />
</c:if>

<fmt:setBundle basename="input" scope="session" />

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> <fmt:message key="title" /> </title>
</head>
<body>

<h1><fmt:message key="title" /></h1>
<form action="index.jsp" method="post" name="frm">
<table>
<tr>
<td><fmt:message key="date" /></td>
<td><input type="text" name="dates"/></td>
</tr>
<tr>
<td><fmt:message key="number" /></td>
<td><input type="text" name="number"/></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="<fmt:message key="submit" />"></td>
</tr>
</table>

<fieldset id="grp">
<legend>Language</legend>
<input type="radio" name="language" onchange="ChangeValue();" value="vi" <c:if test="${currLang=='vi'}">checked</c:if> />Tieng Viet
<input type="radio" name="language" onchange="ChangeValue();" value="en" <c:if test="${currLang=='en'}">checked</c:if> />English
<input type="radio" name="language" onchange="ChangeValue();" value="de" <c:if test="${currLang=='de'}">checked</c:if> />german
</fieldset>
<fieldset id="grp">
<legend>Format</legend>
Định dạng ngày <fmt:formatDate value="<%=new java.util.Date()%>" dateStyle="full" type="both" /><br>
Number: <fmt:formatNumber value="1000.9" pattern="####.00"/>
</fieldset>
</form>
</body>
</html>
[/sourcecode]

- Create properties file có tên input.properties
- Click chuột phải add locale thêm 02 ngôn ngữ tiếng Việt và Tiếng Anh
- Click chuột phải lên file input.properties chọn Open
- Click add new property như sau

jsp_3

Chúc các bạn thành công !

No comments:

Post a Comment

Translate