Is MessageFormat supported?

10 messages Options
Embed this post
Permalink
Peter_Ford

Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink

I'm trying to use MessageFormat in combination with FM's localization
feature but I'm having some problems (which I won't go into at this time).
My question is, are there features in FreeMarker (builtins, for example)
that help with this and that may save me some effort? I don't see anything
in the online manual, but maybe I'm missing something.


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink
Wednesday, October 21, 2009, 6:00:03 PM, [hidden email] wrote:

> I'm trying to use MessageFormat in combination with FM's localization
> feature but I'm having some problems (which I won't go into at this time).
> My question is, are there features in FreeMarker (builtins, for example)
> that help with this and that may save me some effort? I don't see anything
> in the online manual, but maybe I'm missing something.

freemarker.ext.beans.ResourceBundleModel deals with this, AFAIR.
ResoureBundles should be wrapped into it automatically, so it's not
like you should know about this class. Just drop a ResourceBundle into
the data-model, then the resulting variable will be (copy-paste from
the JavaDoc):

A hash model that wraps a resource bundle. Makes it convenient to
store localized content in the data model. It also acts as a method
model that will take a resource key and arbitrary number of arguments
and will apply MessageFormat with arguments on the string represented
by the key.

Typical usages:

    * bundle.resourceKey will retrieve the object from resource bundle
      with key resourceKey

    * bundle("patternKey", arg1, arg2, arg3) will retrieve the string
      from resource bundle with key patternKey, and will use it as a
      pattern for MessageFormat with arguments arg1, arg2 and arg3


--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Peter_Ford

Re: Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink
I'll try this, although I was hoping to get away without ResourceBundles
specifically. To clarify a bit, here's a quick overview of what I'm doing:

I have all my localized strings in two files - i18n_en_US.ftl and
i18n_es_MX.ftl. In my top-level template I have...

<#import "i18n.ftl" as i18n />

...so the localization picks up the correct file based on the locale
string, which is already set up earlier in the template. This works fine
for fixed strings, of course. However I would like to be able to have
MessageFormat pattern strings in those same files, such as:

<#-- English version. i18n_es_MX.ftl has the Spanish translation -->
<#assign dateAndTimeNow = "It is {0,time} on {0,date}." />

Now in my actual template text body I would like to be able to do something
like this, using a hypothetical "?message" builtin:

<#assign now = "2009-10-20 14:38:00"?datetime("yyyy-MM-dd HH:mm:ss") />
${i18n.dateAndTimeNow?message(now)}

In this case the hypothetical "?message" builtin would take the string as a
MessageFormat pattern, and the bracketed parameter list ('now' in this
case) as the argument list to be rendered using a call to
java.text.MessageFormat. Nice for me - I just put my patterns in the same
files as my other strings and use the localization feature to take care of
it, and I don't have to mess with separate resource bundles.

Well, I did a little experiment and it turns out that I can make a
TemplateMethodModel subclass that almost does what I want. The only thing
is that it receives its arguments as a List<String> so I have to preformat
the values in the template, but for now I think it'll do.

i18n_en_US.ftl:
<#assign dateAndTimeNow = "It is {0} on {1}." />

calling template:
<#setting locale = "en_US" />
<#import "i18n.ftl" as i18n />
${messageFormat(i18n.dateAndTimeNow, now?date, now?time)}

Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:06:34 PM:

> Wednesday, October 21, 2009, 6:00:03 PM, [hidden email] wrote:
>
> > I'm trying to use MessageFormat in combination with FM's localization
> > feature but I'm having some problems (which I won't go into at this
time).
> > My question is, are there features in FreeMarker (builtins, for
example)
> > that help with this and that may save me some effort? I don't see
anything

> > in the online manual, but maybe I'm missing something.
>
> freemarker.ext.beans.ResourceBundleModel deals with this, AFAIR.
> ResoureBundles should be wrapped into it automatically, so it's not
> like you should know about this class. Just drop a ResourceBundle into
> the data-model, then the resulting variable will be (copy-paste from
> the JavaDoc):
>
> A hash model that wraps a resource bundle. Makes it convenient to
> store localized content in the data model. It also acts as a method
> model that will take a resource key and arbitrary number of arguments
> and will apply MessageFormat with arguments on the string represented
> by the key.
>
> Typical usages:
>
>     * bundle.resourceKey will retrieve the object from resource bundle
>       with key resourceKey
>
>     * bundle("patternKey", arg1, arg2, arg3) will retrieve the string
>       from resource bundle with key patternKey, and will use it as a
>       pattern for MessageFormat with arguments arg1, arg2 and arg3
>
>
> --
> Best regards,
>  Daniel Dekany
>
>
>
------------------------------------------------------------------------------

> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink
Wednesday, October 21, 2009, 11:04:48 PM, [hidden email] wrote:

> I'll try this, although I was hoping to get away without ResourceBundles
> specifically. To clarify a bit, here's a quick overview of what I'm doing:
>
> I have all my localized strings in two files - i18n_en_US.ftl and
> i18n_es_MX.ftl. In my top-level template I have...
>
> <#import "i18n.ftl" as i18n />
>
> ...so the localization picks up the correct file based on the locale
> string, which is already set up earlier in the template. This works fine
> for fixed strings, of course. However I would like to be able to have
> MessageFormat pattern strings in those same files, such as:
>
> <#-- English version. i18n_es_MX.ftl has the Spanish translation -->
> <#assign dateAndTimeNow = "It is {0,time} on {0,date}." />
>
> Now in my actual template text body I would like to be able to do something
> like this, using a hypothetical "?message" builtin:
>
> <#assign now = "2009-10-20 14:38:00"?datetime("yyyy-MM-dd HH:mm:ss") />
> ${i18n.dateAndTimeNow?message(now)}
>
> In this case the hypothetical "?message" builtin would take the string as a
> MessageFormat pattern, and the bracketed parameter list ('now' in this
> case) as the argument list to be rendered using a call to
> java.text.MessageFormat. Nice for me - I just put my patterns in the same
> files as my other strings and use the localization feature to take care of
> it, and I don't have to mess with separate resource bundles.
>
> Well, I did a little experiment and it turns out that I can make a
> TemplateMethodModel subclass that almost does what I want.

Note that you can put it into i18n.ftl like:
<#assign messageFormat = "com.foo.baar.MyMethod"?new()>

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

> so I have to preformat
> the values in the template, but for now I think it'll do.
>
> i18n_en_US.ftl:
> <#assign dateAndTimeNow = "It is {0} on {1}." />
>
> calling template:
> <#setting locale = "en_US" />
> <#import "i18n.ftl" as i18n />
> ${messageFormat(i18n.dateAndTimeNow, now?date, now?time)}
>
> Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:06:34 PM:
>
>> Wednesday, October 21, 2009, 6:00:03 PM, [hidden email] wrote:
>>
>> > I'm trying to use MessageFormat in combination with FM's localization
>> > feature but I'm having some problems (which I won't go into at this
> time).
>> > My question is, are there features in FreeMarker (builtins, for
> example)
>> > that help with this and that may save me some effort? I don't see
> anything
>> > in the online manual, but maybe I'm missing something.
>>
>> freemarker.ext.beans.ResourceBundleModel deals with this, AFAIR.
>> ResoureBundles should be wrapped into it automatically, so it's not
>> like you should know about this class. Just drop a ResourceBundle into
>> the data-model, then the resulting variable will be (copy-paste from
>> the JavaDoc):
>>
>> A hash model that wraps a resource bundle. Makes it convenient to
>> store localized content in the data model. It also acts as a method
>> model that will take a resource key and arbitrary number of arguments
>> and will apply MessageFormat with arguments on the string represented
>> by the key.
>>
>> Typical usages:
>>
>>     * bundle.resourceKey will retrieve the object from resource bundle
>>       with key resourceKey
>>
>>     * bundle("patternKey", arg1, arg2, arg3) will retrieve the string
>>       from resource bundle with key patternKey, and will use it as a
>>       pattern for MessageFormat with arguments arg1, arg2 and arg3
>>
>>
>> --
>> Best regards,
>>  Daniel Dekany
>>
>>
>>
> ------------------------------------------------------------------------------
>
>> 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
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>
>
> ------------------------------------------------------------------------------
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Peter_Ford

Re: Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink


