Search in sources :

Example 1 with MessageFormats

use of com.squarespace.template.MessageFormats 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 2 with MessageFormats

use of com.squarespace.template.MessageFormats 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 3 with MessageFormats

use of com.squarespace.template.MessageFormats 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 4 with MessageFormats

use of com.squarespace.template.MessageFormats 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 5 with MessageFormats

use of com.squarespace.template.MessageFormats 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

MessageFormats (com.squarespace.template.MessageFormats)7 CLDR (com.squarespace.cldrengine.CLDR)6 Test (org.testng.annotations.Test)6 LongNode (com.fasterxml.jackson.databind.node.LongNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 TextNode (com.fasterxml.jackson.databind.node.TextNode)2 MessageArgs (com.squarespace.cldrengine.api.MessageArgs)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 DecimalNode (com.fasterxml.jackson.databind.node.DecimalNode)1 GeneralUtils.splitVariable (com.squarespace.template.GeneralUtils.splitVariable)1 Variable (com.squarespace.template.Variable)1 BigDecimal (java.math.BigDecimal)1