[Opensource] i18n of DefaultAutoElement and date formatting

addison at inter-locale.com addison at inter-locale.com
Fri May 2 02:27:41 PDT 2003


May I suggest that you use the java.text.DateFormatSymbols class to obtain
these rather than having to localize the values yourself? Java already has
appropriate letter values stored for each locale.

Best Regards,

Addison

===============================================================
Addison P. Phillips                     Globalization Architect
webMethods, Inc                       http://www.webmethods.com
Sunnyvale, CA, USA              mailto:aphillips at webmethods.com

+1 408.210.3569 (mobile)                  +1 408.962.5487 (ofc)
===============================================================
"Internationalization is not a feature. It is an architecture."

On Wed, 30 Apr 2003, D Lloyd wrote:

> I would like all of the end-user GUI to be internationalized, including
> the custom user registration.  
> So I modified DefaultAutoElement to try and find a reasonable entry in the
> message bundles.  
> The prior implementation did not search the bundle for the schema where
> the DBObject actually resided.  
> I had to use setSchema in my DBObject, otherwise it works out-of-the-box. 
> Without this, it works as previously.
> I noticed the comment in the DefaultAutoElement where Mike R. would have
> liked to show the Date format based on Locale.
> There are times where I would also like my GUI to show the date entry
> format, so I created the function getDateFormatString in DateTime.java
> and modified DefaultAutoElement to use it when the field is not read-only.
> 
> I had a lot of problems locating how to localize the resulting pattern.  I
> suppose that is where Mike R. got hung up.
> So I resorted to using the message bundles to tell me what the
> abbreviation should be for a language.  
> The pattern for the US might be M/d/yy so the result is mm/dd/yyyy and for
> German it could be dd-MM-yyyy = Tag-Monat-Jahr.
> I have no idea what the internationalized strings for the date formats
> are, so if someone could fill in the blanks I would appreciate it.
> 
> I tried to cache the DateFormatCacheItem entries using the CacheManager
> and had no luck.  I gave up last night when it got too late.  
> 
> 
> 
> The new MessagesBundle.properties entries are:
> 
> datetime.format.MM = mm
> datetime.format.dd = dd
> datetime.format.yy = yyyy
> datetime.format.hh = hh
> datetime.format.HH = hh
> datetime.format.kk = kk
> datetime.format.KK = kk
> datetime.format.mm = mm
> datetime.format.ss = ss
> 
> 
> 
> 
> The diffs are:
> 
> 
> 
> DateTime.java - just new functions with supporting imports and a map
> 
> 
> import com.jcorporate.expresso.core.cache.Cache;
> import com.jcorporate.expresso.core.cache.Cacheable;
> import com.jcorporate.expresso.core.cache.CacheException;
> import com.jcorporate.expresso.core.cache.CacheManager;
> import com.jcorporate.expresso.core.i18n.Messages;
> 
> import java.text.DateFormat;
> 
>     /** map of locale to date format strings */
>     transient private static Hashtable sDateFormatStrings = null;
> 
>        /**
>         * Get the Date or DateTime format for a Locale.
>         * @param  dateOnly True if this is to get a date only format
> (m/d/yy) versus DateTime (m/d/yy h:m:s)
>         * @param  locale   The locale to get the format for. This may not
> be null
>         *
>         * @return String formatted for the date
>         * @author David Lloyd
>         */
>        public static String getDateFormatString(boolean dateOnly, Locale
> locale) {
>                DateTime.DateFormatCacheItem item = null;
>                String pattern = "";
>                String datePattern = "";
>                String dateTimePattern = "";
> 
>                if (sDateFormatStrings == null)
>                        sDateFormatStrings = new Hashtable();
>                item = (DateTime.DateFormatCacheItem)
> sDateFormatStrings.get(DateFormatCacheItem.deriveKey(locale));
> 
>                if (item == null) {
>                        DateFormat dateFormat;
>                        DateFormat dateTimeFormat;
>                        dateFormat =
> DateFormat.getDateInstance(DateFormat.SHORT, locale);
>                        dateTimeFormat =
> DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT,
> locale);
> 
>                        if (dateFormat instanceof SimpleDateFormat) {
>                                SimpleDateFormat simpleDateFormat =
> (SimpleDateFormat) dateFormat;
>                                datePattern =
> convertDateFormatStringToLocalized(locale, simpleDateFormat.toPattern());
>                        }
>                        if (dateTimeFormat instanceof SimpleDateFormat) {
>                                SimpleDateFormat simpleDateFormat =
> (SimpleDateFormat) dateTimeFormat;
>                                dateTimePattern =
> convertDateFormatStringToLocalized(locale, simpleDateFormat.toPattern());
>                        }
> 
>                        item = new DateFormatCacheItem(locale, datePattern,
> dateTimePattern);
>                        sDateFormatStrings.put(item.key, item);
>                } else {
>                        datePattern = item.dateFormat;
>                        dateTimePattern = item.dateTimeFormat;
>                        //System.err.println("DateTime.getDateFormatString
> cache hit");
>                }
>                if (dateOnly)
>                        pattern = datePattern;
>                else
>                        pattern = dateTimePattern;
> 
>                return pattern;
>        }
> 
>        static String convertDateFormatStringToLocalized(Locale locale,
> String p) {
>                try {
>                        FastStringBuffer fsb = new FastStringBuffer(16);
>                        int n = p.length();
>                        for (int i=0; i < n; i++) {
>                                char c = p.charAt(i);
>                                switch (c) {
>                                        case 'M':
>                                        case 'd':
>                                        case 'y':
>                                        case 'h': case 'H': case 'k': case
> 'K':
>                                        case 'm':
>                                        case 's':
>                                        {
>                                                String s;
>                                                try {
>                                                        s =
> Messages.getString(locale, "datetime.format."+c+c);
>                                                        fsb.append(s);
>                                                } catch (Exception e) {
>                                                       
> fsb.append(c).append(c);
>                                                }
>                                                while (i < n-1 && c ==
> p.charAt(i+1))
>                                                        i++;
>                                        }
>                                        break;
>                                        default:
>                                                fsb.append(c);
>                                                break;
>                                }
>                        }
>                        return fsb.toString();
>                } catch (Exception e) {
>                        // FIXME: log this
>                }
>                return "";
>        }
> 
>        static class DateFormatCacheItem implements Cacheable {
>                public String key;
>                public String dateFormat;
>                public String dateTimeFormat;
> 
>                DateFormatCacheItem(Locale locale, String dateFormat,
> String dateTimeFormat) {
>                        key = deriveKey(locale);
>                        this.dateFormat = dateFormat;
>                        this.dateTimeFormat = dateTimeFormat;
>                }
>                public String getKey() {
>                        return key;
>                }
>                static String deriveKey(Locale locale) {
>                        return
> locale.getCountry()+"-"+locale.getLanguage();
>                }
>        }
> 
> 
> 
> ===========================================
> DefaultAutoElement.java
> 
> 
> D:\Dev\expresso>diff
> expresso\expresso-web\web-inf\src\com\jcorporate\expresso\services\controller\ui\DefaultAutoElement.java
> DefaultAutoElement.java
> 105a106
> >       static final String expressoSchema =
> com.jcorporate.expresso.core.ExpressoSchema.class.getName();
> 328,341d328
> <             //xun li add for time display
> <             //oneField.setLabel(dbobj.getDescription(fieldName));
> <             //xun li add for time display
> < //
> < //  I've commented this out because we now try to parse given the
> current user's
> < // Locale and I can't figure out how to get a descriptive format string
> for
> < // the locale (MR)
> < //
> < //            if (dbobj.getFieldMetaData(fieldName).isDateType()) {
> < //                oneField.setLabel(dbobj.getDescription(fieldName) +
> < //                                  " (YYYY-MM-DD)");
> < //            } else {
> <                 oneField.setLabel(dbobj.getDescription(fieldName));
> < //            } /* xnu li*/
> 342a330,332
> >                       // Get the localized description, including date
> pattern
> >                       {
> >                               String description;
> 343a334,380
> >                               // We could also convert request to
> HttpServletRequest and get locale from user pref.
> >                               // I do not think it would be a problem,
> but I'm not sure this isnot used in a
> >                               // non-web environment.
> >                               Locale locale = response.getLocale();
> >
> >                               Object[] args = {};
> >                               String stringCode =
> dbobj.getDescription(fieldName);
> >
> >                               try {
> >                                       description =
> com.jcorporate.expresso.core.i18n.Messages.getString(
> >                                                                      
> dbobj.getSchema(),
> >                                                                      
> locale,
> >                                                                      
> stringCode,
> >                                                                      
> args);
> >                               } catch (IllegalArgumentException iae) {
> >                                       if (log.isDebugEnabled())
> >                                              
> log.debug("DefaultAutoElement: Key ["+stringCode+"] not found in
> "+dbobj.getSchema());
> >                                       description = null;
> >
> >                                       if ( !
> dbobj.getSchema().equals(expressoSchema)) {
> >                                               try {
> >                                                       description =
> com.jcorporate.expresso.core.i18n.Messages.getString(
> >                                                                         
>   expressoSchema,
> >                                                                         
>   locale,
> >                                                                         
>    stringCode,
> >                                                                         
>    args);
> >                                               } catch
> (IllegalArgumentException e) {
> >                                                       description =
> null;
> >                                               }
> >                                       }
> >                               }
> >
> >                               if (description == null ||
> description.length() == 0)
> >                                       description = stringCode;
> >
> >                               if (metaData.isDateType() &&
> !metaData.isReadOnly()) {
> >                                       // try to show 'A Date
> (MM/dd/yyyy)'
> >                                       String pattern =
> DateTime.getDateFormatString(metaData.isDateOnlyType(), locale);
> >                                       if (pattern.length() > 0)
> >                                               pattern = "
> ("+pattern+")";
> >                                       oneField.setLabel(description +
> pattern);
> >                               } else {
> >                                       oneField.setLabel(description);
> >                               }
> >                       }
> >
> >
> 
> 
> 
> I have also attached the modified files.
> 
> Thanks,
> 
> David Lloyd
> 
> 
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Search - Faster. Easier. Bingo.
> http://search.yahoo.com




More information about the Opensource mailing list