Wicket
  1. Wicket
  2. WICKET-1719

StringResourceModel may fail to format numbers using MessageFormat

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.3.3
    • Fix Version/s: 1.3.5, 1.4-RC1
    • Component/s: wicket
    • Labels:
      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...

        Activity

        No work has yet been logged on this issue.

          People

          • Assignee:
            Igor Vaynberg
            Reporter:
            Andrea Aime
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development