1 package org.apache.turbine.services.localization;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.MissingResourceException;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.turbine.services.pull.ApplicationTool;
29 import org.apache.turbine.util.RunData;
30
31 /***
32 * A pull tool which provides lookups for localized text by delegating
33 * to the configured <code>LocalizationService</code>.
34 *
35 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
36 * @author <a href="mailto:jon@collab.net">Jon Stevens</a>
37 * @version $Id: LocalizationTool.java 534527 2007-05-02 16:10:59Z tv $
38 */
39 public class LocalizationTool implements ApplicationTool
40 {
41 /*** Logging */
42 private static Log log = LogFactory.getLog(LocalizationTool.class);
43
44 /***
45 * The language and country information parsed from the request's
46 * <code>Accept-Language</code> header. Reset on each request.
47 */
48 protected Locale locale;
49
50 /***
51 * The name of the bundle for this tool to use.
52 */
53 private String bundleName;
54
55 /***
56 * Creates a new instance. Used by <code>PullService</code>.
57 */
58 public LocalizationTool()
59 {
60 refresh();
61 }
62
63 /***
64 * <p>Performs text lookups for localization.</p>
65 *
66 * <p>Assuming there is a instance of this class with a HTTP
67 * request set in your template's context named <code>l10n</code>,
68 * the VTL <code>$l10n.HELLO</code> would render to
69 * <code>hello</code> for English requests and <code>hola</code>
70 * in Spanish (depending on the value of the HTTP request's
71 * <code>Accept-Language</code> header).</p>
72 *
73 * @param key The identifier for the localized text to retrieve.
74 * @return The localized text.
75 */
76 public String get(String key)
77 {
78 try
79 {
80 return Localization.getString(getBundleName(null), getLocale(), key);
81 }
82 catch (MissingResourceException noKey)
83 {
84 log.error(noKey);
85 return null;
86 }
87 }
88
89 /***
90 * Gets the current locale.
91 *
92 * @return The locale currently in use.
93 */
94 public Locale getLocale()
95 {
96 return locale;
97 }
98
99 /***
100 * The return value of this method is used to set the name of the
101 * bundle used by this tool. Useful as a hook for using a
102 * different bundle than specifed in your
103 * <code>LocalizationService</code> configuration.
104 *
105 * @param data The inputs passed from {@link #init(Object)}.
106 * (ignored by this implementation).
107 */
108 protected String getBundleName(Object data)
109 {
110 return bundleName;
111 }
112
113 /***
114 * Formats a localized value using the provided object.
115 *
116 * @param key The identifier for the localized text to retrieve,
117 * @param arg1 The object to use as {0} when formatting the localized text.
118 * @return Formatted localized text.
119 * @see #format(String, Locale, String, Object[])
120 */
121 public String format(String key, Object arg1)
122 {
123 return Localization.format(getBundleName(null), getLocale(), key, arg1);
124 }
125
126 /***
127 * Formats a localized value using the provided objects.
128 *
129 * @param key The identifier for the localized text to retrieve,
130 * @param arg1 The object to use as {0} when formatting the localized text.
131 * @param arg2 The object to use as {1} when formatting the localized text.
132 * @return Formatted localized text.
133 * @see #format(String, Locale, String, Object[])
134 */
135 public String format(String key, Object arg1, Object arg2)
136 {
137 return Localization.format(getBundleName(null), getLocale(), key, arg1, arg2);
138 }
139
140 /***
141 * Formats a localized value using the provided objects.
142 *
143 * @param key The identifier for the localized text to retrieve,
144 * @param args The objects to use as {0}, {1}, etc. when
145 * formatting the localized text.
146 * @return Formatted localized text.
147 */
148 public String format(String key, Object[] args)
149 {
150 return Localization.format(getBundleName(null), getLocale(), key, args);
151 }
152
153 /***
154 * Formats a localized value using the provided objects. This variation
155 * allows for a List so that the velocity ["arg1", "arg2", "arg3"] syntax
156 * is supported.
157 *
158 * @param key The identifier for the localized text to retrieve,
159 * @param args The objects to use as {0}, {1}, etc. when
160 * formatting the localized text.
161 * @return Formatted localized text.
162 */
163 public String format(String key, List args)
164 {
165 return Localization.format(getBundleName(null), getLocale(), key, args.toArray());
166 }
167
168
169
170 /***
171 * Sets the request to get the <code>Accept-Language</code> header
172 * from (reset on each request).
173 */
174 public final void init(Object data)
175 {
176 if (data instanceof RunData)
177 {
178
179
180 locale = Localization.getLocale(((RunData) data).getRequest());
181 bundleName = Localization.getDefaultBundleName();
182 }
183 }
184
185 /***
186 * No-op.
187 */
188 public void refresh()
189 {
190 locale = null;
191 bundleName = null;
192 }
193 }