001: package com.opensymphony.webwork.components;
002:
003: import com.opensymphony.xwork.ActionContext;
004: import com.opensymphony.xwork.LocaleProvider;
005: import com.opensymphony.xwork.TextProviderSupport;
006: import com.opensymphony.xwork.util.LocalizedTextUtil;
007: import com.opensymphony.xwork.util.OgnlValueStack;
008: import com.opensymphony.webwork.WebWorkException;
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: import java.io.Writer;
013: import java.util.Locale;
014: import java.util.ResourceBundle;
015:
016: /**
017: * <!-- START SNIPPET: javadoc -->
018: *
019: * Gets a resource bundle and place it on the value stack. This allows
020: * the text tag to access messages from any bundle, and not just the bundle
021: * associated with the current action.
022: *
023: * <!-- END SNIPPET: javadoc -->
024: *
025: * <p/>
026: *
027: * <!-- START SNIPPET: params-->
028: *
029: * <ul>
030: * <li>name* - the resource bundle's name (eg foo/bar/customBundle)</li>
031: * </ul>
032: *
033: * <!-- END SNIPPET: params -->
034: *
035: * <p/>
036: *
037: * Example:
038: *
039: * <pre>
040: * <!-- START SNIPPET: example -->
041: *
042: * <ww:i18n name="myCustomBundle">
043: * The i18n value for key aaa.bbb.ccc in myCustomBundle is <ww:property value="text('aaa.bbb.ccc')" />
044: * </ww:i18n>
045: *
046: * <!-- END SNIPPET: example -->
047: * </pre>
048: *
049: *
050: * <pre>
051: * <!-- START SNIPPET: i18nExample -->
052: *
053: * <ww:i18n name="some.package.bundle" >
054: * <ww:text name="some.key" />
055: * </ww:i18n>
056: *
057: * <!-- END SNIPPET: i18nExample -->
058: * </pre>
059: *
060: *
061: * @author Rickard �berg (rickard@dreambean.com)
062: * @author Rene Gielen
063: * @author tm_jee ( tm_jee (at) yahoo.co.uk )
064: * @version $Revision: 2647 $
065: * @since 2.2
066: *
067: * @ww.tag name="i18n" tld-body-content="JSP" tld-tag-class="com.opensymphony.webwork.views.jsp.I18nTag"
068: * description="Get a resource bundle and place it on the value stack"
069: */
070: public class I18n extends Component {
071: private static final Log LOG = LogFactory.getLog(I18n.class);
072:
073: protected boolean pushed;
074: protected String name;
075:
076: public I18n(OgnlValueStack stack) {
077: super (stack);
078: }
079:
080: public boolean start(Writer writer) {
081: boolean result = super .start(writer);
082:
083: try {
084: String name = this
085: .findString(this .name, "name",
086: "Resource bundle name is required. Example: foo or foo_en");
087: ResourceBundle bundle = (ResourceBundle) findValue("texts('"
088: + name + "')");
089:
090: if (bundle == null) {
091: bundle = LocalizedTextUtil.findResourceBundle(name,
092: (Locale) getStack().getContext().get(
093: ActionContext.LOCALE));
094: }
095:
096: if (bundle != null) {
097: final Locale locale = (Locale) getStack().getContext()
098: .get(ActionContext.LOCALE);
099: getStack().push(
100: new TextProviderSupport(bundle,
101: new LocaleProvider() {
102: public Locale getLocale() {
103: return locale;
104: }
105: }));
106: pushed = true;
107: }
108: } catch (Exception e) {
109: String msg = "Could not find the bundle " + name;
110: LOG.error(msg, e);
111: throw new WebWorkException(msg);
112: }
113:
114: return result;
115: }
116:
117: public boolean end(Writer writer, String body) {
118: if (pushed) {
119: getStack().pop();
120: }
121:
122: return super .end(writer, body);
123: }
124:
125: /**
126: * Name of ressource bundle to use (eg foo/bar/customBundle)
127: * @ww.tagattribute required="true" default="String"
128: */
129: public void setName(String name) {
130: this.name = name;
131: }
132: }
|