9 Replies Latest reply on Apr 16, 2007 1:43 PM by hasc

    Questions on resourceBundle

    hasc

      Hi,

      can someone explain me how to access recource bundle from within the application or give me an appropriate link where it is explained?

      I couldn't find a complete example here in the forum or in the seam documentation.

      I knpow that in components.xml i can define recource bundles but i dont know how to access one from the application.

      do i need to import from java.Util or org.jboss.seam.core?
      how to i load a message from a bundle-name within the application?

      Thanks for help,
      hasc

        • 1. Re: Questions on resourceBundle
          monkeyden

          13.2.2. Displaying labels

          Just do this:

          //Inject the resource bundle, assuming you have a file called
          //messages_en.properties, with your default locale replacing "en"

          @In private Map<String, String> messages;

          //Pull the message out of the bundle
          @In("#{messages['Hello']}")
          private String helloMessage;

          • 2. Re: Questions on resourceBundle
            hasc

            OK this is exactly from the seam docu.

            but what i want is loading a specific resource bundle within the application. and then accessing a key within a method. and i want to do all that in a Converters getAsObject method.

            so i'd like to have a specific bundle for error messages and i want to access that.

            my question is how to access a special bundle lets say "MyBundle" registered in components.xml via

            <component name="org.jboss.seam.core.resourceBundle">
             <property name="bundleNames">
             <value>MyBundle</value>
             </property>
            </component>


            or if its not possible to load MyBundle from a Converter how can I load the messages.properties?



            • 3. Re: Questions on resourceBundle
              hasc

              so far i' ve tried the following:

              in faces-config.xml:

              <application>
               <locale-config>
               <default-locale>de</default-locale>
               <supported-locale>de</supported-locale>
               <supported-locale>en</supported-locale>
               <supported-locale>fr</supported-locale>
               </locale-config>
               <message-bundle>messages</message-bundle>
              </application>


              in the *.ear i have the files messages.properties, messages_en.properties, messages_fr.properties, messages_de.properties in the directory WEB-INF/classes

              in the getAsObject() Method of the CustomConverter i throw an Exception:
              ...
               FacesMessage message = MessageFactory.getMessage(FacesMessage.SEVERITY_ERROR, "AreaConverterPositiveNumber");
               throw new ConverterException(message);


              and the MessageFactory class is the following:

              public class MessageFactory {
              
               public static FacesMessage getMessage(FacesMessage.Severity severity, String id) {
               String summary = "???" + id + "???";
               String detail = null;
              
               FacesContext context = FacesContext.getCurrentInstance();
               String name = context.getApplication().getMessageBundle();
               Locale locale = context.getViewRoot().getLocale();
               ResourceBundle bundle = ResourceBundle.getBundle(name, locale);
               summary = bundle.getString(id);
               detail = bundle.getString(id + "Detail");
              
               return new FacesMessage(severity, summary, detail);
               }
              }


              i get the following error message:

              2007-04-16 17:53:48,828 ERROR [STDERR] 16.04.2007 17:53:48 com.sun.faces.lifecycle.ProcessValidationsPhase execute
              WARNUNG: Can't find bundle for base name messages, locale de
              java.util.MissingResourceException: Can't find bundle for base name messages, locale de
               at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:836)
               at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:805)
               at java.util.ResourceBundle.getBundle(ResourceBundle.java:576)
               at gmp.webapp.converter.MessageFactory.getMessage(MessageFactory.java:18)
               at gmp.webapp.converter.AreaConverter.getAsObject(AreaConverter.java:90)
               at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:188)
               at javax.faces.component.UIInput.getConvertedValue(UIInput.java:943)


              Would be great if someone could help me with that.

              • 4. Re: Questions on resourceBundle
                fernando_jmt

                So, I think the MessageFactory class you have is not needed. You can use Seam org.jboss.seam.core.FacesMessages class instead.

                Below you can see an example I have:

                
                @Name("converters")
                public class Converters {
                
                 @Transactional
                 public Converter getRoleConverter() {
                 return new Converter() {
                
                 @Transactional
                 public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String string) throws ConverterException {
                 Role role = ((EntityManager) Component.getInstance("entityManager")).find(Role.class, string);
                 if (role != null)
                 return role;
                 else {
                 FacesMessage message = FacesMessages.createFacesMessage(FacesMessage.SEVERITY_ERROR, "Common.error.selected_NotFound", null, "");
                 throw new ConverterException(message);
                 }
                 }
                
                 public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object object) throws ConverterException {
                 return ((Role) object).getName();
                 }
                 };
                 }
                
                }
                
                


                Common.error.selected_NotFound is a key in the messages.properties.


                HTH.


                • 5. Re: Questions on resourceBundle
                  hasc

                  thanks for help but this seems to work only if i define the error message in messages.properties

                  if i define a internationalized message in message_XY.properties it is not read and if i delete the message from message.properties i get a NullPointerException

                  if someone could explain me the reason why i get this java.util.MissingResourceException that would be great.

                  regards



                  • 6. Re: Questions on resourceBundle
                    monkeyden

                    Though I only use the default messages file, I might try:

                    @In private Map<String, String> MyBundle;

                    I would also assume Seam just uses the bundle name:

                    <value>MyBundle</value>
                    to find a file named MyBundle_de.properties.


                    • 7. Re: Questions on resourceBundle
                      hasc

                      Yeah but since it is a converter i would appreciate not to inject anything. It would be great to just get the bundle loaded like its explained in various tutorials.

                      i just have no clue why this doesnt work here.


                      • 8. Re: Questions on resourceBundle
                        monkeyden

                        Ah, sorry, didn't see that it was a converter. What it sounds like you're asking is how to use a file other than "messages.properties"in JSF. Are you using the default bundle or not Because here you're not even telling JSF about "MyBundle". You're still using "messages".

                        <application>
                         <locale-config>
                         <default-locale>de</default-locale>
                         <supported-locale>de</supported-locale>
                         <supported-locale>en</supported-locale>
                         <supported-locale>fr</supported-locale>
                         </locale-config>
                         <message-bundle>messages</message-bundle>
                        </application>


                        Where does the converter live by the way (.ear or .war)? I had a problem getting a TabChangeListener to work when it was in the EJB context.

                        • 9. Re: Questions on resourceBundle
                          hasc

                          thanks monkedan for your help.

                          yeah after my several posts i myade this more and more confusing.

                          initially i wanted to load a custom bundle like MyBundle located in /WEB-INF/classes. Then i tried to load the default messages bundle but that caused the NullPointerException

                          What i think that i understood so far:

                          In faces-config i defined a <message-bundle>messages</message-bundle> but i always got the NullPointerException. Now i modified the build.xml and copied the messages_XY.properties in the .jar package. now the bundle is found and a message is returned.

                          I was expecting that it uses the files from /WEB-INF/classes directory but that seems to be wrong.

                          But if i understood right, i can overwrite the built-in errormessages by adapting the messages.properties file in /WEb-INF/classes. What means that the standard converters retrieve the messages from this file.

                          I inspected the source code of the MessageFactory.class from the jsf-api and it looks similar to mine.

                          so that keeps me a little bit confused... but maybe im sitting in front of the screen for too long today...

                          regards,
                          hasc