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...