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);
}
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;
}
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");
}
Aggregations