Daniel Dekany <[hidden email]> wrote on 10/22/2009 01:06:58 AM:

> Wednesday, October 21, 2009, 11:04:48 PM, [hidden email] wrote:
>
> > I'll try this, although I was hoping to get away without
ResourceBundles
> > specifically. To clarify a bit, here's a quick overview of what I'm
doing:
> >
> > I have all my localized strings in two files - i18n_en_US.ftl and
> > i18n_es_MX.ftl. In my top-level template I have...
> >
> > <#import "i18n.ftl" as i18n />
> >
> > ...so the localization picks up the correct file based on the locale
> > string, which is already set up earlier in the template. This works
fine
> > for fixed strings, of course. However I would like to be able to have
> > MessageFormat pattern strings in those same files, such as:
> >
> > <#-- English version. i18n_es_MX.ftl has the Spanish translation -->
> > <#assign dateAndTimeNow = "It is {0,time} on {0,date}." />
> >
> > Now in my actual template text body I would like to be able to do
something
> > like this, using a hypothetical "?message" builtin:
> >
> > <#assign now = "2009-10-20 14:38:00"?datetime("yyyy-MM-dd HH:mm:ss") />
> > ${i18n.dateAndTimeNow?message(now)}
> >
> > In this case the hypothetical "?message" builtin would take the string
as a
> > MessageFormat pattern, and the bracketed parameter list ('now' in this
> > case) as the argument list to be rendered using a call to
> > java.text.MessageFormat. Nice for me - I just put my patterns in the
same
> > files as my other strings and use the localization feature to take care
of
> > it, and I don't have to mess with separate resource bundles.
> >
> > Well, I did a little experiment and it turns out that I can make a
> > TemplateMethodModel subclass that almost does what I want.
>
> Note that you can put it into i18n.ftl like:
> <#assign messageFormat = "com.foo.baar.MyMethod"?new()>

Actually I have it set up as a shared variable when I create the
Configuration.

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

>
> > so I have to preformat
> > the values in the template, but for now I think it'll do.
> >
> > i18n_en_US.ftl:
> > <#assign dateAndTimeNow = "It is {0} on {1}." />
> >
> > calling template:
> > <#setting locale = "en_US" />
> > <#import "i18n.ftl" as i18n />
> > ${messageFormat(i18n.dateAndTimeNow, now?date, now?time)}
> >
> > Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:06:34 PM:
> >
> >> Wednesday, October 21, 2009, 6:00:03 PM, [hidden email] wrote:
> >>
> >> > I'm trying to use MessageFormat in combination with FM's
localization

> >> > feature but I'm having some problems (which I won't go into at this
> > time).
> >> > My question is, are there features in FreeMarker (builtins, for
> > example)
> >> > that help with this and that may save me some effort? I don't see
> > anything
> >> > in the online manual, but maybe I'm missing something.
> >>
> >> freemarker.ext.beans.ResourceBundleModel deals with this, AFAIR.
> >> ResoureBundles should be wrapped into it automatically, so it's not
> >> like you should know about this class. Just drop a ResourceBundle into
> >> the data-model, then the resulting variable will be (copy-paste from
> >> the JavaDoc):
> >>
> >> A hash model that wraps a resource bundle. Makes it convenient to
> >> store localized content in the data model. It also acts as a method
> >> model that will take a resource key and arbitrary number of arguments
> >> and will apply MessageFormat with arguments on the string represented
> >> by the key.
> >>
> >> Typical usages:
> >>
> >>     * bundle.resourceKey will retrieve the object from resource bundle
> >>       with key resourceKey
> >>
> >>     * bundle("patternKey", arg1, arg2, arg3) will retrieve the string
> >>       from resource bundle with key patternKey, and will use it as a
> >>       pattern for MessageFormat with arguments arg1, arg2 and arg3
> >>
> >>
> >> --
> >> Best regards,
> >>  Daniel Dekany
> >>
> >>
> >>
> >
>
------------------------------------------------------------------------------

> >
> >> 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
> >> [hidden email]
> >> https://lists.sourceforge.net/lists/listinfo/freemarker-user
> >
> >
> >
>
------------------------------------------------------------------------------

> > 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
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/freemarker-user
> >
>
> --
> Best regards,
>  Daniel Dekany
>
>
>
------------------------------------------------------------------------------

> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink
Thursday, October 22, 2009, 5:58:34 PM, [hidden email] wrote:

>
>
> Daniel Dekany <[hidden email]> wrote on 10/22/2009 01:06:58 AM:
>
>> Wednesday, October 21, 2009, 11:04:48 PM, [hidden email] wrote:
>>
>> > I'll try this, although I was hoping to get away without
> ResourceBundles
>> > specifically. To clarify a bit, here's a quick overview of what I'm
> doing:
>> >
>> > I have all my localized strings in two files - i18n_en_US.ftl and
>> > i18n_es_MX.ftl. In my top-level template I have...
>> >
>> > <#import "i18n.ftl" as i18n />
>> >
>> > ...so the localization picks up the correct file based on the locale
>> > string, which is already set up earlier in the template. This works
> fine
>> > for fixed strings, of course. However I would like to be able to have
>> > MessageFormat pattern strings in those same files, such as:
>> >
>> > <#-- English version. i18n_es_MX.ftl has the Spanish translation -->
>> > <#assign dateAndTimeNow = "It is {0,time} on {0,date}." />
>> >
>> > Now in my actual template text body I would like to be able to do
> something
>> > like this, using a hypothetical "?message" builtin:
>> >
>> > <#assign now = "2009-10-20 14:38:00"?datetime("yyyy-MM-dd HH:mm:ss") />
>> > ${i18n.dateAndTimeNow?message(now)}
>> >
>> > In this case the hypothetical "?message" builtin would take the string
> as a
>> > MessageFormat pattern, and the bracketed parameter list ('now' in this
>> > case) as the argument list to be rendered using a call to
>> > java.text.MessageFormat. Nice for me - I just put my patterns in the
> same
>> > files as my other strings and use the localization feature to take care
> of
>> > it, and I don't have to mess with separate resource bundles.
>> >
>> > Well, I did a little experiment and it turns out that I can make a
>> > TemplateMethodModel subclass that almost does what I want.
>>
>> Note that you can put it into i18n.ftl like:
>> <#assign messageFormat = "com.foo.baar.MyMethod"?new()>
>
> 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.

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

>> > so I have to preformat
>> > the values in the template, but for now I think it'll do.
>> >
>> > i18n_en_US.ftl:
>> > <#assign dateAndTimeNow = "It is {0} on {1}." />
>> >
>> > calling template:
>> > <#setting locale = "en_US" />
>> > <#import "i18n.ftl" as i18n />
>> > ${messageFormat(i18n.dateAndTimeNow, now?date, now?time)}
>> >
>> > Daniel Dekany <[hidden email]> wrote on 10/21/2009 02:06:34 PM:
>> >
>> >> Wednesday, October 21, 2009, 6:00:03 PM, [hidden email] wrote:
>> >>
>> >> > I'm trying to use MessageFormat in combination with FM's
> localization
>> >> > feature but I'm having some problems (which I won't go into at this
>> > time).
>> >> > My question is, are there features in FreeMarker (builtins, for
>> > example)
>> >> > that help with this and that may save me some effort? I don't see
>> > anything
>> >> > in the online manual, but maybe I'm missing something.
>> >>
>> >> freemarker.ext.beans.ResourceBundleModel deals with this, AFAIR.
>> >> ResoureBundles should be wrapped into it automatically, so it's not
>> >> like you should know about this class. Just drop a ResourceBundle into
>> >> the data-model, then the resulting variable will be (copy-paste from
>> >> the JavaDoc):
>> >>
>> >> A hash model that wraps a resource bundle. Makes it convenient to
>> >> store localized content in the data model. It also acts as a method
>> >> model that will take a resource key and arbitrary number of arguments
>> >> and will apply MessageFormat with arguments on the string represented
>> >> by the key.
>> >>
>> >> Typical usages:
>> >>
>> >>     * bundle.resourceKey will retrieve the object from resource bundle
>> >>       with key resourceKey
>> >>
>> >>     * bundle("patternKey", arg1, arg2, arg3) will retrieve the string
>> >>       from resource bundle with key patternKey, and will use it as a
>> >>       pattern for MessageFormat with arguments arg1, arg2 and arg3
>> >>
>> >>
>> >> --
>> >> Best regards,
>> >>  Daniel Dekany
>> >>
>> >>
>> >>
>> >
>>
> ------------------------------------------------------------------------------
>
>> >
>> >> 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
>> >> [hidden email]
>> >> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>> >
>> >
>> >
>>
> ------------------------------------------------------------------------------
>
>> > 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
>> > [hidden email]
>> > https://lists.sourceforge.net/lists/listinfo/freemarker-user
>> >
>>
>> --
>> Best regards,
>>  Daniel Dekany

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Peter_Ford

