use of com.squarespace.cldrengine.api.CurrencyType 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.CurrencyType 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);
}
Aggregations