Xerces 2.11.0(Java)中特定于语言环境的消息 [英] Locale specific messages in Xerces 2.11.0 (Java)

查看:117
本文介绍了Xerces 2.11.0(Java)中特定于语言环境的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的JAXP和Xerces2中使用特定于语言环境的错误消息.默认情况下,只有英文消息可用.

I want to use locale specific error messages with my JAXP and Xerces2. By default only English messages are available.

第一步是检索消息文件,并将它们放入"org/apache/xerces/impl/msg/"包中-完成.通过使用Locale.setDefault (Locale.GERMANY)会显示德语消息,从而可以正常工作.

First step is to retrieve the messages files and put them into the package "org/apache/xerces/impl/msg/" - done. By using Locale.setDefault (Locale.GERMANY) the German messages are displayed so this is working.

但是我希望根据每个实例对消息进行不同的本地化.因此,一个解析器应该返回英语消息,而另一个解析器应该返回德语消息.

But I want the messages to be differently localized on a per-instance basis. So one parser should return English messages and another parser should return German messages.

我用来创建SAX2解析器的代码是:

The code I'm using to create SAX2 parsers is:

org.xml.sax.XMLReader ret = XMLReaderFactory.createXMLReader ();

对于DOM,我正在使用像这样的DocumentBuilder(非常简化):

for DOM I'm using the DocumentBuilder likes this (very simplified):

    final DocumentBuilderFactory aDocumentBuilderFactory = DocumentBuilderFactory.newInstance ();
    final DocumentBuilder aDocBuilder = aDocBuilderFactory.newDocumentBuilder ();
    final Document doc = aDocumentBuilder.parse (aInputSource);

我发现了类似org.apache.xerces.impl.XMLErrorReporter类的东西,该类具有setLocale(Locale)方法,但是我没有找到获取/设置它的方法.

I found something like the org.apache.xerces.impl.XMLErrorReporter class which has a setLocale(Locale) method but I didn't find a way to get/set it.

切换到SAX1并不是一种选择.

Switching to SAX1 is not an option btw.

感谢您的帮助!

推荐答案

不是最大的可移植性,但它在99%的情况下都是apache解析器,因此可以使用.

Not the maximum in portability but it works as the parser is the apache parser in 99% of all cases.

final DocumentBuilderFactory aDocBuilderFactory = DocumentBuilderFactory.newInstance();
aDocBuilderFactory.setAttribute("http://apache.org/xml/properties/locale", Locale.FRANCE);
final DocumentBuilder aDocBuilder = aDocBuilderFactory.newDocumentBuilder();
final Document doc = aDocBuilder.parse (aInputSource);

对于SAXParser saxParser,只需调用saxParser.setProperty("http://apache.org/xml/properties/locale", Locale.FRANCE);

哦,忘记了官方消息: http://xerces.apache.org/xerces2-j/properties.html

Oh, forgot the official source: http://xerces.apache.org/xerces2-j/properties.html

这篇关于Xerces 2.11.0(Java)中特定于语言环境的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