Announcement Announcement Module
Collapse
No announcement yet.
roo jsf managed bean localized messages Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • roo jsf managed bean localized messages

    Hi,

    Can you help me with localized messages in Managed beans with roo JSF?

    I see that ITDs uses just

    Code:
    Entity.delete() {
    FacesMessage facesMessage = new FacesMessage("Successfully deleted");
    FacesContext.getCurrentInstance().addMessage(null, facesMessage);
    }
    which is not localized. I want to show localized messages from resource bundle dependent on used locale.
    web jsf setup or scaffold maybe create

    Code:
    @ManagedBean
    @SessionScoped
    public class LocaleBean
    where can i get java.util.Locale.

    How should I use FacesMessages?

    I am trying
    Code:
    protected ResourceBundle bundle;
    bundle = ResourceBundle.getBundle("messages", context.getViewRoot().getLocale());
    String message = bundle.getString("message_successfuly_deleted_accounts");
    context.addMessage(null, new FacesMessage(message));
    which ends with java.util.MissingResourceException: Can't find bundle for base name messages, locale en_US

    with faces-config

    Code:
    <resource-bundle>
                <base-name>com.company.projname.web.i18n.messages</base-name>
                <var>messages</var>
    </resource-bundle>
            <locale-config>
                <default-locale>en</default-locale>
                <supported-locale>en</supported-locale>
                <supported-locale>de</supported-locale>
                <supported-locale>es</supported-locale>
                <supported-locale>cz</supported-locale>
            </locale-config>
    located src/main/resources/com/company/projname/web/i18n/messages_en.properties

    Thanks for advise.
    valerian

  • #2
    Internationalization is not complete in the jsf add-on. The problem is that although one can use a
    Code:
    expressionFactory.createValueExpression(elContext, "#{messages.label_list}"
    successfully,
    so far I have been unable to use parameter placeholders to references messages using the Java api , eg

    Code:
    label.saved={0} has been saved
    Please raise an improvement request.

    Comment


    • #3
      I know this thread is a bit old, but I noticed that the improvement was not raised on Jira, so I created a new improvement and suggested a patch for it:

      https://jira.springsource.org/browse/ROO-3284

      Let me know if my suggestion covers what you had in mind for parameter placeholders.

      Comment


      • #4
        In my LocaleBean

        Code:
        /**
        * Location of internationalization property files. 
        */
        public static final String BUNDLE_LOCATION = "com.foo.bar.web.i18n.messages";
        
        /**
             * Creates and adds <code>FacesMessage</code> to current <code>FacesContext</code>.
             * 
             * <p>
             * Uses {@link LocaleBean} to get <code>ResourceBundle</code>, which is used  
             * to get localized messages.
             * 
             * @param severity represent message severity level
             * @param summaryKey i18n key of message summary, displayed at top 
             * @param detailKey i18n key of message detail, can be <code>null</code> for no detail.
             * @param params parameters in message detail. Example: Account {0} was ...
             * 
             * @see FacesContext#addMessage(String, FacesMessage)
             * @see FacesMessage
             */
        	public void createAndAddMessage(Severity severity, String summaryKey,
        			String detailKey, String... params) {
        		ResourceBundle messages = ResourceBundle.getBundle(BUNDLE_LOCATION,
        				getLocale());
        		FacesMessage facesMessage;
        		String summary = messages.getString(summaryKey);
        		if (detailKey != null) {
        			String detail = messages.getString(detailKey);
        			if (params != null && params.length > 0) {
        				MessageFormat messageFormat = new MessageFormat(detail);
        				detail = messageFormat.format(params);
        			}
        			facesMessage = new FacesMessage(severity, summary, detail);
        		} else {
        			facesMessage = new FacesMessage(severity, summary, "");
        		}
        		FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        	}
        
        
        localeBean.createAndAddMessage(FacesMessage.SEVERITY_ERROR,	"message_bad_credentials", "message_bad_credentials");

        Comment


        • #6
          Yes, Allan applied the patch I provided.

          However, my first patch is generating messages with a toString on the object, and the growl component becomes messy on interface.

          On the current solution, the message is in the form “{0} successfully created”, and receives the entity object as a parameter. I intend to change it to pass the entity name instead of the object. So the resulting message would look like “Supplier successfully created”, for example.

          How the message is built currently:
          Code:
          FacesMessage facesMessage = MessageFactory.getMessage(message, supplier);
          How it will be after the fix:
          Code:
          FacesMessage facesMessage = MessageFactory.getMessage(message, “Supplier”);
          I will provide another patch shortly.

          Murilo

          Comment


          • #8
            Now it's fixed: https://github.com/SpringSource/spri...4571f0018cdcc4

            The next issue I plan to tackle is https://jira.springsource.org/browse/ROO-3026, to complete the internationalization of roo-generated JSF applications.

            I already have an idea on how to implement it, and will give it a try when I have some spare time.

            Comment


            • #9
              I also fixed a minor issue with the delete label - see https://github.com/SpringSource/spri...a48a68ee245f62
              Alan

              Comment

              Working...
              X