Overview

Any action can indicate that it supports localization by implementing com.opensymphony.xwork.TextProvider. To access a localized message, simply use one of the various getText() method calls.

The default implementation for this is com.opensymphony.xwork.TextProviderSupport, which in turn relies on com.opensymphony.xwork.util.LocalizedTextUtil. Any Action that extends com.opensymphony.xwork.ActionSupport will automatically gain localization support via TextProviderSupport.

In this implementation, when you attempt to look up a message, it attempts to do the following:

  • Look for the message in the Action's class hierarchy.
    • Look for the message in a resource bundle for the class
    • If not found, look for the message in a resource bundle for any interface implemented by the class
    • If not found, get the super-class and repeat from the first sub-step unless the super-class is Object
  • If not found and the Action is a ModelDriven Action, then look for the message in
    the model's class hierarchy (repeat sub-steps listed above).
  • If not found, look for the message in a child property. This is determined by evaluating the message key as an OGNL expression. For example, if the key is user.address.state, then it will attempt to see if "user" can be resolved into an object. If so, repeat the entire process fromthe beginning with the object's class and address.state as the message key.
  • If not found, look for the message in the Action's package hierarchy.
  • If still not found, look for the message in the default resource bundles.

Default Resource Bundles.

It is possible to register default resource bundles with XWork via LocalizedTextUtil.addDefaultResourceBundle().

Message lookup in the default resource bundles is done in reverse order of their registration (i.e. the first resource bundle registered is the last to be searched).

By default, one default resource bundle name is registered with LocalizedTextUtil – "com/opensymphony/xwork/xwork-messages" – which is bundled with the XWork jar file to provide system-level message texts.

Example

Given a ModelDriven Action called BarnAction where getModel() returns a Horse object, and the Horse object has the following class structure:

interface acme.test.Animal;
class acme.test.AnimalImpl implements Animal;
interface acme.test.Quadraped extends Animal;
class acme.test.QuadrapedImpl extends Animal implements Quadraped;
class acme.test.Horse extends QuadrapedImpl;

Then the localization system will attempt to look up the message in the following resource bundles in this order:

acme.test.BarnAction.properties
acme.test.Horse.properties
acme.test.QuadrapedImpl.properties
acme.test.Quadraped.properties
acme.test.AnimalImpl.properties
acme.test.Animal.properties
acme.test.package.properties
acme.package.properties

Message Key Interpolation

When looking for the message, if the key indexes a collection (e.g. user.phone0) and a message for that specific key cannot be found, the general form will also be looked up (i.e. user.phone\*).

Message Interpolation

If a message is found, it will also be interpolated. Anything within ${...} will be treated as an OGNL expression and evaluated as such.