MonetaryFormatter.java

package org.freshmarker.money;

import org.freshmarker.core.formatter.Formatter;
import org.freshmarker.core.formatter.LocaleLocal;
import org.freshmarker.core.model.TemplateObject;

import javax.money.format.MonetaryAmountFormat;
import javax.money.format.MonetaryFormats;
import java.util.Locale;

public class MonetaryFormatter implements Formatter {
    private final LocaleLocal<MonetaryAmountFormat> localeLocal = LocaleLocal.withInitial(MonetaryFormats::getAmountFormat);

    @Override
    public String format(TemplateObject object, Locale locale) {
        return localeLocal.get(locale).format(((TemplateMoney) object).getValue());
    }
}