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 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 * <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 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 }