Apache Wicket: dynamic key for message with embedded HTML

Christophe L

Wicket supports complex translatable message containing HTML elements like links, etc. as described in https://cwiki.apache.org/confluence/display/WICKET/Everything+about+Wicket+internationalization using wicket:message. E.g.:

<wicket:message key="messageKey">
    <a wicket:id="link"><wicket:message key="linkLabel"></wicket:message></a>
</wicket:message>

and the properties:

messageKey=Click on ${link}

and in Java

add(new BookmarkablePage<Void>("link", SomePage.class);

This works great, however the message key is hardcoded in the HTML.

In my case, I want the message key to be dynamically determined in Java. For regular messages (with string parameters) I can do that with a Label and a StringResourceModel that takes the key as a parameter. But how do I do the same thing for a message that contains Wicket components as parameters? I guess the markup would looks something like:

<span wicket:id="messageId">
    <a wicket:id="link"><wicket:message key="linkLabel"></wicket:message></a>
<span>

But what would the Java be? Label cannot have children. :(

=== UPDATE ===

There's a Wicket class org.apache.wicket.markup.resolver.WicketMessageResolver.MessageContainer that looks interesting. Since it's private static, I cannot use it directly but if I copy paste the code into a public class and tweak a couple things related to component hierarchy, I'm getting close to a solution. But that's pretty hacky. Is there a cleaner solution?

Christophe L

I ended up copying / modifying Wicket's private class MessageContainer (nested in org.apache.wicket.markup.resolver.WicketMessageResolver) like so:

https://gist.github.com/totof3110/cf5f05731816a58d8597

Then I can have Java code like:

final String messageKey;
if (userLoggedIn) {
    messageKey = "logged.in";
} else {
    messageKey = "logged.out";
}
MessageContainer message = new MessageContainer("message", messageKey);
BookmarkablePageLink<Void> link = new BookmarkablePageLink<Void>("link", UserProfilePage.class);
link.add(new Label("username", user.getUsername());
message.add(link);
add(message);

The HTML looks like:

<span wicket:id="message">
    <a wicket:id="link"><span wicket:id="username"></span></a>
</span>

The properties files looks like:

logged.in = ${link} logged in.
logged.out = ${link} logged out.

Depending on whether userLoggedIn is true or false the rendered HTML will look like:

<a href="/profile">totof3110</a> logged in.

or

<a href="/profile">totof3110</a> logged out.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Transclude HTML in Apache Wicket component

From Dev

Wicket feedback message and AjaxButton

From Dev

EJB Injection Apache Wicket

From Dev

Populating table with apache wicket

From Dev

Apache Wicket textfield onUpdate

From Dev

Apache Wicket DateTextField clear

From Dev

Jacoco support for apache wicket?

From Dev

Render dynamic HTML with embedded Razor variables using MVC

From Dev

Wicket validation message use label

From Dev

Dynamic serving - apache - html, javascript, and .htaccess only

From Dev

how to display the key name which is dynamic to html

From Dev

How to edit LoadableDetachableModel in Apache Wicket

From Dev

Apache-Wicket renders tag <wicket:panel> in page body

From Dev

Dynamic SQL with embedded table

From Dev

Trigger repainting a wicket component via push message

From Dev

Changing Wicket text field validation error message

From Dev

Is a link in Wicket IValidator's error message possible?

From Dev

Trigger repainting a wicket component via push message

From Dev

Removing an HTML tag using Wicket

From Dev

How to reload html resources in wicket

From Dev

Removing an HTML tag using Wicket

From Dev

How to insert dynamic text into a Wicket page

From Dev

Reorder items in Apache Wicket's repeating views

From Dev

Multiple paths for one class in Apache Wicket

From Dev

Apache Wicket variable link text inside table

From Dev

What is the reason of getting model of an object in apache wicket?

From Dev

How to add "onchange" SimpleAttributeModifier to DropDownChoice in Apache Wicket

From Dev

How to select multiple options in dropdown in Apache Wicket?

From Dev

Apache Wicket does not load (or display) static image

Related Related

HotTag

Archive