Re: Is MessageFormat supported?

[email protected]
Newsgroups gmane.comp.web.freemarker.user
Message-ID <OFE21C64DA.B14FD777-ON87257658.0071C267-87257658.00733508@blm.gov>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.