use of com.squarespace.template.Variable in project template-compiler by Squarespace.
the class DateTimeFormatter method apply.
@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
Variable var = variables.first();
long epoch = var.node().asLong();
String zoneId = PluginDateUtils.getTimeZoneNameFromContext(ctx);
CLDR cldr = ctx.cldr();
DateFormatOptions options = (DateFormatOptions) args.getOpaque();
CalendarDate date = cldr.Calendars.toGregorianDate(epoch, zoneId);
String result = cldr.Calendars.formatDate(date, options);
var.set(result);
}
use of com.squarespace.template.Variable in project template-compiler by Squarespace.
the class DecimalFormatter method apply.
@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
Variable var = variables.first();
Decimal number = GeneralUtils.nodeToDecimal(var.node());
if (number == null) {
var.setMissing();
return;
}
CLDR cldr = ctx.cldr();
DecimalFormatOptions opts = (DecimalFormatOptions) args.getOpaque();
String result = cldr.Numbers.formatDecimal(number, opts);
var.set(result);
}
use of com.squarespace.template.Variable in project template-compiler by Squarespace.
the class LegacyMoneyFormatter method apply.
@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
Variable var = variables.first();
JsonNode node = var.node();
Locale locale = (Locale) args.getOpaque();
Currency currency = getCurrency(node);
double value = node.path(VALUE_FIELD_NAME).asDouble(0);
String result = LegacyMoneyFormatFactory.create(locale, currency).format(value);
var.set(result);
}
use of com.squarespace.template.Variable in project template-compiler by Squarespace.
the class MessageFormatter method apply.
@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
Variable var = variables.first();
JsonNode node = var.node();
String zoneId = PluginDateUtils.getTimeZoneNameFromContext(ctx);
MessageArgs msgargs = messageArgs(args, ctx);
MessageFormats formats = ctx.messageFormatter();
formats.setTimeZone(zoneId);
String message = node.asText();
String result = formats.formatter().format(message, msgargs);
var.set(result);
}
use of com.squarespace.template.Variable in project template-compiler by Squarespace.
the class UnitFormatter method apply.
@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
Variable var = variables.first();
BigDecimal amount = GeneralUtils.nodeToBigDecimal(var.node());
if (amount == null) {
var.setMissing();
return;
}
UnitFormatterOptions opts = (UnitFormatterOptions) args.getOpaque();
CLDR.Locale locale = ctx.cldrLocale();
Unit inputUnit = opts.inputUnit;
Unit exactUnit = opts.exactUnit;
Unit[] exactUnits = opts.exactUnits;
String compact = opts.compact;
Unit[] sequence = opts.sequence;
// We default to output unit.
if (inputUnit == null && exactUnit == null) {
UnitValue value = new UnitValue(amount, null);
StringBuilder buf = new StringBuilder();
NumberFormatter formatter = CLDR_INSTANCE.getNumberFormatter(locale);
formatter.formatUnit(value, buf, opts.options);
var.set(buf);
return;
}
// At least one argument was provided. We will try to infer the others where possible.
UnitConverter converter = CLDR_INSTANCE.getUnitConverter(locale);
UnitFactorSet factorSet = null;
if (compact != null) {
// First see if compact format matches a direct unit conversion (e.g. temperature, speed)
Unit unit = selectExactUnit(compact, converter);
if (unit != null) {
exactUnit = unit;
} else if (opts.factorSet != null) {
// Compact format might correspond to a factor set (e.g. digital bits, bytes).
factorSet = opts.factorSet;
} else {
factorSet = selectFactorSet(compact, converter);
opts.factorSet = factorSet;
}
} else if (exactUnits != null && exactUnits.length > 0) {
if (opts.factorSet != null) {
factorSet = opts.factorSet;
} else {
UnitCategory category = exactUnits[0].category();
factorSet = converter.getFactorSet(category, exactUnits);
opts.factorSet = factorSet;
}
} else if (sequence != null && sequence.length > 0) {
if (opts.factorSet != null) {
factorSet = opts.factorSet;
} else {
UnitCategory category = sequence[0].category();
factorSet = converter.getFactorSet(category, sequence);
opts.factorSet = factorSet;
}
}
// Make sure we know what the input units are.
if (inputUnit == null) {
inputUnit = inputFromExactUnit(exactUnit, converter);
}
// == CONVERSION ==
UnitValue value = new UnitValue(amount, inputUnit);
// In sequence mode this will get set below.
List<UnitValue> values = null;
if (exactUnit != null) {
// Convert to exact units using the requested unit.
value = converter.convert(value, exactUnit);
} else if (factorSet == null) {
// Convert directly to "best" unit using the default built-in factor sets.
value = converter.convert(value);
} else if (compact != null || exactUnits != null) {
// Use the factor set to build a compact form.
value = converter.convert(value, factorSet);
} else if (sequence != null) {
// Use the factor set to produce a sequence.
values = converter.sequence(value, factorSet);
}
// == FORMATTING ==
StringBuilder buf = new StringBuilder();
NumberFormatter formatter = CLDR_INSTANCE.getNumberFormatter(locale);
if (values == null) {
formatter.formatUnit(value, buf, opts.options);
} else {
formatter.formatUnits(values, buf, opts.options);
}
var.set(buf);
}
Aggregations