1 16 17 package org.apache.taglibs.i18n; 18 19 import java.util.Enumeration ; 20 import java.util.Locale ; 21 22 import javax.servlet.ServletRequest ; 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.PageContext ; 25 import javax.servlet.jsp.tagext.TagSupport ; 26 27 28 42 public class LocaleTag extends TagSupport 43 { 44 45 private Locale locale; 46 47 private String localeRef; 48 49 private String language; 50 51 private String country; 52 53 private String variant; 54 56 private boolean changeResponseLocale = true; 57 58 59 public int doStartTag() throws JspException 62 { 63 if ( this.getId() != null ) { 65 pageContext.setAttribute(getId(),getLocale()); 66 } 67 68 return EVAL_BODY_INCLUDE; 69 } 70 71 75 public int doEndTag() throws JspException 76 { 77 if (this.changeResponseLocale) { 78 pageContext.getResponse().setLocale(getLocale()); 80 } 81 82 return EVAL_PAGE; 83 } 84 85 public void setChangeResponseLocale(boolean value) 86 { 87 changeResponseLocale = value; 88 } 89 90 public void release() 91 { 92 super.release(); 93 locale = null; 94 language = null; 95 country = null; 96 variant = null; 97 changeResponseLocale = true; 98 } 99 100 protected final Locale getLocale() 103 { 104 if (localeRef != null) { 105 locale = (Locale )pageContext.findAttribute(localeRef); 106 } 107 if (locale == null) { 108 locale = createLocale(); 109 } 110 return locale; 111 } 112 113 public final void setLocale(Locale locale) 114 { 115 this.locale = locale; 116 } 117 118 121 public final void setLocaleRef(String value) 122 { 123 this.localeRef = value; 124 } 125 126 public final void setLanguage(String language) 127 { 128 this.language = language; 129 } 130 131 public final void setCountry(String country) 132 { 133 this.country = country; 134 } 135 136 public final void setVariant(String variant) 137 { 138 this.variant = variant; 139 } 140 141 private Locale createLocale() 144 { 145 Locale locale = null; 147 if (language == null) { 148 locale = pageContext.getResponse().getLocale(); 149 } else if (country == null) { 150 locale = new Locale (language,""); 151 } else if (variant == null) { 152 locale = new Locale (language, country); 153 } else { 154 locale = new Locale (language, country, variant); 155 } 156 157 return locale; 158 } 159 160 } 161 | Popular Tags |