< Back
Page 4 of 4
Like this article? We recommend
Conclusions
JSTL provides a uniform way to handle multilingual sites, which seamlessly integrates multilingual tags with your HTML tags. Another advantage of this standardization is that HTML editors should now be able to better support multilingual website development because there is now a standard way to do this. For a more complex example of multilingual and international programming, refer to my book, JSTL: JSP Standard Tag Library Kick Start (Sams, 2002).
Listing 1: Login Page (index.jsp)
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %> <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> <c:if test="${lang==null}"> <fmt:setBundle basename="com.heaton.bundles.Forum" var="lang" scope="session"/> </c:if> <c:if test="${param.lang!=null}"> <fmt:setLocale value="${param.lang}"/> <fmt:setBundle basename="com.heaton.informit.I18NBundle" var="lang" scope="session"/> <c:redirect url="index.jsp"/> </c:if> <html> <head> <title>I18N Example</title> </head> <body> <h1><fmt:message key="login.pleaselogin" bundle="${lang}"/></h1> <table> <form method=post action=main.jsp> <tr><td><fmt:message key="login.uid" bundle="${lang}"/></td><td> <input name=uid></td></tr> <tr><td><fmt:message key="login.pwd" bundle="${lang}"/></td><td> <input name=pwd></td></tr> <tr><td colspan=2> <input type="submit" name="action" value="<fmt:message key="login.title" bundle="${login}"/>"> </td></tr> </form> </table> <h1><fmt:message key="login.language" bundle="${lang}"/></h1> <ul> <li><a href="index.jsp?lang=en"> <fmt:message key="login.english" bundle="${lang}"/>(English)</li> <li><a href="index.jsp?lang=es"> <fmt:message key="login.spanish" bundle="${lang}"/>(Spanish)</li> <li><a href="index.jsp?lang=zh"> <fmt:message key="login.chinese" bundle="${lang}"/>(Chinese)</li> </ul> </body> </html>
Listing 2: English Resource Bundle
package com.heaton.informit; import java.util.*; public class I18NBundle_en extends ListResourceBundle { public Object[][] getContents() { return contents; } static final Object[][] contents = { {"login.pleaselogin","Please Login"}, {"login.uid","User ID:"}, {"login.pwd","Password:"}, {"login.login","Login"}, {"login.english","English"}, {"login.spanish","Spanish"}, {"login.chinese","Chinese"} }; }
Listing 3: Spanish Resource Bundle
package com.heaton.informit; import java.util.*; public class I18NBundle_es extends ListResourceBundle { public Object[][] getContents() { return contents; } static final Object[][] contents = { {"login.pleaselogin","Por favor Conexión"}, {"login.uid","Identificación del usuario:"}, {"login.pwd","Contraseña:"}, {"login.login","Conexión"}, {"login.english","English"}, {"login.spanish","Español"}, {"login.chinese","Chino"} }; }
< Back
Page 4 of 4