Uploaded image for project: 'Wicket'
  1. Wicket
  2. WICKET-1719

StringResourceModel may fail to format numbers using MessageFormat

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 1.3.3
    • 1.3.5, 1.4-RC1
    • wicket
    • None

    Description

      StringResourceModel API seems to suggest it's possible to use it without a Component reference using the following approach:

      m = new StringResourceModel("key", null, new Model(...), new Object[] {p1, p2, ...});
      m.setLocalizer(xxxApplication.get().getResourceSettings().getLocalizer());
      return m.toString()
      

      Unfortunately the above will fail with an exception if the message uses the MessageFormat approach and one of the parameters
      is a number, since the locale field in StringResourceModel won't be initialized, and will then be passed as null to new MessageFormat(value, locale) at line 514 of StringResourceFormat.

      I'm using the following crude workaround:

      StringResourceModel rm = new StringResourceModel(key, null, model, params) {
                  @Override
                  public void setLocalizer(Localizer localizer) {
                      super.setLocalizer(localizer);
                      // crude hack to force into the StringResoruceLoader to grab a locale,
                      // which is needed to format numbers with the MessageFormat approach
                      load();
                  }
              };
              rm.setLocalizer(GeoServerApplication.get().getResourceSettings().getLocalizer());
              return rm.getString();
      

      but boy, this looks ugly... Creating a custom subclass that does locale loading in the
      constructor would look better, but still quite ugly...

      Attachments

        Activity

          People

            ivaynberg Igor Vaynberg
            aaime Andrea Aime
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: