I want to use locale specific error messages with my JAXP and Xerces2. By default only English messages are available.
First step is to retrieve the messages files and put them into the package "org/apache/xerces/impl/msg/" - done. By using Locale.setDefault (Locale.GERMANY)
the German messages are displayed so this is working.
But I want the messages to be differently localized on a per-instance basis. So one parser should return English messages and another parser should return German messages.
The code I'm using to create SAX2 parsers is:
org.xml.sax.XMLReader ret = XMLReaderFactory.createXMLReader ();
for DOM I'm using the DocumentBuilder likes this (very simplified):
final DocumentBuilderFactory aDocumentBuilderFactory = DocumentBuilderFactory.newInstance ();
final DocumentBuilder aDocBuilder = aDocBuilderFactory.newDocumentBuilder ();
final Document doc = aDocumentBuilder.parse (aInputSource);
I found something like the org.apache.xerces.impl.XMLErrorReporter
class which has a setLocale(Locale)
method but I didn't find a way to get/set it.
Switching to SAX1 is not an option btw.
Any help is appreciated!
Not the maximum in portability but it works as the parser is the apache parser in 99% of all cases.
final DocumentBuilderFactory aDocBuilderFactory = DocumentBuilderFactory.newInstance();
aDocBuilderFactory.setAttribute("http://apache.org/xml/properties/locale", Locale.FRANCE);
final DocumentBuilder aDocBuilder = aDocBuilderFactory.newDocumentBuilder();
final Document doc = aDocBuilder.parse (aInputSource);
For a SAXParser saxParser
simply call saxParser.setProperty("http://apache.org/xml/properties/locale", Locale.FRANCE);
Oh, forgot the official source: http://xerces.apache.org/xerces2-j/properties.html
Possibility set/get MessageFormatter:
Validator validator = schema.newValidator();
XMLErrorReporter property = (XMLErrorReporter) validator.getProperty("http://apache.org/xml/properties/internal/error-reporter");
MessageFormatter messageFormatter = property.getMessageFormatter("http://www.w3.org/TR/xml-schema-1");
property.putMessageFormatter(MyMessageFormatter.SCHEMA_DOMAIN, new MyMessageFormatter());
public class MyMessageFormatter implements MessageFormatter {
public static final String SCHEMA_DOMAIN = "http://www.w3.org/TR/xml-schema-1";
//...
public String formatMessage(Locale locale, String key, Object[] arguments)
throws MissingResourceException {...}
//...
}
I think you should try using
com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter
If you are writing a custom validation, try calling its formatMessage(...) method, where you could provide the locale name as parameter.
An example of the same is provided in the apache library itself. See it http://cr.openjdk.java.net/~coffeys/openJDK.7u21.sync/webrev/jaxp/src/com/sun/org/apache/xerces/internal/impl/msg/XMLMessageFormatter_zh_CN.java-.html
or
Another approach could be that you may override the formatMessage() method to implement it in your own way. See the below implemented code of this method:
public String More ...formatMessage(Locale locale, String key, Object[] arguments)
throws MissingResourceException {
if (fResourceBundle == null || locale != fLocale) {
if (locale != null) {
fResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages", locale);
// memorize the most-recent locale
fLocale = locale;
}
if (fResourceBundle == null)
fResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages");
}
This indicate that, if a resource bundle file is declared according to locale, the control should be able to pick a different resource file having error message in different language.
来源:https://stackoverflow.com/questions/18531633/locale-specific-messages-in-xerces-2-11-0-java