View Javadoc

1   package org.apache.turbine.services.localization;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 551164 2007-06-27 14:04:14Z seade $
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      protected 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     // ApplicationTool implmentation
169 
170     /***
171      * Sets the request to get the <code>Accept-Language</code> header
172      * from (reset on each request).
173      */
174     public void init(Object data)
175     {
176         if (data instanceof RunData)
177         {
178             // Pull necessary information out of RunData while we have
179             // a reference to it.
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 }