Re: Is MessageFormat supported?
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Wednesday, October 21, 2009, 11:04:48 PM, [email protected] 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 <[email protected]> wrote on 10/21/2009 02:06:34 PM: > >> Wednesday, October 21, 2009, 6:00:03 PM, [email protected] 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 >> [email protected] >> 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 > [email protected] > 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