1

我有解析 XML 的服务,并使用(返回相同的)可以被人类阅读和理解的解析器错误列表(SAXParseException完全)生成报告。如何将此异常消息本地化为英语以外的其他语言?exception.getMessage()exception.getLocalizedMessage()

4

2 回答 2

4

我找到了解决方案。首先需要XMLSchemaMessages.propertiesApache Xerces. 我Xerces-J-src.2.11.0.tar.gzhttp://xerces.apache.org/下载,解压缩并从位置获取此文件:...\src\org\apache\xerces\impl\msg.

现在将此文件重命名为XMLSchemaMessages_pl.properties或本地化您需要并放置在类路径中。我在 Maven 中有项目,所以我将此文件放入:src\main\resources\com\sun\org\apache\xerces\internal\impl\msg.

就这样。对此文件的更改将在异常消息中可见。

于 2013-12-11T13:59:51.547 回答
2

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>);
于 2013-09-26T12:07:24.560 回答