December 08, 2003

java resource bundles

while i've been using UTF-8 based resource bundles for some time now, larger, more complex projects really need tools like IBM's rbManager to help manage resource bundle creation/translation. the problem with these are that their text messages are stored as ANSI escaped chars: Go=\u0E44\u0E1B (in thai, ไป). this requires quite a bit of extra cf processing to parse these types of "pure" java resource bundles (rb). i've been trying off and on for some months now to make use of the underlying java resourceBundle classes to handle rb files but haven't had much success (mainly because java expects rb files in class paths and thats not something i can live with on some projects nor could i find a simple workaround). while staring at some limestone rocks on saturday i had a micro epiphany about java.util.PropertyResourceBundle class. this class handles rb files from an input stream (ie you can pump in the rb file content from anyplace on the server). badda bing (i actually thought that at the time ;-) here's some test code i whipped up: <cfscript> thisDir= GetDirectoryFromPath(expandpath("*.*")); rbFile=thisDir & "test_th_TH.properties"; rb = createObject("java", "java.util.PropertyResourceBundle"); fis = CreateObject("java", "java.io.FileInputStream"); fis.init(rbFile); rb.init(fis); keys=rb.getKeys(); writeoutput("resourceBundle = #rbFile#
"); while (keys.hasMoreElements()) { thisKEY=keys.nextElement(); thisMSG=rb.handleGetObject(thisKey); writeoutput("#thisKEY# = #thisMSG#
"); } <cfscript> as you can see its quite simple, so simple i built it into a javaRB.cfc. you can see it in action here. limestone rocks, who would have thought?

0 Comments:

Post a Comment

<< Home