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?
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.
Comments