简体   繁体   中英

SAXParseException localized

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 ?

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.

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>);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM