Search in sources :

Example 6 with CLDR

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

the class DateTimeIntervalFormatter method apply.

@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
    int count = variables.count();
    if (count < 2) {
        return;
    }
    Variable v1 = variables.get(0);
    Variable v2 = variables.get(1);
    CLDR cldr = ctx.cldr();
    String zoneId = PluginDateUtils.getTimeZoneNameFromContext(ctx);
    CalendarDate start = cldr.Calendars.toGregorianDate(v1.node().asLong(0), zoneId);
    CalendarDate end = cldr.Calendars.toGregorianDate(v2.node().asLong(0), zoneId);
    DateIntervalFormatOptions options = (DateIntervalFormatOptions) args.getOpaque();
    String result = cldr.Calendars.formatDateInterval(start, end, options);
    v1.set(result);
}
Also used : DateIntervalFormatOptions(com.squarespace.cldrengine.api.DateIntervalFormatOptions) CalendarDate(com.squarespace.cldrengine.api.CalendarDate) Variable(com.squarespace.template.Variable) CLDR(com.squarespace.cldrengine.CLDR)

Example 7 with CLDR

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

the class RelativeTimeFormatter method apply.

@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
    int count = variables.count();
    Variable v1 = variables.get(0);
    Long now = ctx.now();
    long s = now == null ? System.currentTimeMillis() : now.longValue();
    long e = v1.node().asLong();
    if (count > 1) {
        s = e;
        e = variables.get(1).node().asLong();
    }
    CLDR cldr = ctx.cldr();
    CalendarDate start = cldr.Calendars.toGregorianDate(s, "UTC");
    CalendarDate end = cldr.Calendars.toGregorianDate(e, "UTC");
    RelativeTimeFormatOptions opts = OptionParsers.relativetime(args);
    String res = cldr.Calendars.formatRelativeTime(start, end, opts);
    v1.set(res);
}
Also used : CalendarDate(com.squarespace.cldrengine.api.CalendarDate) Variable(com.squarespace.template.Variable) CLDR(com.squarespace.cldrengine.CLDR) RelativeTimeFormatOptions(com.squarespace.cldrengine.api.RelativeTimeFormatOptions)

Example 8 with CLDR

use of com.squarespace.cldrengine.CLDR 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);
}
Also used : Variable(com.squarespace.template.Variable) Decimal(com.squarespace.cldrengine.api.Decimal) CurrencyFormatOptions(com.squarespace.cldrengine.api.CurrencyFormatOptions) CLDR(com.squarespace.cldrengine.CLDR) JsonNode(com.fasterxml.jackson.databind.JsonNode) CurrencyType(com.squarespace.cldrengine.api.CurrencyType)

Example 9 with CLDR

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

the class MessageFormatsTest method testBasicArgs.

@Test
public void testBasicArgs() {
    String actual;
    CLDR cldr = CLDR.get("en");
    MessageFormats formats = new MessageFormats(cldr);
    formats.setTimeZone("America/New_York");
    String message = "{0 select abc {ABC} def {DEF} true {T} false {F}}";
    actual = formats.formatter().format(message, args().add(new TextNode("abc")));
    assertEquals(actual, "ABC");
    actual = formats.formatter().format(message, args().add(new TextNode("def")));
    assertEquals(actual, "DEF");
    actual = formats.formatter().format(message, args().add(new TextNode("ghi")));
    assertEquals(actual, "");
    actual = formats.formatter().format(message, args().add(new LongNode(123)));
    assertEquals(actual, "");
    actual = formats.formatter().format(message, args().add(BooleanNode.TRUE));
    assertEquals(actual, "T");
    actual = formats.formatter().format(message, args().add(BooleanNode.FALSE));
    assertEquals(actual, "F");
    actual = formats.formatter().format(message, args().add(NullNode.getInstance()));
    assertEquals(actual, "");
    actual = formats.formatter().format(message, args().add(MissingNode.getInstance()));
    assertEquals(actual, "");
    actual = formats.formatter().format(message, args().add(JsonUtils.createArrayNode()));
    assertEquals(actual, "");
    actual = formats.formatter().format(message, args().add(JsonUtils.createObjectNode()));
    assertEquals(actual, "");
    message = "{0 plural one {ONE} other {OTHER}}";
    actual = formats.formatter().format(message, args().add("123"));
    assertEquals(actual, "OTHER");
    actual = formats.formatter().format(message, args().add("1"));
    assertEquals(actual, "ONE");
    actual = formats.formatter().format(message, args().add("undefined"));
    assertEquals(actual, "OTHER");
    actual = formats.formatter().format(message, args().add(new LongNode(123)));
    assertEquals(actual, "OTHER");
    actual = formats.formatter().format(message, args().add(new LongNode(1)));
    assertEquals(actual, "ONE");
    actual = formats.formatter().format(message, args().add(BooleanNode.TRUE));
    assertEquals(actual, "ONE");
    actual = formats.formatter().format(message, args().add(BooleanNode.FALSE));
    assertEquals(actual, "OTHER");
    actual = formats.formatter().format(message, args().add(MissingNode.getInstance()));
    assertEquals(actual, "OTHER");
    actual = formats.formatter().format(message, args().add(NullNode.getInstance()));
    assertEquals(actual, "OTHER");
    actual = formats.formatter().format(message, args().add(new TextNode("abc")));
    assertEquals(actual, "OTHER");
    actual = formats.formatter().format(message, args().add(JsonUtils.createObjectNode()));
    assertEquals(actual, "OTHER");
    ObjectNode money = JsonUtils.createObjectNode();
    money.put("decimalValue", new DecimalNode(new BigDecimal("1.2320001")));
    actual = formats.formatter().format(message, args().add(money));
    assertEquals(actual, "OTHER");
    money.put("decimalValue", new DecimalNode(new BigDecimal("1")));
    actual = formats.formatter().format(message, args().add(money));
    assertEquals(actual, "ONE");
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CLDR(com.squarespace.cldrengine.CLDR) TextNode(com.fasterxml.jackson.databind.node.TextNode) LongNode(com.fasterxml.jackson.databind.node.LongNode) DecimalNode(com.fasterxml.jackson.databind.node.DecimalNode) MessageFormats(com.squarespace.template.MessageFormats) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 10 with CLDR

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

the class MessageFormatsTest method testPlural.

@Test
public void testPlural() {
    String actual;
    CLDR cldr = CLDR.get("en");
    MessageFormats formats = new MessageFormats(cldr);
    formats.setTimeZone("America/New_York");
    String message = "you have {0} item{0 plural one{} other{s}} in your cart";
    actual = formats.formatter().format(message, args().add(JsonUtils.decode("1")));
    assertEquals(actual, "you have 1 item in your cart");
    actual = formats.formatter().format(message, args().add(JsonUtils.decode("1.0")));
    assertEquals(actual, "you have 1.0 items in your cart");
    actual = formats.formatter().format(message, args().add(JsonUtils.decode("3")));
    assertEquals(actual, "you have 3 items in your cart");
    ObjectNode money = money("1", "USD");
    actual = formats.formatter().format(message, args().add(money));
    assertEquals(actual, "you have 1 item in your cart");
    money = newmoney("3", "USD");
    actual = formats.formatter().format(message, args().add(money));
    assertEquals(actual, "you have 3 items in your cart");
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) CLDR(com.squarespace.cldrengine.CLDR) MessageFormats(com.squarespace.template.MessageFormats) Test(org.testng.annotations.Test)

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