Jai’s Weblog – Tech, Security & Fun…

Tech, Security & Fun…

  • Jaibeer Malik

    Jaibeer Malik
  • View Jaibeer Malik's profile on LinkedIn
  • Subscribe

  • Feedburner

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 40 other subscribers
  • Archives

  • Categories

  • Stats

    • 426,454
  • Live Traffic

Archive for November, 2008

Localization of Wicket Applications

Posted by Jai on November 12, 2008


In this article we will try to understand how to handle the Localization issues in Wicket Applications.

In Markup, localized labels can be shown using <wicket:message> tags

<wicket:message key=”test.key.here”></wicket:message>

In Java code, Localization can be achieved using following different ways:

  • Localizer
  • ResourceModel
  • StringResourceModel

The following article explains very well that “How does Wicket Resolve Resource Bundles and Spring as a message provider”.

Localizer:

getLocalizer().getString(….);

It returns you the localized string. But the problem here is that “If the component is not an instance of Page then it must be a component that has already been added to a page.” otherwise it will throw the following warning message.

—-

12:07:11,726 WARN [Localizer] Tried to retrieve a localized string for a component that has not yet been added to the page. This can sometimes lead to an invalid or no localized resource returned. Make sure you are not calling Component#getString() inside your Component’s constructor. Offending component: [MarkupContainer [Component id = testPanel, page = <No Page>, path = testPanel.TestPanel]]

—–

So, either we change the design not to call this in component constructor or extract out the method and call it explicitly after adding component to the page or we use other method.

ResourceModel/StringResourceModel:

ResourceModel is a lightweight version of the StringResourceModel. It lacks parameter substitutions, but is generally easier to use.

Here we get rid of the above warning message, it internally calls the localizer but gets it from Application settings. It look for the IStringResourceLoader implementation which can be BundleStringResourceLoader, ComponentStringResourceLoader or ClassStringResourceLoader.

To use BundleStringResourceLoader:

tester.getApplication().getResourceSettings().addStringResourceLoader(new BundleStringResourceLoader(“test.application”));

For wicket versions 1.3.2 and earlier (fixed in 1.3.3 onwards) there is a bug https://issues.apache.org/jira/browse/WICKET-1415 and you will get following exception:

——-

Caused by: java.lang.NullPointerException 
 	at org.apache.wicket.resource.loader.BundleStringResourceLoader.loadStringResource(BundleStringResourceLoader.java:94)
 	at org.apache.wicket.Localizer.getString(Localizer.java:221)
 	at org.apache.wicket.Localizer.getString(Localizer.java:131)
 	at org.apache.wicket.model.ResourceModel.getObject(ResourceModel.java:73)

——–

To use ClassStringResourceLoader:

tester.getApplication().getResourceSettings().addStringResourceLoader(new ClassStringResourceLoader(MaintenanceApplication.class));

And you can escape the above NPE using this. An instance of this loader is registered with the Application by default.

Posted in Wicket | Tagged: , | Leave a Comment »