On Wed, 30 Jul 2014 07:45:20 -0300, Stephan Windmüller
<stephan.windmuel...@tu-dortmund.de> wrote:
Hello,
Hi!
in our Tapestry application we have several product lines which require
a different wording on the pages. My first thought was to provide my own
implementation of the Messages service and set the product line in the
Java Code with something like
messages.setProductLine(ProductLine)
during onActivate and provide different keys like
title-PRODUCT1=Product One
title-PRODUCT2=Product Two
Why don't you create your own 'productmessage' binding so you can write
${productmessage:title} and have it treated like you wrote
messages.get("title")?
That would work for all messages retrieved by the injected service, but
how can I replace the service which is used to parse messages in the TML
files?
The Tapestry template parser does *not* parse messages. Actually, it just
parses ${} expansions and ask their values to the specified binding. The
'message' binding is the one who actually handles the messages. Here's its
implementation:
public class MessageBindingFactory implements BindingFactory
{
public Binding newBinding(String description, ComponentResources
container, ComponentResources component,
String expression, Location location)
{
String messageValue = container.getMessages().get(expression);
return new LiteralBinding(location, description, messageValue);
}
}
Incredibly simple, isn't it? :)
Here's an untested version of your ProductMessageBindingFactory, supposing
you have a ProductService perthread service which provides the current
product:
public class ProductMessageBindingFactory implements BindingFactory
{
final private ProductService productService;
public ProductMessageBindingFactory(ProductService productService) {
this.productService = productService;
}
public Binding newBinding(String description, ComponentResources
container, ComponentResources component,
String expression, Location location)
{
String messageValue = container.getMessages().get(expression + "-"
+ productService.getCurrentProductMessageSuffix());
return new LiteralBinding(location, description, messageValue);
}
}
Then, in your AppModule class (or any other Tapestry-IoC module class),
contribute it to BindingSource:
public static void contributeBindingSource(MappedConfiguration<String,
BindingFactory> configuration) {
configuration.addInstance("productmessages",
ProductMessageBindingFactory.class);
}
And that's it.
--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org