Over a million developers have joined DZone.

Internationalization Support in Apache Wicket

DZone's Guide to

Internationalization Support in Apache Wicket

· Integration Zone ·
Free Resource

Discover how you can get APIs and microservices to work at true enterprise scale.

In this article we are going to see how to internationalize applications in Apache Wicket. Wicket provides a simple way to provide i18n support for your applications. All you need to use the wicket:message element in the HTML file directly:

<wicket:message key="staticMessage" >Message</wicket:message> 

The corresponding property files are for e.g.


staticMessage= Message 

For Germany:


staticMessage= Nachricht 

For Netherland:


staticMessage= Bericht 

The key “staticMessage” is used to lookup the message in the property files. The default text Message is used when the message could not be found. Localized pages and panel can have their own bundle files having as base name as the class name of the related component and placed in the same package.

So in our example we have DisplayPage.java class. We have created our resource bundles based on name of this class for. E.g DisplayPage_de.properties. Note all these resource files are located in same place where our DisplayPage.java class resides.

Alternatively if you want to use text from properties file in wicket components like labels etc. simply use getString("key") method provided by Component class in wicket.

How lookup algorithm works:

Wicket will search all resource files with the names equal to the components in your component hierarchy, starting from application level at top to component defaults as a last place to look.

So when Wickets wants to look up a message used in MyPanel, where MyPanel is contained within MyPage, and the application is called MyApplication,

Wicket will look in:

1. MyApplication_locale.properties, ..., and then in case defaults, MyApplication.properties (..)

2. MyPage_locale.properties, ..., and then MyPage.properties

3. MyPanel_locale.properties, ..., and then MyPanel.properties

And what if it has same key in all these hierarchy properties files?

The lookup algorithm gives priority to the bundles of the containers (parents) of the component that is requesting a localized resource. The more a container is higher in the hierarchy, the bigger is its priority over the other components. In other words properties inside the resource bundle of a page will have the priority over the properties with the same key defined in the bundles of children components.

So for example in our sample code if we have HomePage which is container for DisplayPage, have a property with same key i.e. staticMessage with some other value in its resource bundle then it will be displayed instead of property in DisplayPage resource bundles. HomePage.properties staticMessage= Title Message.

So here text “Title Message” will be displayed instead of DisplayPage.

Also note that for DisplayPage component DisplayPage.properties is a default resource bundle.

So for e.g. your locale is set to “de” and there is no resource bundle for that locale (DisplayPage_de.properties is missing) then lookup algorithm will fall back to default resource bundle in this case DisplayPage.properties

To display languages like Chinese, Japanese etc, which need UTF-8 encoding you, need to just append resource bundles with .utf8.properties extension.

Finally how to change locale in wicket?

To manually switch the locale in wicket:

Session.get().setLocale(new Locale(Locale.GERMAN));

A complete source code for sample example could be found here .

APIs and microservices are maturing, quickly. Learn what it takes to manage modern APIs and microservices at enterprise scale.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}