我有類似這個問題一個問題:SAXParseException localized如何在解析XML文件時國際化SAXParseException?
我試圖解析XML文件,並在幾種語言得到分析器錯誤(的SAXParseException)的列表,例如:
XmlImporter.importFile(params, "en")
應返回英文錯誤列表,XmlImporter.importFile(params, "fr")
應返回法語錯誤列表,XmlImporter.importFile(params, "pl")
應返回波蘭語錯誤列表。
XmlImporter.importFile(params, "...")
的每個調用都可能使用不同的語言環境。
這是我的驗證方法:
private void validate(String xmlFilePath, String schemaFilePath) throws Exception {
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = schemaFactory.newSchema(new File(schemaFilePath));
Validator validator = schema.newValidator();
XmlErrorHandler errorHandler = new XmlErrorHandler();
validator.setErrorHandler(errorHandler);
try (InputStream stream = new FileInputStream(new File(xmlFilePath))) {
validator.validate(new StreamSource(stream));
}
XmlErrorHandler:
public class XmlErrorHandler implements ErrorHandler {
private List<String> errorsList = new ArrayList<>();
public List<String> getErrorsList() {
return errorsList;
}
@Override
public void warning(SAXParseException exception) throws SAXException {
errorsList.add(prepareExceptionDescription(exception));
}
@Override
public void error(SAXParseException exception) throws SAXException {
errorsList.add(prepareExceptionDescription(exception));
}
@Override
public void fatalError(SAXParseException exception) throws SAXException {
errorsList.add(prepareExceptionDescription(exception));
}
private String prepareExceptionDescription(SAXParseException exception) {
return "Error: " +
"colNumber: " + exception.getColumnNumber() +
" line number: " + exception.getLineNumber() +
" message: " + exception.getLocalizedMessage();
}
}
我想,我需要通過某種方式/某處的java.util.Locale /字符串來獲得exception.getLocalizedMessage ()自定義消息(在en,fr或pl)?