Sign In/My Account | View Cart  

advertisement

AddThis Social Bookmark Button

Print

Designing Internationalized User Interface Components for Web Applications: Examples

Designing Internationalized User Interface Components for Web Applications:
Examples

[ Back to Article ]

Example 1-1. taglib.tld for the JSP custom tags in this article

<?xml version="1.0"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 
1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<taglib>
  <tlibversion>1.0</tlibversion>
  <shortname>oreillytags</shortname>
  <jspversion>1.1</jspversion>

  <tag>
    <name>InsertText</name>
    <tagclass>com.ora.i18n.jsptags.InsertText</tagclass>
    <bodycontent>JSP</bodycontent>
    <attribute>
      <name>textID</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>resourceBundleName</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>locale</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <name>OptionList</name>
    <tagclass>com.ora.i18n.jsptags.OptionList</tagclass>
    <bodycontent>JSP</bodycontent>
    <attribute>
      <name>optionList</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>locale</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

  <tag>
    <name>InsertDate</name>
    <tagclass>com.ora.i18n.jsptags.InsertDate</tagclass>
    <bodycontent>JSP</bodycontent>
    <attribute>
      <name>date</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>datePattern</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>style</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>locale</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
</tag>

<tag>
    <name>InsertImage</name>
    <tagclass>com.ora.i18n.jsptags.InsertImage</tagclass>
    <bodycontent>JSP</bodycontent>
    <attribute>
      <name>image</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>imagePath</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>imageWidth</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>imageHeight</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>locale</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

</taglib>

[ Back to Article ]


Example 1-3: InsertText JSP tag

package com.ora.i18n.jsptags;

import java.awt.ComponentOrientation;
import java.io.IOException;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

public class InsertText extends TagSupport {
    private Locale locale;
    private String textID;
    private String resourceBundleName;

    private final static String DIV_LTR = "<DIV DIR=\"LTR\">";
    private final static String DIV_RTL = "<DIV DIR=\"RTL\">";
    private final static String CLOSING_DIV = "</DIV>";

    public InsertText() {
        locale = Locale.getDefault();
        textID = null;
        resourceBundleName = null;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public void setTextID(String textID) {
        this.textID = textID;
    }

    public void setResourceBundleName(String resourceBundleName) {
        this.resourceBundleName = resourceBundleName;
    }

    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();

        ResourceBundle rb = ResourceBundle.getBundle(resourceBundleName, 
locale);
        try {
            ComponentOrientation co = 
ComponentOrientation.getOrientation(locale);
            if (co.isLeftToRight() || 
co.equals(ComponentOrientation.UNKNOWN)) {
                out.print(this.DIV_LTR);
            }
            else {
                out.print(this.DIV_RTL);
            }
            out.print(rb.getString(textID));
            out.print(this.CLOSING_DIV);
        }
        catch (IOException ioe) {
            throw new JspTagException(ioe.toString());
        }
        catch (MissingResourceException mre) {
            throw new JspTagException(mre.toString());
        }
        return SKIP_BODY;
    }

    public void release() {
        super.release();
        locale = Locale.getDefault();
        textID = null;
        resourceBundleName = null;
    }
}

[ Back to Article ]


Example 1-9. JSP page to demonstrate use of InsertDate tag

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/oreilly-taglib" prefix="oreillytags"%>
<HTML>
<HEAD>
<TITLE>
InsertDate Tag Demonstration
</TITLE>
</HEAD>
<BODY>

<%
    java.util.Date currentDate = new java.util.Date();
%>

Today's date (English, short format): <oreillytags:InsertDate date='<%= 
currentDate %>'
        locale='<%= new java.util.Locale("en", "US") %>' style='<%= 
java.text.DateFormat.SHORT %>'/>
<BR>
Today's date (English, long format): <oreillytags:InsertDate date='<%= 
currentDate %>'
        locale='<%= new java.util.Locale("en", "US") %>' style='<%= 
