View Javadoc

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