Search in sources :

Example 1 with CLDR

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

the class PluginDateUtilsTest method formatDate.

private String formatDate(String format, long timestamp, String tzId, Locale locale) {
    CLDR cldr = CLDR.get(locale);
    StringBuilder buf = new StringBuilder();
    PluginDateUtils.formatDate(cldr, format, timestamp, tzId, buf);
    return buf.toString();
}
Also used : CLDR(com.squarespace.cldrengine.CLDR)

Example 2 with CLDR

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

the class MessageFormatsTest method testDecimal.

@Test
public void testDecimal() {
    String actual;
    CLDR cldr = CLDR.get("en");
    MessageFormats formats = new MessageFormats(cldr);
    formats.setTimeZone("America/New_York");
    String message = "{0 decimal style:short}";
    actual = formats.formatter().format(message, args().add(new TextNode("12345.789")));
    assertEquals(actual, "12K");
}
Also used : CLDR(com.squarespace.cldrengine.CLDR) TextNode(com.fasterxml.jackson.databind.node.TextNode) MessageFormats(com.squarespace.template.MessageFormats) Test(org.testng.annotations.Test)

Example 3 with CLDR

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

the class MessageFormatsTest method testDateTime.

@Test
public void testDateTime() {
    String actual;
    CLDR cldr = CLDR.get("en");
    MessageFormats formats = new MessageFormats(cldr);
    formats.setTimeZone("America/New_York");
    String message = "{0 datetime date:long time:medium}";
    long epoch = 1582129775000L;
    actual = formats.formatter().format(message, args().add(new LongNode(epoch)));
    assertEquals(actual, "February 19, 2020 at 11:29:35 AM");
}
Also used : CLDR(com.squarespace.cldrengine.CLDR) LongNode(com.fasterxml.jackson.databind.node.LongNode) MessageFormats(com.squarespace.template.MessageFormats) Test(org.testng.annotations.Test)

Example 4 with CLDR

use of com.squarespace.cldrengine.CLDR 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 5 with CLDR

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

Aggregations

CLDR (com.squarespace.cldrengine.CLDR)13 Test (org.testng.annotations.Test)7 MessageFormats (com.squarespace.template.MessageFormats)6 Variable (com.squarespace.template.Variable)5 LongNode (com.fasterxml.jackson.databind.node.LongNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 CalendarDate (com.squarespace.cldrengine.api.CalendarDate)3 Decimal (com.squarespace.cldrengine.api.Decimal)3 TextNode (com.fasterxml.jackson.databind.node.TextNode)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 DecimalNode (com.fasterxml.jackson.databind.node.DecimalNode)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 BigDecimal (java.math.BigDecimal)1