Re: Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink

Daniel Dekany <[hidden email]> wrote on 10/22/2009 10:09:20 AM:

> Thursday, October 22, 2009, 5:58:34 PM, [hidden email] 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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany

Re: Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink
Friday, October 23, 2009, 10:58:22 PM, [hidden email] wrote:

>
> Daniel Dekany <[hidden email]> wrote on 10/22/2009 10:09:20 AM:
>
>> Thursday, October 22, 2009, 5:58:34 PM, [hidden email] 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).
[snip]

What I'm saying is that you have a #include-ed or #import-ed file
somewhere anyway, and you need that TemplateMethodModel modell class
in the "class path" somewhere too. That's already two things to care
about. Adding a shared variable, which happens on a yet another
separate place, is a third thing to care about. But, it's needless,
because you can just pull that TemplateMethodModel inside that
#include-ed/#import-ed file, and that's it. One less thing that can
break later... It's a maintenance thing.

--
Best regards,
 Daniel Dekany


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Peter_Ford

Re: Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink
Building the app so that ant TemplateModel classes are in the classpath
isn't a problem - it's essentially automatic. Adding TemplateMethodModel
and TemplateDirectiveModel instances to the FreeMarker shared variables via
the Spring configuration gives a number of advantages - for example, I have
beans containing helper classes for i18n & localization that are used by
the Spring controllers and are also useful during the View phase, and being
able to use Spring's dependency injection to make these available to those
TemplateXModel instances is a better solution for a number of reasons -
everything is gathered in one XML configuration file, the components are
instantiated once and once only during application startup and not
recreated for every template render, we don't have obscure assignments
hidden away in various template files, and I don't have to worry about my
developers forgetting the necessary include/import statements in their FTL
files. Sure, doing things the other way has value but at the end of the day
the options are open and it's up to the designer to figure out what's best
for a specific application. In this case, I'm confident that making use of
the Spring features is the better way to go.

To get back to the problem I was having, it turns out that you can
configure all the FreeMarker settings using the "setFreemarkerSettings()"
and "setFreemarkerVariables()" methods in the Spring MVC FreeMarker
Configurer (I was trying to use "settings" rather than "freemarkerSettings"
as the property name in the Spring XML - no wonder it didn't work). With
that corrected my application is now working correctly.

Daniel Dekany <[hidden email]> wrote on 10/23/2009 05:19:08 PM:

> Friday, October 23, 2009, 10:58:22 PM, [hidden email] wrote:
>
> >
> > Daniel Dekany <[hidden email]> wrote on 10/22/2009 10:09:20 AM:
> >
> >> Thursday, October 22, 2009, 5:58:34 PM, [hidden email] 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).
> [snip]
>
> What I'm saying is that you have a #include-ed or #import-ed file
> somewhere anyway, and you need that TemplateMethodModel modell class
> in the "class path" somewhere too. That's already two things to care
> about. Adding a shared variable, which happens on a yet another
> separate place, is a third thing to care about. But, it's needless,
> because you can just pull that TemplateMethodModel inside that
> #include-ed/#import-ed file, and that's it. One less thing that can
> break later... It's a maintenance thing.
>
> --
> Best regards,
>  Daniel Dekany
>
>
>
------------------------------------------------------------------------------

> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Attila Szegedi-3

Re: Is MessageFormat supported?

Reply Threaded More More options
Print post
Permalink
In reply to this post by Peter_Ford
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, [hidden email] wrote:

>
> Daniel Dekany <[hidden email]> wrote on 10/22/2009 10:09:20 AM:
>
>> Thursday, October 22, 2009, 5:58:34 PM, [hidden email] 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
> [hidden email]
> 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
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user