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);
}
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);
}
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 €");
}
Aggregations