简体   繁体   English

SAXParseException已本地化

[英]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. 我提供的服务可以使用人类读取和理解的XML。并使用exception.getMessage()生成带有解析器错误列表的报告(精确地为SAXParseException exception.getMessage()exception.getLocalizedMessage()返回相同的结果)。 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 . 首先需要从Apache Xerces获取XMLSchemaMessages.properties 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 . 我从http://xerces.apache.org/下载了Xerces-J-src.2.11.0.tar.gz ,解压缩并从以下位置获取此文件: ...\\src\\org\\apache\\xerces\\impl\\msg

Now rename this file to XMLSchemaMessages_pl.properties or localization You need and place in classpath. 现在,将此文件重命名为XMLSchemaMessages_pl.properties或本地化,您需要将其放置在类路径中。 I have project in Maven so i put this file into: src\\main\\resources\\com\\sun\\org\\apache\\xerces\\internal\\impl\\msg . 我在Maven中有项目,因此我将此文件放入: 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 . 根据Java文档 ,您需要扩展SAXParseException并覆盖getLocalizedMessage ,默认实现返回与getMessage相同的结果。

Edit: You can have seperate property file for each language and in each you can have code and local message . 编辑:您可以为每种语言使用单独的属性文件,并且在每种语言中可以包含codelocal message When you raise SAXParseException , based on the locale and some code , returns the appropriate message . 当根据locale和一些code引发SAXParseException ,将返回适当的message

MySAXParseException ex = new MySAXParseException(<code>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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