Friday, June 20, 2008

Metawidget and Resource Bundles

I was recently asked if I could 'give an example of how to get translated labels (from a resource bundle) in the JSF version' of Metawidget.

You can see a working example of this in the addressbook-faces.war included in the download (with tutorial here), but in this blog entry I'll focus specifically on translated labels.

Metawidget generates labels for widgets based on...
  • the uncamel-cased version of the property name (eg. 'Date of birth' for 'dateOfBirth')

  • the exact text specified in the @UiLabel annotation (or label XML attribute)

...if a resource bundle is specified, it also tries...
  • a bundle key based on the camel-cased version of the text specified in @UiLabel or label (eg. 'dateOfBirth' for 'Date of birth')

  • a bundle key based on the property name

That's a lot of combinations, I know! But the idea is you can develop your app first without a bundle, and Metawidget will use sensible defaults. Then you can add a bundle later and Metawidget will automatically adapt to using it.

To add a bundle, you can either add one globally in faces-config.xml...

<application>
   <message-bundle>com.myapp.Resources</message-bundle>
</application>

...or on the page...

<f:loadBundle basename="com.myapp.Resources" var="bundle"/>
   ...
<m:metawidget value="#{myBusinessObject}" bundle="#{bundle}"/>

...and that's it! The other supported Metawidgets (for Android, GWT, JSP, Spring, Struts, Swing, etc.) all do localization in a similar fashion, following the conventions of their particular platform.

0 comments: