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 import java.util.Locale;
20 import java.util.MissingResourceException;
21 import java.util.ResourceBundle;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.turbine.services.pull.ApplicationTool;
26 import org.apache.turbine.util.RunData;
27
28 /***
29 * A pull tool which provides lookups for localized text by delegating
30 * to the configured <code>LocalizationService</code>.
31 *
32 * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
33 * @author <a href="mailto:jon@collab.net">Jon Stevens</a>
34 * @version $Id: LocalizationTool.java 264148 2005-08-29 14:21:04Z henning $
35 */
36 public class LocalizationTool implements ApplicationTool
37 {
38 /*** Logging */
39 private static Log log = LogFactory.getLog(LocalizationTool.class);
40
41 /***
42 * The language and country information parsed from the request's
43 * <code>Accept-Language</code> header. Reset on each request.
44 */
45 protected Locale locale;
46
47 /***
48 * The bundle for this request.
49 */
50 private ResourceBundle bundle;
51
52 /***
53 * The name of the bundle for this tool to use.
54 */
55 private String bundleName;
56
57 /***
58 * Creates a new instance. Used by <code>PullService</code>.
59 */
60 public LocalizationTool()
61 {
62 refresh();
63 }
64
65 /***
66 * <p>Performs text lookups for localization.</p>
67 *
68 * <p>Assuming there is a instance of this class with a HTTP
69 * request set in your template's context named <code>l10n</code>,
70 * the VTL <code>$l10n.HELLO</code> would render to
71 * <code>hello</code> for English requests and <code>hola</code>
72 * in Spanish (depending on the value of the HTTP request's
73 * <code>Accept-Language</code> header).</p>
74 *
75 * @param key The identifier for the localized text to retrieve.
76 * @return The localized text.
77 */
78 public String get(String key)
79 {
80 try
81 {
82 return Localization.getString(getBundleName(null), getLocale(), key);
83 }
84 catch (MissingResourceException noKey)
85 {
86 log.error(noKey);
87 return null;
88 }
89 }
90
91 /***
92 * Gets the current locale.
93 *
94 * @return The locale currently in use.
95 */
96 public Locale getLocale()
97 {
98 return locale;
99 }
100
101 /***
102 * The return value of this method is used to set the name of the
103 * bundle used by this tool. Useful as a hook for using a
104 * different bundle than specifed in your
105 * <code>LocalizationService</code> configuration.
106 *
107 * @param data The inputs passed from {@link #init(Object)}.
108 * (ignored by this implementation).
109 */
110 protected String getBundleName(Object data)
111 {
112 return Localization.getDefaultBundleName();
113 }
114
115
116
117
118 /***
119 * Sets the request to get the <code>Accept-Language</code> header
120 * from (reset on each request).
121 */
122 public final void init(Object data)
123 {
124 if (data instanceof RunData)
125 {
126
127
128 locale = Localization.getLocale(((RunData) data).getRequest());
129 bundleName = getBundleName(data);
130 }
131 }
132
133 /***
134 * No-op.
135 */
136 public void refresh()
137 {
138 locale = null;
139 bundle = null;
140 bundleName = null;
141 }
142 }