FileDocCategorySizeDatePackage
I18n.javaAPI DocExample4441Mon Jul 23 13:26:36 BST 2007org.apache.struts2.components

I18n

public class I18n extends Component
Gets a resource bundle and place it on the value stack. This allows the text tag to access messages from any bundle, and not just the bundle associated with the current action.

  • name* - the resource bundle's name (eg foo/bar/customBundle)

Example:



<s:i18n name="myCustomBundle">
The i18n value for key aaa.bbb.ccc in myCustomBundle is <s:property value="text('aaa.bbb.ccc')" />
</s:i18n>




<s:i18n name="some.package.bundle" >
<s:text name="some.key" />
</s:i18n>


Fields Summary
protected boolean
pushed
protected String
name
Constructors Summary
public I18n(com.opensymphony.xwork2.util.ValueStack stack)

        super(stack);
    
Methods Summary
public booleanend(java.io.Writer writer, java.lang.String body)

        if (pushed) {
            getStack().pop();
        }

        return super.end(writer, body);
    
public voidsetName(java.lang.String name)

        this.name = name;
    
public booleanstart(java.io.Writer writer)

        boolean result = super.start(writer);

        try {
            String name = this.findString(this.name, "name", "Resource bundle name is required. Example: foo or foo_en");
            ResourceBundle bundle = (ResourceBundle) findValue("texts('" + name + "')");

            if (bundle == null) {
                bundle = LocalizedTextUtil.findResourceBundle(name, (Locale) getStack().getContext().get(ActionContext.LOCALE));
            }

            if (bundle != null) {
                final Locale locale = (Locale) getStack().getContext().get(ActionContext.LOCALE);
                TextProviderFactory tpf = new TextProviderFactory();
                Dispatcher.getInstance().getContainer().inject(tpf);
                getStack().push(tpf.createInstance(bundle, new LocaleProvider() {
                     public Locale getLocale() {
                         return locale;
                     }
                }));
                pushed = true;
            }
        } catch (Exception e) {
            String msg = "Could not find the bundle " + name;
            throw new StrutsException(msg, e);
        }

        return result;