tdawson 01/10/14 15:05:22
Modified: i18n/src/org/apache/taglibs/i18n LocaleTag.java
Log:
added changeResponseLocale attribute, defaulted to true
Revision Changes Path
1.3 +143 -102 jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/LocaleTag.java
Index: LocaleTag.java
===================================================================
RCS file: /home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/LocaleTag.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- LocaleTag.java 2001/06/05 08:35:42 1.2
+++ LocaleTag.java 2001/10/14 22:05:22 1.3
@@ -1,13 +1,13 @@
/*
- * $Header:
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/LocaleTag.java,v 1.2
2001/06/05 08:35:42 jstrachan Exp $
- * $Revision: 1.2 $
- * $Date: 2001/06/05 08:35:42 $
+ * $Header:
/home/cvs/jakarta-taglibs/i18n/src/org/apache/taglibs/i18n/LocaleTag.java,v 1.3
2001/10/14 22:05:22 tdawson Exp $
+ * $Revision: 1.3 $
+ * $Date: 2001/10/14 22:05:22 $
*
* ====================================================================
- *
+ *
* The Apache Software License, Version 1.1
*
- * Copyright (c) 1999 The Apache Software Foundation. All rights
+ * Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -15,7 +15,7 @@
* are met:
*
* 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
+ * notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
@@ -23,15 +23,15 @@
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
- * any, must include the following acknowlegement:
- * "This product includes software developed by the
+ * any, must include the following acknowlegement:
+ * "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
- * from this software without prior written permission. For written
+ * from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
@@ -57,7 +57,7 @@
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
- */
+ */
package org.apache.taglibs.i18n;
@@ -68,120 +68,161 @@
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
+
-/** This tag defines a {@link Locale} context for use by other inner JSP tags.
- * If no has been configured directly via the "locale" property then the
+/** This tag defines a
+ {@link java.util.Locale}
+ context for use by other inner JSP tags.
+ * If no has been configured directly via the "locale" property then the
* language, country and optional varient properties are used to
- * create a new Locale instance.
- * If these properties are not specified then the Locale is taken from
- * {@link ServletRequest} is used.
- * If still no {@link Locale} could be found then the default JVM
- * {@link Locale} is used.
+ * create a new Locale instance.
+ * If these properties are not specified then the Locale is taken from
+ * {@link javax.servlet.ServletRequest}
+ is used.
+ * If still no
+ {@link java.util.Locale}
+ could be found then the default JVM
+ * {@link java.util.Locale}
+ is used.
*
* @author James Strachan
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
-public class LocaleTag extends TagSupport {
-
- /** Holds value of property locale. */
- private Locale locale;
- /** Holds value of property language. */
- private String language;
- /** Holds value of property country. */
- private String country;
- /** Holds value of property variant. */
- private String variant;
-
- //-------------------------------------------------------------------------
- public LocaleTag() {
+public class LocaleTag extends TagSupport
+ {
+ /** Holds value of property locale. */
+ private Locale locale;
+ /** Holds value of property language. */
+ private String language;
+ /** Holds value of property country. */
+ private String country;
+ /** Holds value of property variant. */
+ private String variant;
+ /** specifies whether or not the response locale should be changed to match
+ * the locale used by this tag */
+ private boolean changeResponseLocale = true;
+
+
+ //-------------------------------------------------------------------------
+ public LocaleTag()
+ {
}
-
- // Tag interface
- //-------------------------------------------------------------------------
- public int doStartTag() throws JspException {
- return EVAL_BODY_INCLUDE;
- }
+
+ // Tag interface
+ //-------------------------------------------------------------------------
+ public int doStartTag() throws JspException
+ {
+ // set the bundle as a variable in the page
+ if ( this.getId() != null )
+ {
+ pageContext.setAttribute(this.getId(),this.getLocale());
+ }
- public void release() {
- super.release();
- locale = null;
- language = null;
- country = null;
- variant = null;
+ return EVAL_BODY_INCLUDE;
}
-
- // Properties
- //-------------------------------------------------------------------------
- public Locale getLocale() {
- if ( locale == null ) {
- locale = createLocale();
+
+ /**
+ * Sets the response locale if the changeResponseLocale attribute was set
+ * to true, OR if changeResponseLocale was unset and the tag was empty
+ */
+ public int doEndTag()
+ throws JspException
+ {
+ if (this.changeResponseLocale)
+ {
+ // set the locale for the response
+ pageContext.getResponse().setLocale(this.getLocale());
}
- return locale;
+
+ return EVAL_PAGE;
}
-
- public void setLocale( Locale locale ) {
- this.locale = locale;
+
+ public void setChangeResponseLocale(boolean value)
+ {
+ this.changeResponseLocale = value;
+ }
+
+ public void release()
+ {
+ super.release();
+ locale = null;
+ language = null;
+ country = null;
+ variant = null;
+ changeResponseLocale = true;
+ }
+
+ // Properties
+ //-------------------------------------------------------------------------
+ public Locale getLocale()
+ {
+ if (locale == null)
+ {
+ locale = createLocale();
+ }
+ return locale;
}
- public String getLanguage() {
- return language;
+ public void setLocale(Locale locale)
+ {
+ this.locale = locale;
}
-
- public void setLanguage( String language ) {
- this.language = language;
+
+ public String getLanguage()
+ {
+ return language;
}
- public String getCountry() {
- return country;
+ public void setLanguage(String language)
+ {
+ this.language = language;
}
-
- public void setCountry( String country ) {
- this.country = country;
+
+ public String getCountry()
+ {
+ return country;
}
- public String getVariant() {
- return variant;
+ public void setCountry(String country)
+ {
+ this.country = country;
}
-
- public void setVariant( String variant ) {
- this.variant = variant;
+
+ public String getVariant()
+ {
+ return variant;
}
- // Implementation methods
- //-------------------------------------------------------------------------
- protected Locale createLocale() {
- // let's try use the language & country properties first...
- if ( language != null && country != null ) {
- return (variant != null)
- ? new Locale( language, country, variant )
- : new Locale( language, country );
- }
- // otherwise lets use the ServletRequest
- return getLocale( pageContext );
+ public void setVariant(String variant)
+ {
+ this.variant = variant;
}
-
- /** Extracts the {@link Locale} from the given {@link ServletRequest}
- * or returns the default JVM's {@link Locale} if it could not be found
- */
- protected static Locale getLocale( PageContext pageContext ) {
- Locale answer = pageContext.getResponse().getLocale();
- if ( answer == null ) {
- ServletRequest request = pageContext.getRequest();
- answer = request.getLocale();
- if ( answer == null ) {
- for ( Enumeration enum = request.getLocales();
enum.hasMoreElements(); ) {
- answer = (Locale) enum.nextElement();
- if ( answer != null ) {
- break;
- }
- }
- if ( answer == null ) {
- // XXXX: may wish to log warning?
- answer = Locale.getDefault();
- }
- }
+
+ // Implementation methods
+ //-------------------------------------------------------------------------
+ protected Locale createLocale()
+ {
+ // let's try use the language & country properties first...
+ Locale locale = null;
+ if (language == null)
+ {
+ locale = pageContext.getResponse().getLocale();
+ }
+ else if (country == null)
+ {
+ locale = new Locale(language,"");
+ }
+ else if (variant == null)
+ {
+ locale = new Locale(language, country);
}
- return answer;
+ else
+ {
+ locale = new Locale(language, country, variant);
+ }
+
+ return locale;
}
-}
+
+ }