JSP 支援多國語系
Tomcat + JSP + i18n + taglib
環境
內容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>i18n</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>http://jakarta.apache.org/taglibs/i18n-1.0</taglib-uri>
<taglib-location>/WEB-INF/taglibs-i18n.tld</taglib-location>
</taglib>
</web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
import="java.io.*,java.text.*,java.util.*,javax.servlet.jsp.*" %>
<%@ taglib uri="http://jakarta.apache.org/taglibs/i18n-1.0" prefix="i18n" %>
<HTML>
<HEAD>
<TITLE>Examples of I18N Custom Tag Library
Tag Usage</TITLE>
</HEAD>
<BODY>
This page displays all key/value pairs in the bundle.
locale = <b><%= pageContext.getResponse().getLocale() %></b><br>
charset = <b><%= pageContext.getResponse().getCharacterEncoding() %></b><br>
<H2>WebApp translations</H2>
<%
Locale locale = new Locale("zh","");
%>
<i18n:bundle baseName="org.mytest.i18n" locale="<%=locale%>" id="bundle"/>
<TABLE>
<TR> <TD>1</TD><TD> <i18n:message key="test1"/></TD> </TR>
<TR> <TD>2</TD><TD> <i18n:message key="test2"/></TD> </TR>
</TABLE>
</BODY>
</HTML>
(可下載附件)
(可下載附件)
Eclipse | /Java Resource:src /org.mytest/
|
test1 = Hello world
test2 = Now you can let jsp i18n using i18n taglib!
Eclipse | /Java Resource:src /org.mytest/
|
test1 = 你好!世界
test2 = 你現在已經能讓你的JSP使用I18N的taglib跟世界接軌囉!