java.text.DateFormat.LONG %>'/>
<BR>
Today's date (German, short format): <oreillytags:InsertDate date='<%= 
currentDate %>'
        locale='<%= new java.util.Locale("de", "DE") %>' style='<%= 
java.text.DateFormat.SHORT %>'/>
<BR>
Today's date (German, long format): <oreillytags:InsertDate date='<%= 
currentDate %>'
        locale='<%= new java.util.Locale("de", "DE") %>' style='<%= 
java.text.DateFormat.LONG %>'/>
<BR>
Today's date (English, EEEE, MMMM d yyyy pattern): <oreillytags:InsertDate 
date='<%= currentDate %>'
        locale='<%= new java.util.Locale("en", "US") %>' datePattern='EEEE, 
MMMM d yyyy'/>
<BR>
Today's date (German, EEEE, MMMM d yyyy pattern): <oreillytags:InsertDate 
date='<%= currentDate %>'
        locale='<%= new java.util.Locale("de", "DE") %>' datePattern='EEEE, 
MMMM d yyyy'/>
<BR>

</BODY>
</HTML>

[ Back to Article ]


Example 1-10. OptionList JSP tag

package com.ora.i18n.jsptags;

import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

public class OptionList extends TagSupport {

    private Locale locale;
    private ArrayList optionList;

    public OptionList() {
        locale = Locale.getDefault();
        optionList = null;
    }

    public void setLocale(Locale locale) {
        this.locale = locale;
    }

    public void setOptionList(ArrayList optionList) {
        this.optionList = optionList;
    }

    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();

        try {
            Collator collator = Collator.getInstance(locale);
            Collections.sort(optionList, collator);
            Iterator sortedList = optionList.iterator();
            while (sortedList.hasNext()) {
                String option = (String) sortedList.next();
                out.print("<OPTION value=\"");
                out.print(option);
                out.print("\">");
                out.print(option);
                out.println("</OPTION>");
            }
        }
        catch (IOException ioe) {
            throw new JspTagException(ioe.toString());
        }
        return SKIP_BODY;
    }

    public void release() {
        super.release();
        locale = Locale.getDefault();
        optionList = null;
    }
} 

[ Back to Article ]


Example 1-11. JSP page to demonstrate use of OptionList JSP tag

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/oreilly-taglib" prefix="oreillytags"%>
<HTML>
<HEAD>
<TITLE>
OptionList Tag Demonstration
</TITLE>
</HEAD>
<BODY>

<%
    java.util.ArrayList fruits = new java.util.ArrayList();
    fruits.add("\u00e4pple");
    fruits.add("banan");
    fruits.add("p\u00e4ron");
    fruits.add("orange");
%>

<FORM name="OptionListTestForm">
Fruits list (Swedish):

    <SELECT name="SwedishSortedFruits">
        <oreillytags:OptionList optionList='<%= fruits %>' locale='<%= new 
java.util.Locale("sv", "")%>'/>
    </SELECT>

<P>

Fruits list (German):

    <SELECT name="GermanSortedFruits">
        <oreillytags:OptionList optionList='<%= fruits %>' locale='<%= new 
java.util.Locale("de", "")%>'/>
    </SELECT>
</FORM>

</BODY>
</HTML>

[ Back to Article ]


Example 1-13. JSP page to demonstrate use of InsertImage JSP tag

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/oreilly-taglib" prefix="oreillytags"%>
<HTML>
<HEAD>
<TITLE>
InsertImage Tag Demonstration
</TITLE>
</HEAD>
<BODY>

O'Reilly Header (English):<P>
<oreillytags:InsertImage image='OReillyHeader.gif' locale='<%= new 
java.util.Locale("en", "US") %>'
    imagePath='/oreilly/images' imageWidth='700' imageHeight='75'/>

<P>

O'Reilly Header (German):<P>
<oreillytags:InsertImage image='OReillyHeader.gif' locale='<%= new 
java.util.Locale("de", "DE") %>'
    imagePath='/oreilly/images' imageWidth='464' imageHeight='80'/>
</BODY>
</HTML>

[ Back to Article ]