Search in sources :

Example 6 with Decimal

use of com.squarespace.cldrengine.api.Decimal 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 7 with Decimal

use of com.squarespace.cldrengine.api.Decimal 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)

Example 8 with Decimal

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

the class PluginUtilsTest method testFormatMoneyCLDR.

@Test
public void testFormatMoneyCLDR() {
    CLDR en = CLDR.get("en-US");
    assertEquals(PluginUtils.formatMoney(new Decimal(1), "USD", en), "$1.00");
    assertEquals(PluginUtils.formatMoney(new Decimal(123.45), "USD", en), "$123.45");
    CLDR de = CLDR.get("de");
    assertEquals(PluginUtils.formatMoney(new Decimal(123.45), "USD", de), "123,45 $");
    assertEquals(PluginUtils.formatMoney(new Decimal(123.45), "EUR", de), "123,45 €");
}
Also used : Decimal(com.squarespace.cldrengine.api.Decimal) CLDR(com.squarespace.cldrengine.CLDR) Test(org.testng.annotations.Test)

Aggregations

Decimal (com.squarespace.cldrengine.api.Decimal)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 CLDR (com.squarespace.cldrengine.CLDR)3 ProductType (com.squarespace.template.plugins.platform.enums.ProductType)3 CurrencyFormatOptions (com.squarespace.cldrengine.api.CurrencyFormatOptions)2 CurrencyType (com.squarespace.cldrengine.api.CurrencyType)2 DecimalFormatOptions (com.squarespace.cldrengine.api.DecimalFormatOptions)2 Variable (com.squarespace.template.Variable)2 Test (org.testng.annotations.Test)1