Great! I implemented RB myself once, but this looks much better, thanks!
Posted by Marcin Cenkier at June 10, 2005 05:50 AMHay quá, xin cảm ơn nhiều
Posted by haso at December 11, 2005 07:31 PMVery cool however I found a bug in your impl. when asking for a resource bundle with a parent. This is how I fixed it. Notice the call to getString which makes sure to recursively go over the parent bundles. Also checking for null.
protected Object handleGetObject(String key) {
String value = (String)bundle.getString(key);
if (value==null) return null;
try {
return new String (value.getBytes("ISO-8859-1"),"UTF-8") ;
} catch (UnsupportedEncodingException e) {
// Shouldn't fail - but should we still add logging message?
return null;
}
}
And another small thing: not all ISO-8859-1 chars are a subset of UTF-8. The (C) sign seems to clash. If I put a \u00a9 in a UTF-8 file, it fails. But if I put the actual sign, it works. For some reason it is encoded as two bytes even though it is below 192 decimal.
Not a big deal though. Still very cool solution.
Posted by Brian at December 27, 2005 06:28 PMYou are absolutely right on both points Brian. A good catch on NPE with parent bundles. About the other thing you mentioned, I've known about it for a long time but I've been lazy to update this entry and also wanted to see when somebody would report on it. The thing is that even though ISO-8859 characters have the same numeric character reference in UTF-8, all the 8-bit (above ASCII) characters are encoded with a double byte in UTF-8 (whereas all characters are encoded as a single byte - 7 or 8 bit - in ISO-8859). So, the above really works only if you know which character set you've stored your properties file in. Still, (and why it may be better, depending on your application code) *the rest* of your application code doesn't need to know about it.
Posted by Alphageek at January 8, 2006 11:18 PMVery cool. I've found a bug, too. If you have your own classloader to load the resources, the method
public static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) {
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale);
return createUtf8PropertyResourceBundle(bundle);
}
does not work.
Instead, you have to fix it to
public static ResourceBundle getBundle(String baseName, Locale locale, ClassLoader loader) {
ResourceBundle bundle = ResourceBundle.getBundle(baseName, locale, loader);
return createUtf8PropertyResourceBundle(bundle);
}
supplying the loader to the getBundle method. Now all works great as expected.
Posted by Manfred Steinbach at April 24, 2007 07:46 AM