You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by vi...@staff.mcgill.ca on 2004/10/19 18:54:25 UTC

PROPOSAL LabelValueBean extension

Hi everybody,

The LabelValueBean is a good convenience class.

Currently, this class can NOT translate its label, so we cant use the message
tag in a JSP.
However, I think that its an important convenience issue.
Thus, I propose to you to have also a LabelKeyValueBean class which would allow
to translate the label.
You ll need a standard way to write property key.

For instance, in the message-resources Application.properties we have the key:
widget.test.text=TEST

In the JSP, if the object myLabelKeyValueBean contains the label=TEST, the
labelKey is widget.test.text; and we can use in a JSP:
<bean:message name="myLabelKeyValueBean" property="labelKey" />

Feel free to e-mail me with any questions or comments you have about my
proposal.

Vincent

==================================================
LabelKeyValueBean class
==================================================

public class LabelKeyValueBean extends org.apache.struts.util.LabelValueBean {
  public static final String KEY_SEPARATOR = ".";
  public static final String KEY_PREFIX = "widget";
  public static final String KEY_SUFFIX = "text";
  public String additionalPrefix_ = null;

  /**
   * @param label
   * @param value
   */
  public LabelKeyValueBean(String label, String value) {
    super(label, value);
  }

  public LabelKeyValueBean(){
    super();
  }

  /**
   * Only to get the label in an bundle key
   *
   * @see org.apache.struts.util.LabelValueBean#getLabel()
   */
  public String getLabelKey() {
    return LabelKeyValueBean.createLabelKey(super.getLabel(),
getAdditionalPrefix());
  }

  /**
   * @return
   */
  public String getAdditionalPrefix() {
    return additionalPrefix_;
  }

  /**
   * @param string
   */
  public void setAdditionalPrefix(String string) {
    additionalPrefix_ = string;
  }

  /**
   * Utility method to create the label key in a standard way
   *
   * @param label
   * @param additionalPrefix can be null
   * @return
   */
  static public String createLabelKey(String label, String additionalPrefix) {
    return KEY_PREFIX + KEY_SEPARATOR +
      ((additionalPrefix != null) ? (additionalPrefix + KEY_SEPARATOR) : "") +
      label.toLowerCase().replace(' ', '_') +
      KEY_SEPARATOR + KEY_SUFFIX;
  }

}

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Re: PROPOSAL LabelValueBean extension

Posted by Michael McGrady <mi...@michaelmcgrady.com>.
I did this a long time ago, and, I suspect, others must have too.  I 
assumed that the lack of this meant everyone had sort of done it already 
for themselves.  You might want to use what has been created already.

Michael McGrady

vincent.siveton@staff.mcgill.ca wrote:

>Hi everybody,
>
>The LabelValueBean is a good convenience class.
>
>Currently, this class can NOT translate its label, so we cant use the message
>tag in a JSP.
>However, I think that its an important convenience issue.
>Thus, I propose to you to have also a LabelKeyValueBean class which would allow
>to translate the label.
>You ll need a standard way to write property key.
>
>For instance, in the message-resources Application.properties we have the key:
>widget.test.text=TEST
>
>In the JSP, if the object myLabelKeyValueBean contains the label=TEST, the
>labelKey is widget.test.text; and we can use in a JSP:
><bean:message name="myLabelKeyValueBean" property="labelKey" />
>
>Feel free to e-mail me with any questions or comments you have about my
>proposal.
>
>Vincent
>
>==================================================
>LabelKeyValueBean class
>==================================================
>
>public class LabelKeyValueBean extends org.apache.struts.util.LabelValueBean {
>  public static final String KEY_SEPARATOR = ".";
>  public static final String KEY_PREFIX = "widget";
>  public static final String KEY_SUFFIX = "text";
>  public String additionalPrefix_ = null;
>
>  /**
>   * @param label
>   * @param value
>   */
>  public LabelKeyValueBean(String label, String value) {
>    super(label, value);
>  }
>
>  public LabelKeyValueBean(){
>    super();
>  }
>
>  /**
>   * Only to get the label in an bundle key
>   *
>   * @see org.apache.struts.util.LabelValueBean#getLabel()
>   */
>  public String getLabelKey() {
>    return LabelKeyValueBean.createLabelKey(super.getLabel(),
>getAdditionalPrefix());
>  }
>
>  /**
>   * @return
>   */
>  public String getAdditionalPrefix() {
>    return additionalPrefix_;
>  }
>
>  /**
>   * @param string
>   */
>  public void setAdditionalPrefix(String string) {
>    additionalPrefix_ = string;
>  }
>
>  /**
>   * Utility method to create the label key in a standard way
>   *
>   * @param label
>   * @param additionalPrefix can be null
>   * @return
>   */
>  static public String createLabelKey(String label, String additionalPrefix) {
>    return KEY_PREFIX + KEY_SEPARATOR +
>      ((additionalPrefix != null) ? (additionalPrefix + KEY_SEPARATOR) : "") +
>      label.toLowerCase().replace(' ', '_') +
>      KEY_SEPARATOR + KEY_SUFFIX;
>  }
>
>}
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
>For additional commands, e-mail: dev-help@struts.apache.org
>
>
>
>
>  
>



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org