1
votes

My task is to translate all the messages of an application into another language, different than English. The tricky part is with the messages generated by the JSF framework itself. I came across various articles showing how to customize some particular conversion or validation error message, but i am interested in customizing every possible error message that the application might generate (including, for example, authentication and navigation error messages).

Is there a file that contains all the possible error messages?

So far, i came across a file named Messages.properties, located in the jsf-api jar , which contains Validation, Converter and Component error messages. But that is not enough, there is plenty other errors apart from these.

This jar is actually named jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar and is located under jboss-eap\modules\system\layers\base\javax\faces\api\main folder.
Inside this jar, the Messages.properties file is located under the javax\faces package.

I currently use Red Hat JBoss Enterprise Application Platform - Version 6.4.0.GA, JSF Implementation-Version: 2.1.28.Final-redhat-1

1
This has been answered here: stackoverflow.com/a/2668602/637609Mark W
I read that answer, i followed the link to the JSF specification, and the keys given there all fit into one of the following 3 packages: javax.faces.component, javax.faces.converter, javax.faces.validator. I had already mentioned in the question that i was looking for other types of errors, apart from "Validation, Converter and Component error". What if the application raises an Navigation Exception? I need to handle that, too. For example, javax.faces.application.NavigationCase.fromViewId could be the key of message for the case "Unable to find matching navigation case with from-view-id .."Newton fan 01

1 Answers

0
votes

The problem can be split into two parts:

  1. How do i override a framework error message?

  2. What is the list of all possible error messages that can be generated by the JSF framework?

Part 1: OVERRIDING FRAMEWORK'S ERROR MESSAGES

In my project, under WebContent\WEB-INF, there is faces-config.xml, which contains <message-bundle>resources</message-bundle>. 'resources' points to src\resources.properties. In this .properties file, i just have to add the corresponding entries, like

javax.faces.converter.DateTimeConverter.DATE={2}: ''{0}'' non poteva essere inteso come una data.

Should such error be generated by the application, the translated message will be displayed. However, as i understand from Cannot override validation error message, the use of a resources.properties file under src is maven specific. Different packaging technologies may require different solutions.

Part 2: LIST OF ALL ERROR MESSAGES

Taking a look at the files contained in javax.faces.jar http://www.java2s.com/Code/Jar/j/Downloadjavaxfacesjar.htm , i see the following packages:

  • javax.faces.application

  • javax.faces.bean

  • javax.faces.component

  • javax.faces.context

  • javax.faces.convert

  • javax.faces.el

  • javax.faces.event

  • javax.faces.lifecycle

  • javax.faces.model

  • javax.faces.render

  • javax.faces.validator

  • javax.faces.view

  • javax.faces.webapp

For every class in each of these packages, i have to add entries in the resources.properties for every error that this class might raise. But what are the errors that a class might raise? And what should the entry in the resource.properties look like?

I let myself guided by the entries that I've found in the Messages.properties file inside the jboss-jsf-api_2.1_spec-2.1.28.Final-redhat-1.jar (which I've mentioned in the question). It has the following entries, among many others:

# ==============================================================================
# Component Errors
# ==============================================================================
javax.faces.component.UIInput.CONVERSION={0}: Conversion error occurred.
javax.faces.component.UIInput.REQUIRED={0}: Validation Error: Value is required.
javax.faces.component.UIInput.UPDATE={0}: An error occurred when processing your submitted information.

In my IDE (eclipse), i click on an import statement import javax.faces.component.UIInput; to navigate to UIInput.class . In the Class File Editor , i have the following lines :

  // Field descriptor #193 Ljava/lang/String;
  public static final java.lang.String CONVERSION_MESSAGE_ID = "javax.faces.component.UIInput.CONVERSION";

  // Field descriptor #193 Ljava/lang/String;
  public static final java.lang.String REQUIRED_MESSAGE_ID = "javax.faces.component.UIInput.REQUIRED";

  // Field descriptor #193 Ljava/lang/String;
  public static final java.lang.String UPDATE_MESSAGE_ID = "javax.faces.component.UIInput.UPDATE";

I remark that the value of the String field is exactly the key that must be added in the resources.properties, like, for example, javax.faces.component.UIInput.CONVERSION.

For a new class, like, for example, javax.faces.validator.RegexValidator class, I will do the following: open it in class file editor, pick those fields whose name ends in _ID, like :

  // Field descriptor #30 Ljava/lang/String;
  public static final java.lang.String VALIDATOR_ID = "javax.faces.RegularExpression";

  // Field descriptor #30 Ljava/lang/String;
  public static final java.lang.String PATTERN_NOT_SET_MESSAGE_ID = "javax.faces.validator.RegexValidator.PATTERN_NOT_SET";

  // Field descriptor #30 Ljava/lang/String;
  public static final java.lang.String NOT_MATCHED_MESSAGE_ID = "javax.faces.validator.RegexValidator.NOT_MATCHED";

  // Field descriptor #30 Ljava/lang/String;
  public static final java.lang.String MATCH_EXCEPTION_MESSAGE_ID = "javax.faces.validator.RegexValidator.MATCH_EXCEPTION";

and add the following entries to the resources.properties file:

javax.faces.RegularExpression=custom message
javax.faces.validator.RegexValidator.PATTERN_NOT_SET=custom message
javax.faces.validator.RegexValidator.NOT_MATCHED=custom message
javax.faces.validator.RegexValidator.MATCH_EXCEPTION=custom message

Unfortunately, given the huge number of classes for which error messages must be provided, i don't find this as a feasible solution.

UPDATE

I just realized why only Conversion and Validation error messages should be provided: because they are related to user input, which the programmer cannot control. All the other errors like Navigation related issues must be properly handled by the programmer. Such errors should not appear in the first place, so it does not make sense to translate those messages anyway.