在 FreeMarker 中使用相同模板在两个 .properties 之间切换

Changing between two .properties with a same template in FreeMarker

我是 FreeMarker 的新手,我想完成的是使用相同的模板文件在不同的属性之间切换,以便发送不同语言的电子邮件。

我的默认属性文件是 messages.properties,我想使用的第二个文件(非默认)是 messages_fr.properties。

模板文件是template.ftl

我的密码是:

Map<String, Object> content = new HashMap<String, Object>();
    content.put(I18N, i18nService);
    content.put("someText", "a text";
    content.put("language", language);

    String setBodyTextMessage = commonService.getProcessedTemplate("template.ftl", content);

我正在努力寻找的是如何做与我提供的代码相同的事情,但是通过使用 messages_fr.properties 而不更改配置文件(我想在Java 代码)。此外,设置不同的语言环境也不起作用。

那里有一个数据模型,称为 content。您可以使用 Java 代码将任何内容放入其中。每个模板处理都可以使用自己的数据模型。所以我不确定你卡在哪里了。

或者,您可以创建一个 ResourceBundle 并将其放入数据模型(甚至作为 Configuration 级别的共享变量),然后设置 locale 设置对于单个请求(不在共享 Configuration 对象中)。为此,请使用 Template.createProcessingEnvironment 而不是 Configuration.getTemplate。在返回的 freemarker.core.Environment 中,您可以设置语言环境(和其他设置),然后调用 Environment.process 生成输出。