8 Replies Latest reply on Mar 26, 2008 11:35 AM by pmuir

    Using messages (from messages.properties) in code

    rstevens

      For internationalization, we use messages from messages.properties in our XHTML file but are having problem using them in our JAVA files.


      In the seam 2.0.1.GA documentation it says:



      You can even use the messages in your code:
      
      @In private Map<String, String> messages;
      
      @In("#{messages['Hello']}") private String helloMessage;




      We tried that but messages is always null. We have tried using



      @In, @In (create=true), and from POJOs, entities, and even changing the scope




      What are we doing wrong? Here's a code snippet:


      @Name ("nameValue")
      @Scope(ScopeType.APPLICATION)
      @AutoCreate
      
      
      public class NameValue extends Object {
      
           @In(create=true) private Map<String, String> messages;
      
           public String getName(String message, Integer index)
           {
                String[] result;
                
                result = messages.get(message).split(",");
      
                return result[index];
           }


        • 1. Re: Using messages (from messages.properties) in code
          msystems

          Robert Stevens wrote on Mar 22, 2008 01:09 AM:


          @In private Map<String, String> messages;



          @Name ("nameValue")
          @Scope(ScopeType.APPLICATION)
          @AutoCreate
          
          
          public class NameValue extends Object {
          
               @In(create=true) private Map<String, String> messages;
          
               public String getName(String message, Integer index)
               {
                    String[] result;
                    
                    result = messages.get(message).split(",");
          
                    return result[index];
               }




          What?


          But it should work just fine if you declare all your bundles in the components.xml:


             <core:resource-loader>
                  <core:bundle-names>
                      <value>your bundle #1</value>
                      <value>your bundle #2</value>
                      <value>your bundle etc</value>
                  </core:bundle-names>
              </core:resource-loader>



          and now you can use e.g.:


          @In("#{messages['Hello']}") private String helloMessage;

          • 2. Re: Using messages (from messages.properties) in code
            msystems

            BTW: You don't need to declare built-in bundles (e.g. 'messages' JSF bundles) in the components.xml. You can just use, e.g.:


            @In("#{messages['javax.faces.converter.ShortConverter.SHORT_detail']}") private String message;


            • 3. Re: Using messages (from messages.properties) in code
              rstevens

              Thanks. I've tried all your suggestions but cannot get them to work in java files.


              Messages from my messages.properties bundle works fine inside annotations like:


                   @Length(max=50, message="#{messages['error.length.fifty']}")
                   private String city = "";



              Or in xhtml files like:



                   <s:decorate template="/tags/required.xhtml">
                          <ui:define name="label">#{messages['register.city']}</ui:define>
                          <h:inputText id="city" value="#{user.city}" required="true" size="40" maxlength="64" requiredMessage="#{messages['error.user.city.required']}"/>
                      </s:decorate>



              but I can't get them to work as you both have suggested in java files. I am using Seam 2.0.1.GA. Any more suggestions?


              What I really want to do is to pass in string or an id to a function and have the function look up the string from messages.properties. Can that be done?


              • 4. Re: Using messages (from messages.properties) in code
                msystems

                Robert Stevens wrote on Mar 22, 2008 06:17 PM:


                Thanks. I've tried all your suggestions but cannot get them to work in java files.

                Messages from my messages.properties bundle works fine inside annotations like:

                     @Length(max=50, message="#{messages['error.length.fifty']}")
                     private String city = "";



                Or in xhtml files like:


                     <s:decorate template="/tags/required.xhtml">
                            <ui:define name="label">#{messages['register.city']}</ui:define>
                            <h:inputText id="city" value="#{user.city}" required="true" size="40" maxlength="64" requiredMessage="#{messages['error.user.city.required']}"/>
                        </s:decorate>



                but I can't get them to work as you both have suggested in java files. I am using Seam 2.0.1.GA. Any more suggestions?


                It works for me - there must be something wrong with your project structure/setup/packing.



                Robert Stevens wrote on Mar 22, 2008 06:17 PM:


                What I really want to do is to pass in string or an id to a function and have the function look up the string from messages.properties. Can that be done?




                What about


                SeamResourceBundle.getBundle().getString(<bundleKey>);



                Or


                ResourceBundle.getBundle("messages", FacesContext.getCurrentInstance().getViewRoot().getLocale()).getString(<bundleKey>);


                • 5. Re: Using messages (from messages.properties) in code
                  rstevens

                  Kenneth,


                  Thank you! Both your examples work:



                  import org.jboss.seam.core.SeamResourceBundle;
                  import javax.faces.context.FacesContext;
                  
                            s= SeamResourceBundle.getBundle().getString("register.city");
                  
                            s= SeamResourceBundle.getBundle("messages", FacesContext.getCurrentInstance().getViewRoot().getLocale()).getString("register.city");
                  
                  



                  But why does it not work with normal notation such as:



                       @In("#{register.city}") String city;




                  I packaged my application as a WAR file and am using hibernate. I am using mostly POJOs but some classes are declared @Entity.


                  Any other clues?



                  • 6. Re: Using messages (from messages.properties) in code
                    msystems

                    Robert Stevens wrote on Mar 22, 2008 07:28 PM:


                    Kenneth,

                    Thank you! Both your examples work:


                    import org.jboss.seam.core.SeamResourceBundle;
                    import javax.faces.context.FacesContext;
                    
                              s= SeamResourceBundle.getBundle().getString("register.city");
                    
                              s= SeamResourceBundle.getBundle("messages", FacesContext.getCurrentInstance().getViewRoot().getLocale()).getString("register.city");
                    
                    



                    But why does it not work with normal notation such as:


                         @In("#{register.city}") String city;







                    Because you need to use:


                    @In("#{messages['register.city']}") String city;



                    • 7. Re: Using messages (from messages.properties) in code
                      rstevens

                      Sorry, typo. I meant:


                      @In("#{messages['register.city']}") String city;




                      OK. I got it working this way in a Java action file by injecting the messages:



                           @In
                           protected Map<String, String> messages;
                      
                           String s = messages.get("register.city");



                      but the same code does not work in a Java file for my model.


                      • 8. Re: Using messages (from messages.properties) in code
                        pmuir

                        Injection doesn't work in classes annotated


                        @Name("foo") @Entity



                        RTFM ;-)