6 Replies Latest reply on Mar 25, 2010 8:51 AM by nimo22

    pass a variable to a message-key

    nimo22

      Does anyone know, how can I do something like this:


      I want to pass a variable to a message-key. But this does not work.


      <s:selectItems value="#{users}" var="i" label="#{messages[hobby.#{_s}]}"/>



      this does also not work:


      <s:selectItems value="#{users}" var="i" label="#{messages[name]}">
           <ui:param name="name" value="hobby.#{_s}"/>
      </s:selectItems>




      any ideas?



        • 1. Re: pass a variable to a message-key
          niox.nikospara.yahoo.com

          Hi,


          What I do is create an EL function named x:strcat and use it as follows:


          <s:selectItems value="#{users}" var="i" label="#{messages[x:strcat('hobby.' , _s]}"/>
          



          I had written a detailed description on how to create such a function here (its very simple actually) and there are plenty of examples in the web!

          • 2. Re: pass a variable to a message-key
            thokuest

            That's even easier:


            <s:selectItems value="#{users}" var="i" label="#{messages['hobby.'.concat(_s)]}"/>



            By the way: what is _s?


            • 3. Re: pass a variable to a message-key
              nimo22

              hello thanks.


              sorry I have ment that:



              <s:selectItems value="#{users}" var="_s" label="#{messages[hobby.#{_s}]}"/>




              So you see, I want to pass the variable


              "_s"



              into my message-bundle.


              So Thomas, your solution will not work.


              Nikos, I am not sure, if your solution works.


              I do not concat a static string


              "_s"



              with hobby - the string is only available via


              #{_s}



              .


              Any Ideas?

              • 4. Re: pass a variable to a message-key
                thokuest

                So, hobby is your message key?

                • 5. Re: pass a variable to a message-key
                  thokuest

                  Any Ideas?


                  If you have the following message key


                  hobby=Your hobby is #{_s}



                  you only have to write


                  <s:selectItems value="#{users}" var="_s" label="#{messages['hobby']}"/>



                  and #{_s} will resolved to it's current value.


                  Alternatively, you can introduce a utility component that interpolates the message for you:


                  @Name("messageUtil")
                  @BypassInterceptors
                  pubic class MessageUtil {
                      public String getMessage(String key, Object... params) {
                          SeamResourceBundle resourceBundle = (SeamResourceBundle) Component
                                  .getInstance("org.jboss.seam.core.resourceBundle", true);
                          
                          if (!resourceBundle.containsKey(key)) {
                              return key;
                          }
                  
                          return Interpolator.instance().interpolate(resourceBundle.getString(key), params);
                      }
                  }
                  



                  Your message key should look like the following:


                  hobby=Your hobby is {0}



                  You can use the component like this:


                  <s:selectItems value="#{users}" var="_s" label="#{messageUtil.getMessage('hobby', _s)}"/>



                  Hope that helps!

                  • 6. Re: pass a variable to a message-key
                    nimo22

                    Thank you!!