KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > localization > LocalizationTool


1 package org.apache.turbine.services.localization;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.Locale JavaDoc;
20 import java.util.MissingResourceException JavaDoc;
21 import java.util.ResourceBundle JavaDoc;
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,v 1.7.2.2 2004/05/20 03:06:51 seade Exp $
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 JavaDoc locale;
46
47     /**
48      * The bundle for this request.
49      */

50     private ResourceBundle JavaDoc bundle;
51
52     /**
53      * The name of the bundle for this tool to use.
54      */

55     private String JavaDoc 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 JavaDoc get(String JavaDoc key)
79     {
80         try
81         {
82             return Localization.getString(getBundleName(null), getLocale(), key);
83         }
84         catch (MissingResourceException JavaDoc 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 JavaDoc 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 JavaDoc getBundleName(Object JavaDoc data)
111     {
112         return Localization.getDefaultBundleName();
113     }
114
115
116     // ApplicationTool implmentation
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 JavaDoc data)
123     {
124         if (data instanceof RunData)
125         {
126             // Pull necessary information out of RunData while we have
127
// a reference to it.
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 }
143
Popular Tags