Search in sources :

Example 1 with MessageArgs

use of com.squarespace.cldrengine.api.MessageArgs 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 2 with MessageArgs

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

the class MessageFormatter method messageArgs.

private static MessageArgs messageArgs(Arguments args, Context ctx) {
    MessageArgs res = new MessageArgs();
    int count = args.count();
    for (int i = 0; i < count; i++) {
        String raw = args.get(i);
        String name = null;
        // Either ':' or '=' can delimit key/value arguments
        int index = delimiter(raw);
        if (index != -1) {
            // Map named argument
            name = raw.substring(0, index);
            raw = raw.substring(index + 1);
        }
        Object[] ref = splitVariable(raw);
        // Since the message string is the node on the current stack frame, the
        // variable reference '@' will point to it, instead of the parent scope.
        // Skip the current frame so we avoid trying to resolve variables against
        // the message string.
        Frame parent = ctx.frame().parent();
        JsonNode value = ctx.resolve(ref, parent == null ? ctx.frame() : parent);
        if (name != null) {
            res.add(name, value);
        }
        // Keyword arguments are also positional
        res.add(value);
    }
    return res;
}
Also used : MessageArgs(com.squarespace.cldrengine.api.MessageArgs) Frame(com.squarespace.template.Frame) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 3 with MessageArgs

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

the class MessageFormatsTest method testInterval.

@Test
public void testInterval() {
    String actual;
    CLDR cldr = CLDR.get("en");
    MessageFormats formats = new MessageFormats(cldr);
    formats.setTimeZone("America/New_York");
    String message = "{0;1 datetime-interval}";
    long epoch = 1582129775000L;
    long extra = 86400000 + 3600000;
    MessageArgs args = args().add(new LongNode(epoch)).add(new LongNode(epoch + extra));
    actual = formats.formatter().format(message, args);
    assertEquals(actual, "Feb 19 – 20, 2020");
}
Also used : MessageArgs(com.squarespace.cldrengine.api.MessageArgs) CLDR(com.squarespace.cldrengine.CLDR) LongNode(com.fasterxml.jackson.databind.node.LongNode) MessageFormats(com.squarespace.template.MessageFormats) Test(org.testng.annotations.Test)

Aggregations

MessageArgs (com.squarespace.cldrengine.api.MessageArgs)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 MessageFormats (com.squarespace.template.MessageFormats)2 LongNode (com.fasterxml.jackson.databind.node.LongNode)1 CLDR (com.squarespace.cldrengine.CLDR)1 Frame (com.squarespace.template.Frame)1 GeneralUtils.splitVariable (com.squarespace.template.GeneralUtils.splitVariable)1 Variable (com.squarespace.template.Variable)1 Test (org.testng.annotations.Test)1