You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Oleg V Alexeev <go...@penza.net> on 2000/10/27 09:23:10 UTC

implementing i18n in

Hello struts-dev,

  I make some additions to bean:write tag. It can be useful to display
  date-time and number values with currently selected locale.

>---------------------------------------------------

    public static final String TIMESTAMP_FORMAT_KEY =
      "org.apache.struts.taglib.bean.format.timestamp";
    public static final String DATE_FORMAT_KEY = 
      "org.apache.struts.taglib.bean.format.date";
    public static final String TIME_FORMAT_KEY = 
      "org.apache.struts.taglib.bean.format.time";
    public static final String INT_FORMAT_KEY = 
      "org.apache.struts.taglib.bean.format.int";
    public static final String REAL_FORMAT_KEY = 
      "org.apache.struts.taglib.bean.format.real";

    private String formatStr = null;
    
    public String getFormat() {
     return formatStr;
    }

    public void setFormat( String formatStr ) {
     this.formatStr = formatStr;
    }

      public int doStartTag() throws JspException {

      ... some code skipped
      
            // Convert the property value to a String if necessary
            // FIXME - deal with array valued properties
            // FIXME - use a PropertyEditor as necessary
            
            MessageResources  formats = (MessageResources)
             pageContext.getAttribute(
                Action.MESSAGES_KEY,
                PageContext.APPLICATION_SCOPE);

            Locale locale = getLocale();
            Format format = null;

            if (  value instanceof java.sql.Timestamp ) {
             if( formatStr==null )
              formatStr = messages.getMessage(locale, TIMESTAMP_FORMAT_KEY );
             if( formatStr==null )
              format = DateFormat.getDateTimeInstance(
                     DateFormat.SHORT, DateFormat.SHORT, locale);
             else
              format = new SimpleDateFormat( formatStr, locale );
            } else if (  value instanceof java.sql.Date ) {
             if( formatStr==null )
              formatStr = messages.getMessage(locale, DATE_FORMAT_KEY );
             if( formatStr==null )
              format = DateFormat.getDateInstance(DateFormat.SHORT, locale);
             else
              format = new SimpleDateFormat( formatStr, locale );
            } else if (  value instanceof java.sql.Timestamp ) {
             if( formatStr==null )
              formatStr = messages.getMessage(locale, TIME_FORMAT_KEY );
             if( formatStr==null )
              format = DateFormat.getTimeInstance(DateFormat.SHORT, locale);
             else
              format = new SimpleDateFormat( formatStr, locale );
            } else if ( value instanceof Number ) {
             if( formatStr==null ) {
              if( ( value instanceof Byte )    ||
                  ( value instanceof Short )   ||
                  ( value instanceof Integer ) ||
                  ( value instanceof Long )    ||
                  ( value instanceof BigInteger ) )
               formatStr = messages.getMessage(locale, INT_FORMAT_KEY );
              else
              if( ( value instanceof Float ) ||
                  ( value instanceof Double ) ||
                  ( value instanceof BigDecimal ) )
               formatStr = messages.getMessage(locale, REAL_FORMAT_KEY );
             }
             if( formatStr==null )
              format = NumberFormat.getInstance( locale );
             else
              try {
               format = new DecimalFormat( formatStr );
              }
              catch( IllegalArgumentException e ) {
               throw new JspException( "Wrong format string - '" + format + "'" );
              }
            } else if (!(value instanceof String)) {
             value = value.toString();
            }

            if( format!=null ) {
             value = format.format( value );
            }
            
      ... some code skipped

    }
    
>-----------------------------------------------------


-- 
Best regards,
 Oleg                          mailto:gonza@penza.net