The Tag handler implementing custom JSP actions.

Enterprise Java
Internationalize Your Web Site

Gary Bollinger and Bharathi Natarajan
Listing 2. The Tag handler implementing custom JSP actions.


package i18ntest;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.io.*;

public class TextTag
  extends BodyTagSupport {

  public int doAfterBody()
    throws JspException {
    try {
      JspWriter w = bodyContent.getEnclosingWriter();
      String s = bodyContent.getString().trim();
      String ls = localize(s);
      w.println(ls);
      return SKIP_BODY;
    }
    catch(IOException ex) {
      throw
       new JspTagException(ex.toString());
    }
  }

  public String localize(String key) {
    ResourceBundle msgs = ResourceBundle.getBundle(
                                  "i18n.i18nMessages", i18nLocale.getLocale());
    String val = msgs.getString(key);
    return (val == null) ? key : val;
  }
}