سؤال

I'm developing a Java app for IBM Connections, and my app needs to produce and send template-based emails.
Connections includes several Freemarker templates which use resource bundles. I want my code to use copies of these with minimal changes, but I've never used Freemarker before.

In my Java code, how do I associate resource bundles with Freemarker templates in such a way that the existing templates work?

The templates and resource bundles are in this directory structure:

notifications (directory)
-> activities (directory)
  -> resources (directory)
    -> nls (directory)
      -> properties files
  -> Template FTL files
-> resources (directory)
  -> nls (directory)
    -> properties files
  -> Imported FTL files

One of the main template files is "notifyMail.ftl". Some lines of particular interest in that file are:

<#import "*/resources/commonStructure.ftl" as s>
<#import "*/resources/commonUtil.ftl" as u>
<#import "*/resources/commonUrlUtil.ftl" as urlUtil>
<#lt><@s.header>${u.resource("email.notify.body."+"${key}","${activity.event.sender.display.name}",urlUtil.linkifyItem("${activity.node.permalink}", "${activity.node.name}"))}</@s.header>

The "commonUtil.ftl" file declares two functions that use resource bundles, show below.
The first function uses members of something called "__parameters".
I'm assuming that needs to be passed to Freemarker in the Java code, as I don't see it defined anywhere in the templates.

<#function resource messageKey params...>
    <#if __parameters.__resourceBundle?keys?seq_contains(messageKey)>
        <#local bundleString = bundleResource(__parameters.__resourceBundle,messageKey,params) />
    <#elseif __parameters.__sharedBundle?keys?seq_contains(messageKey)>
        <#local bundleString = bundleResource(__parameters.__sharedBundle,messageKey,params) />
    <#else>
        <#return messageKey /> <#-- message key not found, return the key back -->
    </#if>

    <#if bundleString??>
        <#return bundleString />
    <#else>
        <#return messageKey />
    </#if>
</#function>

<#function bundleResource bundle messageKey params>
<#if bundle??>
<#switch params?size>
<#case 0>
    <#return bundle(messageKey)>
    <#break>
<#case 1>
    <#return bundle(messageKey, params[0])>
    <#break>
<#case 2>
    <#return bundle(messageKey, params[0], params[1])>
    <#break>
<#case 3>
    <#return bundle(messageKey, params[0], params[1], params[2])>
    <#break>
<#case 4>
    <#return bundle(messageKey, params[0], params[1], params[2], params[3])>
    <#break>
<#case 5>
    <#return bundle(messageKey, params[0], params[1], params[2], params[3], params[4])>
    <#break>
<#default>
    <#stop "resource function doesn't support more than 5 parameters for a message due to language reason. And it's seldom to have more than 5 parameters in a message. However, you can extend the limit by changing the function if you really want to."/>
</#switch>
</#if>
</#function>
هل كانت مفيدة؟

المحلول

The solution to this turned out to be simple, though I had a lot of trouble getting message formatting to work correctly due to problems with my properties files.

The Map I send to freemarker.template.Template.process() simply needs ResourceBundle instances in the same hierarchy used by the FTL files.

E.g. Where the FTL file has this:

__parameters.__resourceBundle

I have a notification.properties file with my Java source, and added it to the map sent to Freemarker like this:

HashMap tmplParams=new HashMap();

tmplParams.put("__resourceBundle",ResourceBundle.getBundle(
    "<parent directory path>.activities.resources.nls.notification"));

root.put("__parameters",tmplParams);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top