You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Apache Wiki <wi...@apache.org> on 2007/09/07 17:50:34 UTC

[Tapestry Wiki] Update of "Tapestry5HowToAddMessageFormatPrefix" by FilipSAdamsen

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.

The following page has been changed by FilipSAdamsen:
http://wiki.apache.org/tapestry/Tapestry5HowToAddMessageFormatPrefix

New page:
This page describes how to add a messageformat prefix to Tapestry 5.

The messageformat prefix allows you to specify values to use in a message inline like this:
{{{
${messageformat:key,value1,value2} or <t:component t:parameter="messageformat:key,value1,value2">
}}}

First create the class !MessageFormatBindingFactory:

{{{
public class MessageFormatBindingFactory implements BindingFactory {

  private final BindingSource bindingSource;

  public MessageFormatBindingFactory(BindingSource bindingSource) {
    this.bindingSource = bindingSource;
  }

  public Binding newBinding(String description, ComponentResources container, ComponentResources component,
                            String expression, Location location) {
    String key = expression.substring(0, expression.indexOf(','));
    String[] parts = expression.substring(expression.indexOf(',') + 1).split(",");

    Object[] values = new Object[parts.length];
    for (int i = 0; i < parts.length; i++) {
      String part = parts[i];
      String prefix = TapestryConstants.PROP_BINDING_PREFIX;

      if ('\'' == part.charAt(0) && '\'' == part.charAt(part.length() - 1)) {
        part = part.substring(1, part.length() - 1);
        prefix = TapestryConstants.LITERAL_BINDING_PREFIX;
      }

      values[i] = bindingSource.newBinding(description, container, component, prefix, part, location).get();
    }

    String messageValue = container.getMessages().format(key, values);

    return new LiteralBinding(description, messageValue, location) {

      @Override
      public boolean isInvariant() {
        return false;
      }
    };
  }
}
}}}

And then contribute it to the BindingSource configuration:

{{{
  public static void contributeBindingSource(MappedConfiguration<String, BindingFactory> configuration,
                                             BindingSource bindingSource) {
    configuration.add("messageformat", new MessageFormatBindingFactory(bindingSource));
  }
}}}

A few things to note about the use of this prefix:

 1. Literal values should be enclosed in single quotes, and
 2. it is possible to use other prefixes for the values.

That's all there is to it.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tapestry.apache.org
For additional commands, e-mail: dev-help@tapestry.apache.org