Results 1 to 9 of 9

Thread: roo jsf managed bean localized messages

  1. #1

    Question 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. #2
    Join Date
    Dec 2005
    Posts
    930

    Default

    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.
    Alan Stewart
    Spring Roo Committer
    twitter @alankstewart

  3. #3
    Join Date
    Sep 2009
    Posts
    13

    Default

    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.

  4. #4

    Default

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

  5. #5
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    They recently fixed your submitted JIRA issue for the upcoming Roo 1.2.3 release.

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

    Ken
    Ken Rimple
    Chariot Solutions
    email: krimple@chariotsolutions.com
    work: www.chariotsolutions.com/education
    personal: www.rimple.com

    Author: Spring Roo in Action (Manning)
    MEAP Site: manning.com/rimple

  6. #6
    Join Date
    Sep 2009
    Posts
    13

    Default

    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

  7. #7
    Join Date
    Jun 2008
    Location
    Philadelphia, PA, USA
    Posts
    212

    Default

    Great! Yes, I wanted to let people know there was activity. Sorry for jumping on it too early

    Ken
    Ken Rimple
    Chariot Solutions
    email: krimple@chariotsolutions.com
    work: www.chariotsolutions.com/education
    personal: www.rimple.com

    Author: Spring Roo in Action (Manning)
    MEAP Site: manning.com/rimple

  8. #8
    Join Date
    Sep 2009
    Posts
    13

    Default

    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.

  9. #9
    Join Date
    Dec 2005
    Posts
    930

    Default

    I also fixed a minor issue with the delete label - see https://github.com/SpringSource/spri...a48a68ee245f62
    Alan
    Alan Stewart
    Spring Roo Committer
    twitter @alankstewart

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •