1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.components;
22
23 import java.io.Writer;
24 import java.util.Locale;
25 import java.util.ResourceBundle;
26
27 import org.apache.struts2.views.annotations.StrutsTag;
28 import org.apache.struts2.views.annotations.StrutsTagAttribute;
29 import org.apache.struts2.StrutsException;
30
31 import com.opensymphony.xwork2.ActionContext;
32 import com.opensymphony.xwork2.LocaleProvider;
33 import com.opensymphony.xwork2.TextProviderSupport;
34 import com.opensymphony.xwork2.util.LocalizedTextUtil;
35 import com.opensymphony.xwork2.util.ValueStack;
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 * <s:i18n name="myCustomBundle">
64 * The i18n value for key aaa.bbb.ccc in myCustomBundle is <s:property value="text('aaa.bbb.ccc')" />
65 * </s:i18n>
66 *
67 * <!-- END SNIPPET: example -->
68 * </pre>
69 *
70 *
71 * <pre>
72 * <!-- START SNIPPET: i18nExample -->
73 *
74 * <s:i18n name="some.package.bundle" >
75 * <s:text name="some.key" />
76 * </s:i18n>
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 getStack().push(new TextProviderSupport(bundle, new LocaleProvider() {
106 public Locale getLocale() {
107 return locale;
108 }
109 }));
110 pushed = true;
111 }
112 } catch (Exception e) {
113 String msg = "Could not find the bundle " + name;
114 throw new StrutsException(msg, e);
115 }
116
117 return result;
118 }
119
120 public boolean end(Writer writer, String body) {
121 if (pushed) {
122 getStack().pop();
123 }
124
125 return super.end(writer, body);
126 }
127
128 @StrutsTagAttribute(description="Name of ressource bundle to use (eg foo/bar/customBundle)", required=true, defaultValue="String")
129 public void setName(String name) {
130 this.name = name;
131 }
132 }