JAXB exception messages: How to change language?
OK, I found out that the language used for the JAXB event messages is determined by the java system property user.language
. The default language is therefore dependent on the system it runs on.
I changed the run configuration of my java program by adding -Duser.language=en
. This changed the language of the JAXB event messages to english.
You should use a validator to validate, not unmarshaller. You can change its locale like this:
import javax.xml.validation.Validator;import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;import org.xml.sax.ErrorHandler;Validator validator = schema.newValidator();ErrorHandler errorHandler = new YourErrorHandler();validator.setErrorHandler(errorHandler);XMLErrorReporter xmlErrorReporter = (XMLErrorReporter) validator .getProperty("http://apache.org/xml/properties/internal/error-reporter");xmlErrorReporter.setLocale(new Locale("ru", "RU"));
Though with Java9+ you'd need to export the com.sun.org.apache.xerces.internal.impl
package from the java.xml
module via compiler option. See this answer for more details.