简体   繁体   English

我的 Freemarker ObjectWrapper 如何访问模板设置

[英]How can my Freemarker ObjectWrapper access a template setting

Use case: system administrator stores a Freemarker template in a database which is used (by Spring Boot REST API) to present information stored by system users (respondents) in a locale-aware way to a different user type (reviewer).用例:系统管理员将 Freemarker 模板存储在数据库中,该模板用于(由 Spring 引导 REST API)以区域设置感知方式将系统用户(响应者)存储的信息呈现给不同的用户类型(审阅者)。

A respondent's response might be stored in this sort of object (or in lists of this sort of object, in the event a question posed to the respondent is expected to have multiple answers):受访者的回答可能存储在此类 object 中(或此类 object 的列表中,如果向受访者提出的问题预计会有多个答案):

// snip
import com.fasterxml.jackson.databind.node.ObjectNode;
// more imports snipped

public class LanguageStringMap {
    private Map<Language, String> languageStringMap;

    public LanguageStringMap(ObjectNode languageMapNode) {
        // snip of code instantiating a LanguageStringMap from JSON
    }

    public void put(Language language, String value) {
        if (value.length() == 0)
            throw new IllegalArgumentException(String.format(
                    "value for language '%s' of zero length", language.getCode()));
        languageStringMap.put(language, value);
    }

    public String get(Language language) { return languageStringMap.get(language); }
}

What I think I want to do is write an ObjectWrapper that maps instances of LanguageStringMap to a string (obtained by calling the get() method with a language derived from the Locale requested by the reviewer's browser and set in the template's settings).我想我想要做的是编写一个 ObjectWrapper,它将LanguageStringMap的实例映射到一个字符串(通过调用get()方法获得,该方法使用从审阅者的浏览器请求的语言环境派生的语言并在模板的设置中设置)。 This presents a cleaner user experience to the system administrator than making the uploaded template contain a bunch of template method calls would.与使上传的模板包含一堆模板方法调用相比,这为系统管理员提供了更清晰的用户体验。

To do this, my object wrapper needs to access a template setting.为此,我的 object 包装器需要访问模板设置。 I have perused the pertinent Freemarker documentation , but I am still unclear on how to do this or if it is even possible.我已经仔细阅读了相关的 Freemarker 文档,但我仍然不清楚如何做到这一点,甚至是否可能。

I think it would be a mistake to try to implement this with resource bundles uploaded to the database alongside the templates, but that is a consideration.我认为尝试通过将资源包与模板一起上传到数据库来实现这一点是错误的,但这是一个考虑因素。

Typically you simply put the locale specific string into the data-model before the template is processed, along with all the other variables.通常,您只需在处理模板之前将特定于语言环境的字符串与所有其他变量一起放入数据模型中。 In that case no ObjectWrapper customization is needed.在这种情况下,不需要ObjectWrapper自定义。 But if you have to use an ObjectWrapper -based solution, then you can get the locale inside an ObjectWrapper method (like in the override of DefaultObjectWrapper.handleUnknownType ) with Environment.getCurrentEnvironment().getLocale() .但是,如果您必须使用基于ObjectWrapper的解决方案,那么您可以使用Environment.getCurrentEnvironment().getLocale()ObjectWrapper方法中获取语言环境(例如在DefaultObjectWrapper.handleUnknownType的覆盖中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM