3 Replies Latest reply on Oct 31, 2007 10:49 AM by mars1412

    Interpolator: FacesMessages from resource bundle with parame


      short explanation:

      the registration.success string in the resource bundle is:

      registration.success={0} has been registered sucessfully

      in my java file, I just want to add a faces massage like this:
      facesMessages.add("#{messages['registration.success']}", "#{webUser.name}");

      but this will render to:
      {0} has been registered sucessfully

      as you can see the parameter {0} has not been replaced with the given parameter

      so how would you make seam replace the parameter as expected?

      a workaround would of course be, to include the parameter #{webUser.name} directly in the .properties file, like:
      registration.success=#{webUser.name} has been registered sucessfully

      and to omit the parameter when calling facesMessages.add:

      this will of course work, but I do not want to use this, because:
      • I don't think it's good practice to inlcude buissnes intelligence in my resource files, because:

        • I will have to teach the translators another syntax exception
        • if I will ever rename the class of class member, I will probably forget to adopt the .properties file

        • I cannot re-use the same message. e.g. if I had another registration for customers, I would need another translation: e.g.
          registration.success.customer=#{customer.name} has been registered sucessfully

          thanks in advance

        • 1. Re: Interpolator: FacesMessages from resource bundle with pa

          I forgot to post the explanation, why the original code does not work:

          facesMessages.add("#{messages['registration.success']}", "#{webUser.name}");

          When debugging into the seam source I learned, that this cannot work, because the Interpolator.interpolate() method wil:
          examine the given string and try to evaluate all parameters in this string, which means:
          1st: "#{messages['registration.success']}" will be evaluated
          the one and only expression in this string is #{messages['registration.success']}
          the evaluation of this string results in this call:
          Interpolator.interpolate("#{messages['registration.success']}", null)

          which will return "{0} has been registered sucessfully"
          because no parameters have been passed to replace the parameter {0}
          then this string will be returned to the original Interpolator.interpolate() function (which has the parameters), but this resulting string will not be reevaluated, so that the given parameters will never be replaced in my message.

          • 2. Re: Interpolator: FacesMessages from resource bundle with pa
            Pete Muir Master

            I think you want something like

            @In User webUser;
            facesMessages.addFromResourceBundle("registration.success", webUser.getName());

            i.e. you can't add from a resource bundle with interpolation quite like you try.

            • 3. Re: Interpolator: FacesMessages from resource bundle with pa

              that was exactly what I was searching for

              tx pete
              - U really prevented a newbie of going mad -