1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.components;
23
24 import java.io.IOException;
25 import java.io.Writer;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
29
30 import org.apache.struts2.views.annotations.StrutsTag;
31 import org.apache.struts2.views.annotations.StrutsTagAttribute;
32 import org.apache.struts2.util.TextProviderHelper;
33
34 import com.opensymphony.xwork2.util.TextUtils;
35 import com.opensymphony.xwork2.util.ValueStack;
36 import com.opensymphony.xwork2.util.logging.Logger;
37 import com.opensymphony.xwork2.util.logging.LoggerFactory;
38
39 /***
40 * <!-- START SNIPPET: javadoc -->
41 * Render a I18n text message.
42 *
43 * <p/>
44 *
45 * The message must be in a resource bundle
46 * with the same name as the action that it is associated with. In practice
47 * this means that you should create a properties file in the same package
48 * as your Java class with the same name as your class, but with .properties
49 * extension.
50 *
51 * <p/>
52 *
53 * If the named message is not found, then the body of the tag will be used as default message.
54 * If no body is used, then the name of the message will be used.
55 *
56 * <!-- END SNIPPET: javadoc -->
57 *
58 *
59 *
60 * <!-- START SNIPPET: params -->
61 *
62 * <ul>
63 * <li>name* (String) - the i18n message key</li>
64 * </ul>
65 *
66 * <!-- END SNIPPET: params -->
67 *
68 * <p/>
69 *
70 * Example:
71 * <pre>
72 * <!-- START SNIPPET: exdescription -->
73 *
74 * Accessing messages from a given bundle (the i18n Shop example bundle in the first example) and using bundle defined through the framework in the second example.</p>
75 *
76 * <!-- END SNIPPET: exdescription -->
77 * </pre>
78 *
79 * <pre>
80 * <!-- START SNIPPET: example -->
81 *
82 * <!-- First Example -->
83 * <s:i18n name="struts.action.test.i18n.Shop">
84 * <s:text name="main.title"/>
85 * </s:i18n>
86 *
87 * <!-- Second Example -->
88 * <s:text name="main.title" />
89 *
90 * <!-- Third Examlpe -->
91 * <s:text name="i18n.label.greetings">
92 * <s:param >Mr Smith</s:param>
93 * </s:text>
94 *
95 * <!-- END SNIPPET: example -->
96 * </pre>
97 *
98 *
99 * <pre>
100 * <!-- START SNIPPET: i18nExample -->
101 *
102 * <-- Fourth Example -->
103 * <s:text name="some.key" />
104 *
105 * <-- Fifth Example -->
106 * <s:text name="some.invalid.key" >
107 * The Default Message That Will Be Displayed
108 * </s:text>
109 *
110 * <!-- END SNIPPET: i18nExample -->
111 * </pre>
112 *
113 * @see Param
114 *
115 */
116 @StrutsTag(
117 name="text",
118 tldTagClass="org.apache.struts2.views.jsp.TextTag",
119 description="Render a I18n text message")
120 public class Text extends ContextBean implements Param.UnnamedParametric {
121 private static final Logger LOG = LoggerFactory.getLogger(Text.class);
122
123 protected List values = Collections.EMPTY_LIST;
124 protected String actualName;
125 protected String name;
126
127 public Text(ValueStack stack) {
128 super(stack);
129 }
130
131 @StrutsTagAttribute(description=" Name of resource property to fetch", required=true)
132 public void setName(String name) {
133 this.name = name;
134 }
135
136
137 public boolean usesBody() {
138
139
140
141 return true;
142 }
143
144 public boolean end(Writer writer, String body) {
145 actualName = findString(name, "name", "You must specify the i18n key. Example: welcome.header");
146 String defaultMessage;
147 if (TextUtils.stringSet(body)) {
148 defaultMessage = body;
149 } else {
150 defaultMessage = actualName;
151 }
152
153 String msg = TextProviderHelper.getText(actualName, defaultMessage, values, getStack());
154
155 if (msg != null) {
156 try {
157 if (getVar() == null) {
158 writer.write(msg);
159 } else {
160 putInContext(msg);
161 }
162 } catch (IOException e) {
163 LOG.error("Could not write out Text tag", e);
164 }
165 }
166
167 return super.end(writer, "");
168 }
169
170 public void addParameter(String key, Object value) {
171 addParameter(value);
172 }
173
174 public void addParameter(Object value) {
175 if (values.isEmpty()) {
176 values = new ArrayList(4);
177 }
178
179 values.add(value);
180 }
181 }