Re: Is MessageFormat supported?
Attila Szegedi <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
There's a performance advantage of using the ResourceBundleModel's message formatting approach, namely that it'll cache the MessageFormat instances created from the parsed pattern. The way you're doing it, each invocation of MessageFormat.format(pattern, params) will parse the patterns again. This of course might be acceptable performance in your application, just wanted to make sure you're aware of it. Attila. On 2009.10.23., at 21:58, [email protected] wrote: > > Daniel Dekany <[email protected]> wrote on 10/22/2009 10:09:20 AM: > >> Thursday, October 22, 2009, 5:58:34 PM, [email protected] wrote: >> >>> >>> Actually I have it set up as a shared variable when I create the >>> Configuration. >> >> Don't... A year later you won't understand where did "messageFormat" >> gone. Better said, if where did it come earlier. >> > > According to the documentation I can set this as a shared variable in the > SpringMVC View Resolvers configuration (specifically on the > FreeMarkerConfigurer bean definition). For the app I'm working on this is > by far the best place to set this up (assuming it works... I've had some > problems getting the Spring configurer to play nice with the FreeMarker > Configuration, such as not being able to use FreeMarker's "setSettings()" > method because the Spring developers didn't include that in the > FreeMarkerConfigurer class). > >>>> >>>>> The only thing >>>>> is that it receives its arguments as a List<String> >>>> >>>> Note that TempalteMethodModelEx gets the arguments without being >>>> converted to string. (This TempalteMethodModel interface is a legacy >>>> thing...) >>> >>> This was what I tried the first time but I had a few problems getting > it >>> working. I'll keep trying because I'd like to get rid of those String >>> conversions; it shouldn't be that hard. >> >> Maybe you need this: freemarker.template.utility.DeepUnwrap >> > > I changed my TemplateMethodModel to a TemplateMethodModelEx, using > DeepUnwrap, and it's now much cleaner - I can pass in numbers and dates > instead of strings, and use the MessageFormat the way it was intended. > Since the code is small, here it is: > > public class MessageFormatter implements TemplateMethodModelEx > { > @SuppressWarnings("unchecked") > @Override > public Object exec(List arguments) throws TemplateModelException > { > // Unwrap the arguments > List<Object> unwrappedArgs = new ArrayList<Object>(); > for (Object a : arguments) > { > unwrappedArgs.add(DeepUnwrap.unwrap((TemplateModel) a)); > } > > // Get the MessageFormat pattern > String pattern = (String) unwrappedArgs.remove(0); > > // Convert the remaining sublist to an array > Object[] params = unwrappedArgs.toArray(); > > // Format and return > return MessageFormat.format(pattern, params); > } > } > > > ------------------------------------------------------------------------------ > Come build with us! The BlackBerry(R) Developer Conference in SF, CA > is the only developer event you need to attend this year. Jumpstart your > developing skills, take BlackBerry mobile applications to market and stay > ahead of the curve. Join us from November 9 - 12, 2009. Register now! > http://p.sf.net/sfu/devconference > _______________________________________________ > FreeMarker-user mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/freemarker-user ------------------------------------------------------------------------------ Join us December 9, 2009 for the Red Hat Virtual Experience, a free event focused on virtualization and cloud computing. Attend in-depth sessions from your desk. Your couch. Anywhere. http://p.sf.net/sfu/redhat-sfdev2dev