Locale specific messages in Xerces 2.11.0 (Java)

主宰稳场 提交于 2019-12-06 01:49:25

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

For a SAXParser saxParser simply call saxParser.setProperty("http://apache.org/xml/properties/locale", Locale.FRANCE);

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

Possibility set/get MessageFormatter:

Validator validator = schema.newValidator();      
XMLErrorReporter property = (XMLErrorReporter) validator.getProperty("http://apache.org/xml/properties/internal/error-reporter");
MessageFormatter messageFormatter = property.getMessageFormatter("http://www.w3.org/TR/xml-schema-1");
property.putMessageFormatter(MyMessageFormatter.SCHEMA_DOMAIN, new MyMessageFormatter());


public class MyMessageFormatter implements MessageFormatter {
    public static final String SCHEMA_DOMAIN = "http://www.w3.org/TR/xml-schema-1";
    //...
    public String formatMessage(Locale locale, String key, Object[] arguments)
            throws MissingResourceException {...}
    //...

}

I think you should try using

com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter 

If you are writing a custom validation, try calling its formatMessage(...) method, where you could provide the locale name as parameter.

An example of the same is provided in the apache library itself. See it http://cr.openjdk.java.net/~coffeys/openJDK.7u21.sync/webrev/jaxp/src/com/sun/org/apache/xerces/internal/impl/msg/XMLMessageFormatter_zh_CN.java-.html

or

http://www.docjar.com/html/api/com/sun/org/apache/xerces/internal/impl/msg/XMLMessageFormatter.java.html

Another approach could be that you may override the formatMessage() method to implement it in your own way. See the below implemented code of this method:

 public String More ...formatMessage(Locale locale, String key, Object[] arguments)
         throws MissingResourceException {
          if (fResourceBundle == null || locale != fLocale) {
             if (locale != null) {
                 fResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages", locale);
                 // memorize the most-recent locale
                 fLocale = locale;
             }
             if (fResourceBundle == null)
                 fResourceBundle = PropertyResourceBundle.getBundle("com.sun.org.apache.xerces.internal.impl.msg.XMLMessages");
         }

This indicate that, if a resource bundle file is declared according to locale, the control should be able to pick a different resource file having error message in different language.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!