Reputation: 28556
I have service that parse XML and produce report with list of parser errors (SAXParseException
exactly) using exception.getMessage()
(exception.getLocalizedMessage()
return the same) that can be read and understand by humans. How to localize this exception messages in a language other than English ?
Upvotes: 1
Views: 2238
Reputation: 127
1.Use xercesImpl dependency
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.12.2</version>
</dependency>
see https://mvnrepository.com/artifact/xerces/xercesImpl
2.Translate this file https://github.com/JetBrains/jdk8u_jaxp/blob/master/src/com/sun/org/apache/xerces/internal/impl/msg/XMLSchemaMessages.properties
name it like XMLSchemaMessages_<lower_case_language_code>.properties and put into directory src/main/resources/org/apache/xerces/impl/msg, according to path in the org.apache.xerces.impl.xs.XSMessageFormatter
3.Create custom XSMessageFormatterLocale, extends org.apache.xerces.impl.xs.XSMessageFormatter:
/** Use file XMLSchemaMessages_<lower_case_language_code>.properties,
ex: put file src/main/resources/org/apache/xerces/impl/msg/XMLSchemaMessages_ru.properties for "ru-RU"
final Validator validator = schema.newValidator();
// https://svn.apache.org/repos/asf/xerces/site/trunk/production/xerces2-j/javadocs/xerces2/org/apache/xerces/impl/XMLErrorReporter.html
XMLErrorReporter property = (XMLErrorReporter) validator.getProperty("http://apache.org/xml/properties/internal/error-reporter");
property.putMessageFormatter(XSMessageFormatterLocale.SCHEMA_DOMAIN, new XSMessageFormatterLocale(Locale.forLanguageTag("ru-RU")));
*/
public class XSMessageFormatterLocale extends XSMessageFormatter { // org.apache.xerces.impl.xs.XSMessageFormatter
private final Locale locale;
public XSMessageFormatterLocale(Locale locale) {
this.locale = locale;
}
public String formatMessage(Locale locale, String key, Object[] arguments)throws MissingResourceException {
return super.formatMessage(this.locale, key, arguments);
}
}
Profit!
final Validator validator = schema.newValidator();
XMLErrorReporter property = (XMLErrorReporter) validator.getProperty("http://apache.org/xml/properties/internal/error-reporter");
property.putMessageFormatter(XSMessageFormatterLocale.SCHEMA_DOMAIN, new XSMessageFormatterLocale(Locale.forLanguageTag("ru-RU")));
validator.validate(...);
Upvotes: 0
Reputation: 28556
I've found solution. First need to get XMLSchemaMessages.properties
from Apache Xerces
. I downloaded Xerces-J-src.2.11.0.tar.gz
from http://xerces.apache.org/, unzip and get this file from location: ...\src\org\apache\xerces\impl\msg
.
Now rename this file to XMLSchemaMessages_pl.properties or localization You need and place in classpath. I have project in Maven so i put this file into: src\main\resources\com\sun\org\apache\xerces\internal\impl\msg
.
And that's all. Changes to this file will be visible in exception messages.
Upvotes: 4
Reputation: 11487
As per the java doc, you need to extends SAXParseException
and override getLocalizedMessage
, the default implementation returns the same as getMessage
.
Edit:
You can have seperate property file for each language and in each you can have code
and local message
.
When you raise SAXParseException
, based on the locale
and some code
, returns the appropriate message
.
MySAXParseException ex = new MySAXParseException(<code>);
Upvotes: 2