March 02, 2006

an unstealthy icu4j upgrade

IBM has announced a maintenance release for icu4j, version 3.4.3. among the goodies for this version are:
  • Olson 2006a time zone data (just in time to get ready for the new DST in the US)
  • corrects mistakes in the CLDR data found in icu4j 3.4.2
  • MessageFormat (like core java's but it can use icu4j's super cool ULocale class) upgraded to @stable"
  • fixed bugs in DateFormat, SimpleDateFormat, etc.
  • and a bit more trivial (to me) but should make some folks happy this release no longer tags "@draft" APIs with "@deprecated" by default--though why they ever did that in the first place is a bit of a mystery to me


the MessageFormat class is kind of cool in that it handles compound rb strings (which i'd rather have never learned about) such as: "At {1} on {2}, there was {3} on planet {4}". in the past, we normally handled this with in-house methods which are somewhat cumbersome in that we needed to do any date/numeric/currency formatting on the substituted values for the message's placeholders (the bits in between the {}) prior to formatting the message. now using the com.ibm.icu.text.MessageFormat you could do something like:

<cfscript>
   mfObject=createobject("java","com.ibm.icu.text.MessageFormat");
   args=arrayNew(1);
   args[1]=now();
   args[2]="the office";
   args[3]="microbrewery";
   // pass in the message string and substitution arguments    thisMsg=mfObject.format("On {0,date,full} at {0,time,full}, I left {1} for the {2}.",args);
   writeoutput(thisMsg);
</cfscript>


which would produce something like (in the en_US locale) "On Wednesday, March 1, 2006 at 8:44:22 PM GMT+07:00, I left the office for the microbrewery.".

to explain a bit more : {0,date,full} is a placeholder that takes the first element in the args array (java arrays start at 0) and applies localized date formatting with the "full" style. {0,time,full} ditto but uses time formatting and {1} and {2} are placeholders for simple strings.

however in order to make this more flexible (ie. use locales other than the server's default), you'll have to use a simple java wrapper class--the MessageFormat format method is overloaded and coldfusion can't easily use it's other "flavors" which require StringBuffer and FieldPosition classes.

0 Comments:

Post a Comment

<< Home