1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 * <s:i18n name="myCustomBundle">
59 * The i18n value for key aaa.bbb.ccc in myCustomBundle is <s:property value="text('aaa.bbb.ccc')" />
60 * </s:i18n>
61 *
62 * <!-- END SNIPPET: example -->
63 * </pre>
64 *
65 *
66 * <pre>
67 * <!-- START SNIPPET: i18nExample -->
68 *
69 * <s:i18n name="some.package.bundle" >
70 * <s:text name="some.key" />
71 * </s:i18n>
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 }