Search in sources :

Example 1 with CurrencyFormatOptions

use of com.squarespace.cldrengine.api.CurrencyFormatOptions in project template-compiler by Squarespace.

the class MoneyFormatter method apply.

@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
    Variable var = variables.first();
    JsonNode node = var.node();
    JsonNode decimalValue = node.path("decimalValue");
    JsonNode currencyNode = node.path("currencyCode");
    if (decimalValue.isMissingNode() || currencyNode.isMissingNode()) {
        // Check if we have a new money node and CLDR formatting is enabled.
        if (CommerceUtils.useCLDRMode(ctx)) {
            decimalValue = node.path("value");
            currencyNode = node.path("currency");
        }
        // No valid money node found.
        if (decimalValue.isMissingNode() || currencyNode.isMissingNode()) {
            var.setMissing();
            return;
        }
    }
    CLDR cldr = ctx.cldr();
    String code = currencyNode.asText();
    Decimal decimal = GeneralUtils.nodeToDecimal(decimalValue);
    CurrencyType currency = CurrencyType.fromString(code);
    CurrencyFormatOptions opts = (CurrencyFormatOptions) args.getOpaque();
    String result = cldr.Numbers.formatCurrency(decimal, currency, opts);
    var.set(result);
}
Also used : Variable(com.squarespace.template.Variable) Decimal(com.squarespace.cldrengine.api.Decimal) CurrencyFormatOptions(com.squarespace.cldrengine.api.CurrencyFormatOptions) CLDR(com.squarespace.cldrengine.CLDR) JsonNode(com.fasterxml.jackson.databind.JsonNode) CurrencyType(com.squarespace.cldrengine.api.CurrencyType)

Example 2 with CurrencyFormatOptions

use of com.squarespace.cldrengine.api.CurrencyFormatOptions in project template-compiler by Squarespace.

the class MoneyFormatter method validateArgs.

@Override
public void validateArgs(Arguments args) throws ArgumentsException {
    CurrencyFormatOptions opts = OptionParsers.currency(args);
    args.setOpaque(opts);
}
Also used : CurrencyFormatOptions(com.squarespace.cldrengine.api.CurrencyFormatOptions)

Example 3 with CurrencyFormatOptions

use of com.squarespace.cldrengine.api.CurrencyFormatOptions in project template-compiler by Squarespace.

the class OptionParsers method currency.

public static CurrencyFormatOptions currency(List<String> args) {
    CurrencyFormatOptions options = new CurrencyFormatOptions();
    parse(args, options, OptionParsers::currencyOption);
    return options;
}
Also used : CurrencyFormatOptions(com.squarespace.cldrengine.api.CurrencyFormatOptions)

Example 4 with CurrencyFormatOptions

use of com.squarespace.cldrengine.api.CurrencyFormatOptions in project template-compiler by Squarespace.

the class MessageFormats method currency.

/**
 * Currency message formatter.
 */
private String currency(List<Object> args, List<String> options) {
    if (args.isEmpty()) {
        return "";
    }
    JsonNode node = (JsonNode) args.get(0);
    if (node == null) {
        return "";
    }
    JsonNode decimalValue = node.path("decimalValue");
    JsonNode currencyCode = node.path("currencyCode");
    if (decimalValue.isMissingNode() || currencyCode.isMissingNode()) {
        decimalValue = node.path("value");
        currencyCode = node.path("currency");
    }
    if (decimalValue.isMissingNode() || currencyCode.isMissingNode()) {
        return "";
    }
    Decimal value = this.converter.asDecimal(decimalValue);
    String code = this.converter.asString(currencyCode);
    CurrencyType currency = CurrencyType.fromString(code);
    CurrencyFormatOptions opts = OptionParsers.currency(options);
    return cldr.Numbers.formatCurrency(value, currency, opts);
}
Also used : Decimal(com.squarespace.cldrengine.api.Decimal) CurrencyFormatOptions(com.squarespace.cldrengine.api.CurrencyFormatOptions) JsonNode(com.fasterxml.jackson.databind.JsonNode) CurrencyType(com.squarespace.cldrengine.api.CurrencyType)

Aggregations

CurrencyFormatOptions (com.squarespace.cldrengine.api.CurrencyFormatOptions)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 CurrencyType (com.squarespace.cldrengine.api.CurrencyType)2 Decimal (com.squarespace.cldrengine.api.Decimal)2 CLDR (com.squarespace.cldrengine.CLDR)1 Variable (com.squarespace.template.Variable)1