Search in sources :

Example 1 with Variable

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);
}
Also used : CalendarDate(com.squarespace.cldrengine.api.CalendarDate) Variable(com.squarespace.template.Variable) DateFormatOptions(com.squarespace.cldrengine.api.DateFormatOptions) CLDR(com.squarespace.cldrengine.CLDR)

Example 2 with Variable

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);
}
Also used : Variable(com.squarespace.template.Variable) Decimal(com.squarespace.cldrengine.api.Decimal) CLDR(com.squarespace.cldrengine.CLDR) DecimalFormatOptions(com.squarespace.cldrengine.api.DecimalFormatOptions)

Example 3 with Variable

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);
}
Also used : Locale(java.util.Locale) Variable(com.squarespace.template.Variable) Currency(java.util.Currency) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 4 with Variable

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);
}
Also used : MessageArgs(com.squarespace.cldrengine.api.MessageArgs) GeneralUtils.splitVariable(com.squarespace.template.GeneralUtils.splitVariable) Variable(com.squarespace.template.Variable) JsonNode(com.fasterxml.jackson.databind.JsonNode) MessageFormats(com.squarespace.template.MessageFormats)

Example 5 with Variable

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);
}
Also used : Variable(com.squarespace.template.Variable) UnitFormatterOptions(com.squarespace.template.plugins.platform.i18n.UnitUtils.UnitFormatterOptions) CLDR(com.squarespace.cldr.CLDR) UnitValue(com.squarespace.cldr.units.UnitValue) Unit(com.squarespace.cldr.units.Unit) BigDecimal(java.math.BigDecimal) UnitFactorSet(com.squarespace.cldr.units.UnitFactorSet) UnitConverter(com.squarespace.cldr.units.UnitConverter) UnitCategory(com.squarespace.cldr.units.UnitCategory) NumberFormatter(com.squarespace.cldr.numbers.NumberFormatter)

Aggregations

Variable (com.squarespace.template.Variable)8 CLDR (com.squarespace.cldrengine.CLDR)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 CalendarDate (com.squarespace.cldrengine.api.CalendarDate)3 Decimal (com.squarespace.cldrengine.api.Decimal)2 CLDR (com.squarespace.cldr.CLDR)1 NumberFormatter (com.squarespace.cldr.numbers.NumberFormatter)1 Unit (com.squarespace.cldr.units.Unit)1 UnitCategory (com.squarespace.cldr.units.UnitCategory)1 UnitConverter (com.squarespace.cldr.units.UnitConverter)1 UnitFactorSet (com.squarespace.cldr.units.UnitFactorSet)1 UnitValue (com.squarespace.cldr.units.UnitValue)1 CurrencyFormatOptions (com.squarespace.cldrengine.api.CurrencyFormatOptions)1 CurrencyType (com.squarespace.cldrengine.api.CurrencyType)1 DateFormatOptions (com.squarespace.cldrengine.api.DateFormatOptions)1 DateIntervalFormatOptions (com.squarespace.cldrengine.api.DateIntervalFormatOptions)1 DecimalFormatOptions (com.squarespace.cldrengine.api.DecimalFormatOptions)1 MessageArgs (com.squarespace.cldrengine.api.MessageArgs)1 RelativeTimeFormatOptions (com.squarespace.cldrengine.api.RelativeTimeFormatOptions)1 GeneralUtils.splitVariable (com.squarespace.template.GeneralUtils.splitVariable)1