Hi,
Thanks to everyone for their suggestions. I thought I would share my
solution as well. It appears that Sun added the basic things you need for
UTF-8 based property files in JDK 1.6. They added a new constructor to
PropertyResourceBundle which takes a Reader and they introduced the
ResourceBundle.Control class to give you more control over bundle creation.
See:
http://72.5.124.55/javase/6/docs/api/java/util/ResourceBundle.Control.html
I made my own ResourceBundle.Control class that uses the new constructor in
PropertyResourceBundle which takes a reader.
I started from the their example for doing "XML based" bundle support.
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
public class UTF8ResourceBundleControl extends ResourceBundle.Control {
public List<String> getFormats(String basename){
if (basename == null) throw new NullPointerException();
return Arrays.asList("properties");
}
@Override
public ResourceBundle newBundle(String baseName,Locale locale,String
format,ClassLoader loader,boolean reload) throws
IllegalAccessException,InstantiationException,IOException {
if (baseName == null || locale == null || format == null || loader
== null) throw new NullPointerException();
ResourceBundle bundle = null;
if (format.equals("properties")) {
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, format);
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
* InputStreamReader is=new
InputStreamReader(stream,"UTF-8");
bundle = new PropertyResourceBundle(is);*
is.close();
}
}
return bundle;
}
}
This could be used anywhere you want UTF-8 bundle loading.
Then I found this "secret sauce" buried away on a forum post which lets you
manipulate the JSTL localization context (used by <fmt:message/>) directly
from java. I put it in a @Before method on a base action bean so everything
is setup by the time the pages run.
@Before(stages={LifecycleStage.EventHandling})
public void init(){
ResourceBundle bundle =
ResourceBundle.getBundle("resources.module.cms.template.messages",
this.getContext().getRequest().getLocale(), new
UTF8ResourceBundleControl());
javax.servlet.jsp.jstl.core.Config.set(this.getContext().getRequest().getSession(),
Config.FMT_LOCALIZATION_CONTEXT, new LocalizationContext(bundle
,this.getContext().getRequest().getLocale()));
}
The other nice thing about this is that I believe it kills of having to do
this (taken from stripes doc)
One exception is that <fmt:message> does not call request.getLocale()(1). In
the case that a request sends no accept-language headers, you will need to
include a call to <fmt:setLocale value="${pageContext.request.locale}"/>before
<fmt:message>.
by propagating the locale picker's selection into the JSTL stuff.
On Tue, Mar 9, 2010 at 2:52 PM, Ross Sargant <rsarg...@tvrc.com> wrote:
> Hi,
> Does anybody know of a good solution for dealing with the fact that java
> property files only support ISO-8859-1 encoding?
>
> I would really prefer that I could have them be UTF-8 and work with them
> naturally in different languages (with no need to use the "nativetoascii"
> tool) or look at "U" codes in my files.
>
> I'm having trouble using the fmt:message tag with resource bundles that
> contain non-ascii unicode characters ( I get garbled output). Other UTF-8
> data displays without issue so page encoding, response encoding etc is all
> good.
>
> I *think* the problem is that java property files are assumed to be
> ISO-8859-1 encoded so the localized values aren't ready correctly from the
> file.
>
> Am I the only one that finds this hilarious considering that property
> files are touted as the answer to internationalization?
>
> Appreciate any and all suggestions!
>
>
>
> --
> Ross Sargant
> Software Engineer
> p: 954-623-6015 x2108
> email: rsarg...@tvrc.com
>
> TVR Communications LLC
> 541 S. State Road 7,Suite 5,Margate, Florida,33068
>
> http://www.tvrc.com
>
>
--
Ross Sargant
Software Engineer
p: 954-623-6015 x2108
email: rsarg...@tvrc.com
TVR Communications LLC
541 S. State Road 7,Suite 5,Margate, Florida,33068
http://www.tvrc.com
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users