Handling UI messages in wicket tools

Wicket based tools

Wicket has a few ways of rendering internationalised messages. For static messages, you can put them right into the HTML template:

<span><wicket:message key="some.message.key" /></span>

You can also set internationalised HTML attributes right into the HTML templates:

<table wicket:message="summary:my.great.table.message.key">

For the case where you need a handle on the component, ie for Wicket AJAX updates, add a Label component with a ResourceModel:

Label myLabel = new Label("myLabel", new ResourceModel("some.message.key"));
someComponent.add(myLabel);

And for parameterised substitution, use a StringResourceModel instead, which allows an array of replacements:

Label myLabel = new Label("myLabel", new StringResourceModel("some.message.key.with.params", null, new Object[]{ value1, value2 } ));

If your properties files are not in the default location, you can implement your own ResourceLoader, perhaps taking advantage of the Sakai ResourceLoader. Drop this into your main WebApplication class.

//Custom resource loader
private static class MyStringResourceLoader implements IStringResourceLoader {

	private ResourceLoader messages = new ResourceLoader("MyApplication");

	public String loadStringResource(Component component, String key) {
		return messages.getString(key, key);
	}

	public String loadStringResource(Class clazz, String key, Locale locale, String style) {
		messages.setContextLocale(locale);
		return messages.getString(key, key);
	}
}

And to configure your app to use it, set it up in your WebApplication init method:

// Custom resource loader since our properties are not in the default location
getResourceSettings().addStringResourceLoader(new MyStringResourceLoader());

For more information about i18n in Wicket, see https://cwiki.apache.org/WICKET/general-i18n-in-wicket.html