gpt4 book ai didi

java - 如何使资源包与现有 Freemarker 模板配合使用?

转载 作者:行者123 更新时间:2023-11-30 03:57:33 27 4
gpt4 key购买 nike

我正在为 IBM Connections 开发一个 Java 应用程序,我的应用程序需要生成和发送基于模板的电子邮件。
Connections 包括多个使用资源包的 Freemarker 模板。我希望我的代码能够使用这些代码的副本并进行最小的更改,但我以前从未使用过 Freemarker。

在我的 Java 代码中,如何将资源包与 Freemarker 模板关联起来,以便现有模板可以工作?

模板和资源包位于以下目录结构中:

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>

“commonUtil.ftl”文件声明了两个使用资源包的函数,如下所示。
第一个函数使用名为“__parameters”的成员。
我假设需要在 Java 代码中传递给 Freemarker,因为我没有看到它在模板中的任何地方定义。

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

最佳答案

事实证明,这个问题的解决方案很简单,尽管由于属性文件的问题,我在使消息格式正确工作方面遇到了很多麻烦。

我发送到 freemarker.template.Template.process()Map 只需要 FTL 使用的同一层次结构中的 ResourceBundle 实例文件。

例如FTL 文件中包含以下内容:

__parameters.__resourceBundle

我有一个包含 Java 源代码的 notification.properties 文件,并将其添加到发送到 Freemarker 的 map 中,如下所示:

HashMap tmplParams=new HashMap();

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

root.put("__parameters",tmplParams);

关于java - 如何使资源包与现有 Freemarker 模板配合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22799877/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com