View Javadoc

1   /*
2    * $Id: I18n.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.components;
19  
20  import java.io.Writer;
21  import java.util.Locale;
22  import java.util.ResourceBundle;
23  
24  import org.apache.struts2.StrutsException;
25  
26  import com.opensymphony.xwork2.ActionContext;
27  import com.opensymphony.xwork2.LocaleProvider;
28  import com.opensymphony.xwork2.TextProviderSupport;
29  import com.opensymphony.xwork2.util.LocalizedTextUtil;
30  import com.opensymphony.xwork2.util.ValueStack;
31  
32  /***
33   * <!-- START SNIPPET: javadoc -->
34   * 
35   * Gets a resource bundle and place it on the value stack. This allows
36   * the text tag to access messages from any bundle, and not just the bundle
37   * associated with the current action.
38   * 
39   * <!-- END SNIPPET: javadoc -->
40   * 
41   * <p/>
42   * 
43   * <!-- START SNIPPET: params-->
44   * 
45   * <ul>
46   * 		<li>name* - the resource bundle's name (eg foo/bar/customBundle)</li>
47   * </ul>
48   * 
49   * <!-- END SNIPPET: params -->
50   * 
51   * <p/>
52   * 
53   * Example:
54   * 
55   * <pre>
56   * <!-- START SNIPPET: example -->
57   * 
58   * &lt;s:i18n name="myCustomBundle"&gt;
59   *    The i18n value for key aaa.bbb.ccc in myCustomBundle is &lt;s:property value="text('aaa.bbb.ccc')" /&gt;
60   * &lt;/s:i18n&gt;
61   * 
62   * <!-- END SNIPPET: example -->
63   * </pre>
64   * 
65   * 
66   * <pre>
67   * <!-- START SNIPPET: i18nExample -->
68   * 
69   * &lt;s:i18n name="some.package.bundle" &gt;
70   *      &lt;s:text name="some.key" /&gt;
71   * &lt;/s:i18n&gt;
72   * 
73   * <!-- END SNIPPET: i18nExample -->
74   * </pre>
75   * 
76   * @s.tag name="i18n" tld-body-content="JSP" tld-tag-class="org.apache.struts2.views.jsp.I18nTag"
77   * description="Get a resource bundle and place it on the value stack"
78   */
79  public class I18n extends Component {
80      protected boolean pushed;
81      protected String name;
82  
83      public I18n(ValueStack stack) {
84          super(stack);
85      }
86  
87      public boolean start(Writer writer) {
88          boolean result = super.start(writer);
89  
90          try {
91              String name = this.findString(this.name, "name", "Resource bundle name is required. Example: foo or foo_en");
92              ResourceBundle bundle = (ResourceBundle) findValue("texts('" + name + "')");
93  
94              if (bundle == null) {
95                  bundle = LocalizedTextUtil.findResourceBundle(name, (Locale) getStack().getContext().get(ActionContext.LOCALE));
96              }
97  
98              if (bundle != null) {
99                  final Locale locale = (Locale) getStack().getContext().get(ActionContext.LOCALE);
100                 getStack().push(new TextProviderSupport(bundle, new LocaleProvider() {
101                     public Locale getLocale() {
102                         return locale;
103                     }
104                 }));
105                 pushed = true;
106             }
107         } catch (Exception e) {
108             String msg = "Could not find the bundle " + name;
109             throw new StrutsException(msg, e);
110         }
111 
112         return result;
113     }
114 
115     public boolean end(Writer writer, String body) {
116         if (pushed) {
117             getStack().pop();
118         }
119 
120         return super.end(writer, body);
121     }
122 
123     /***
124      * Name of ressource bundle to use (eg foo/bar/customBundle)
125      * @s.tagattribute required="true" default="String"
126      */
127     public void setName(String name) {
128         this.name = name;
129     }
130 }