Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locale specific messages in Xerces 2.11.0 (Java)

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!

like image 456
Philip Helger Avatar asked Dec 26 '22 21:12

Philip Helger


1 Answers

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

like image 191
Holger Avatar answered Jan 16 '23 18:01

Holger