All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.squarespace.template.plugins.platform.i18n.MessageFormatter Maven / Gradle / Ivy

There is a newer version: 1.5.1
Show newest version
/**
 * Copyright (c) 2017 SQUARESPACE, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.squarespace.template.plugins.platform.i18n;

import static com.squarespace.template.GeneralUtils.splitVariable;

import java.time.ZoneId;

import com.fasterxml.jackson.databind.JsonNode;
import com.squarespace.cldr.MessageArgs;
import com.squarespace.cldr.MessageFormat;
import com.squarespace.template.Arguments;
import com.squarespace.template.ArgumentsException;
import com.squarespace.template.BaseFormatter;
import com.squarespace.template.CodeExecuteException;
import com.squarespace.template.Context;
import com.squarespace.template.Variable;
import com.squarespace.template.Variables;
import com.squarespace.template.plugins.PluginDateUtils;


/**
 * MESSAGE - Evaluates a MessageFormat against one or more arguments.
 */
public class MessageFormatter extends BaseFormatter {

  public MessageFormatter() {
    this("message");
  }

  public MessageFormatter(String alias) {
    super(alias, true);
  }

  @Override
  public void validateArgs(Arguments args) throws ArgumentsException {
    args.setOpaque(messageArgs(args));
  }

  @Override
  public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
    Variable var = variables.first();
    JsonNode node = var.node();

    MessageArgs msgArgs = (MessageArgs) args.getOpaque();
    msgArgs.resetArgs();
    setContext(msgArgs, ctx);
    String message = node.asText();
    String tzName = PluginDateUtils.getTimeZoneNameFromContext(ctx);
    MessageFormat msgFormat = new MessageFormat(ctx.cldrLocale(), ZoneId.of(tzName), message);
    StringBuilder buf = new StringBuilder();
    msgFormat.format(msgArgs, buf);
    var.set(buf);
  }

  /**
   * Set the context instance used to resolve the argument values on demand.
   */
  private static void setContext(MessageArgs args, Context ctx) {
    int count = args.count();
    for (int i = 0; i < count; i++) {
      MsgArg arg = (MsgArg)args.get(i);
      arg.setContext(ctx);
    }
  }

  /**
   * Initialize the plural arguments array.
   */
  private static MessageArgs messageArgs(Arguments arguments) {
    MessageArgs result = new MessageArgs();
    int count = arguments.count();
    for (int i = 0; i < count; i++) {
      String raw = arguments.get(i);
      String name = null;

      // Check if this is a named argument, e.g. :
      int index = raw.indexOf(':');
      if (index != -1) {
        name = raw.substring(0, index);
        raw = raw.substring(index + 1);
      }

      // Parse the context variable reference and append the argument.
      Object[] variable = splitVariable(raw);
      MsgArg arg = new MsgArg(variable);
      if (name == null) {
        result.add(arg);
      } else {
        result.add(name, arg);
      }
    }
    return result;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy