0
votes

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).

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):

// 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). 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. I have perused the pertinent Freemarker documentation, but I am still unclear on how to do this or if it is even possible.

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.

1

1 Answers

0
votes

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. 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().