001: /*
002: * $Id: I18n.java 523619 2007-03-29 08:31:43Z rgielen $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.components;
022:
023: import com.opensymphony.xwork2.ActionContext;
024: import com.opensymphony.xwork2.LocaleProvider;
025: import com.opensymphony.xwork2.TextProviderFactory;
026: import com.opensymphony.xwork2.util.LocalizedTextUtil;
027: import com.opensymphony.xwork2.util.ValueStack;
028: import org.apache.struts2.StrutsException;
029: import org.apache.struts2.dispatcher.Dispatcher;
030: import org.apache.struts2.views.annotations.StrutsTag;
031: import org.apache.struts2.views.annotations.StrutsTagAttribute;
032:
033: import java.io.Writer;
034: import java.util.Locale;
035: import java.util.ResourceBundle;
036:
037: /**
038: * <!-- START SNIPPET: javadoc -->
039: *
040: * Gets a resource bundle and place it on the value stack. This allows
041: * the text tag to access messages from any bundle, and not just the bundle
042: * associated with the current action.
043: *
044: * <!-- END SNIPPET: javadoc -->
045: *
046: * <p/>
047: *
048: * <!-- START SNIPPET: params-->
049: *
050: * <ul>
051: * <li>name* - the resource bundle's name (eg foo/bar/customBundle)</li>
052: * </ul>
053: *
054: * <!-- END SNIPPET: params -->
055: *
056: * <p/>
057: *
058: * Example:
059: *
060: * <pre>
061: * <!-- START SNIPPET: example -->
062: *
063: * <s:i18n name="myCustomBundle">
064: * The i18n value for key aaa.bbb.ccc in myCustomBundle is <s:property value="text('aaa.bbb.ccc')" />
065: * </s:i18n>
066: *
067: * <!-- END SNIPPET: example -->
068: * </pre>
069: *
070: *
071: * <pre>
072: * <!-- START SNIPPET: i18nExample -->
073: *
074: * <s:i18n name="some.package.bundle" >
075: * <s:text name="some.key" />
076: * </s:i18n>
077: *
078: * <!-- END SNIPPET: i18nExample -->
079: * </pre>
080: *
081: */
082: @StrutsTag(name="i18n",tldTagClass="org.apache.struts2.views.jsp.I18nTag",description="Get a resource bundle" + " and place it on the value stack")
083: public class I18n extends Component {
084: protected boolean pushed;
085: protected String name;
086:
087: public I18n(ValueStack stack) {
088: super (stack);
089: }
090:
091: public boolean start(Writer writer) {
092: boolean result = super .start(writer);
093:
094: try {
095: String name = this
096: .findString(this .name, "name",
097: "Resource bundle name is required. Example: foo or foo_en");
098: ResourceBundle bundle = (ResourceBundle) findValue("texts('"
099: + name + "')");
100:
101: if (bundle == null) {
102: bundle = LocalizedTextUtil.findResourceBundle(name,
103: (Locale) getStack().getContext().get(
104: ActionContext.LOCALE));
105: }
106:
107: if (bundle != null) {
108: final Locale locale = (Locale) getStack().getContext()
109: .get(ActionContext.LOCALE);
110: TextProviderFactory tpf = new TextProviderFactory();
111: Dispatcher.getInstance().getContainer().inject(tpf);
112: getStack().push(
113: tpf.createInstance(bundle,
114: new LocaleProvider() {
115: public Locale getLocale() {
116: return locale;
117: }
118: }));
119: pushed = true;
120: }
121: } catch (Exception e) {
122: String msg = "Could not find the bundle " + name;
123: throw new StrutsException(msg, e);
124: }
125:
126: return result;
127: }
128:
129: public boolean end(Writer writer, String body) {
130: if (pushed) {
131: getStack().pop();
132: }
133:
134: return super .end(writer, body);
135: }
136:
137: @StrutsTagAttribute(description="Name of ressource bundle to use (eg foo/bar/customBundle)",required=true,defaultValue="String")
138: public void setName(String name) {
139: this.name = name;
140: }
141: }
|