wiki:waue/2010/0805

Version 10 (modified by waue, 14 years ago) (diff)

--

JSP 支援多國語系
Tomcat + JSP + i18n + taglib

環境

  • sun java jdk 6
  • tomcat 6
  • 參數說明
$tomcat 代表你放tomcat的路徑。如 /opt/tomcat/
$pj 代表你的project 名稱。如:i18n
$worspace 代表你Eclipse該專案所放置的路徑。如:/home/myname/workspace/
  • 以下提供兩種實做的方法,第一種是直接放到 tomcat 下的目錄下;第二種方法用Eclipse 輔助編輯與即時編譯

說明

開發jps上的i18n,基本上要準備的有 (Tomcat + JSP + i18n + taglib)

並且將內容分成三大部份,1. i18n函式庫設定 2. 多國語系參數properties檔 3. JSP 程式碼

1. i18n函式庫設定

首先要把 i18n 的taglibs-i18n.jar 包找到,放到我們要引用的tomcat 專案目錄中,用 taglibs-i18n.tld 描述這個jar ,而web.xml 則要告訴tomcat 要用到的 taglibs-i18n.tld 與 http://jakarta.apache.org/taglibs/i18n-1.0 建立連結。

如此,則i18n與taglib 的環境則準備好了

web.xml

Tomcat $tomcat/webapps/$pj/WEB-INF/web.xml
Eclipse $worspace/$pj/WebContent/WEB-INF/web.xml
<?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>

taglibs-i18n.tld

Tomcat $tomcat/webapps/$pj/WEB-INF/taglibs-i18n.tld
Eclipse $worspace/$pj/WebContent/WEB-INF/taglibs-i18n.tld

(可下載附件)

taglibs-i18n.jar

Tomcat $tomcat/webapps/$pj/WEB-INF/lib/taglibs-i18n.jar
Eclipse $worspace/$pj/WebContent/WEB-INF/lib/taglibs-i18n.jar

(可下載附件)

2. 多國語系參數properties檔

多國語系目前較好的用法是,我們設計一個 jps 檔,當他out.print 的時候是用參數的方法印出,而參數的語系則交由使用者或程式來決定

因此,同一個參數 test1 ,我們需要 i18n_en.properties 來定義英文版本;i18n_zh_TW.properties 來定義中文版本;...依此類推

這個範例中,為了專案程式碼目錄的整潔度而言,將properties 檔放到 org/mytest/ 下,若看官想放其他目錄,需注意與jsp檔 <i18n:bundle baseName="org.mytest.i18n" > 程式碼的搭配。

i18n_en.properties

Tomcat $tomcat/webapps/$pj/WEB-INF/classes/org/mytest/i18n_en.properties
Eclipse $worspace/$pj/src/org/mytest/i18n_en.properties
test1 = Hello world
test2 = Now you can let jsp i18n using i18n taglib!

i18n_zh_TW.properties

Tomcat $tomcat/webapps/$pj/WEB-INF/classes/org/mytest/i18n_zh_TW.properties
Eclipse $worspace/$pj/src/org/mytest/i18n_zh_TW.properties
test1 = &#x4F60;&#x597D;&#xFF01;&#x4E16;&#x754C;
test2 = &#x4F60;&#x73FE;&#x5728;&#x5DF2;&#x7D93;&#x80FD;&#x8B93;&#x4F60;&#x7684;JSP&#x4F7F;&#x7528;I18N&#x7684;taglib&#x8DDF;&#x4E16;&#x754C;&#x63A5;&#x8ECC;&#x56C9;&#xFF01;

3. JSP 程式碼

index.jsp

Tomcat $tomcat/webapps/$pj/index.jsp
Eclipse $worspace/$pj/WebContent/index.jsp
<%@ 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" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 

<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>

<%
locale = new Locale("en",""); 
%>
<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>

結果

http://localhost:8080/$pj/index.jsp

Attachments (2)

Download all attachments as: .zip