View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.jetspeed.services.customlocalization;
17  
18  import java.util.Locale;
19  import java.util.ResourceBundle;
20  import java.util.MissingResourceException;
21  
22  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
23  import org.apache.jetspeed.services.logging.JetspeedLogger;
24  
25  import org.apache.turbine.services.pull.ApplicationTool;
26  import org.apache.turbine.util.RunData;
27  
28  
29  
30  /***
31   * Custom localization tool.
32   *
33   * @author <a href="mailto:desmax74@yahoo.it">Dessė Massimiliano</a>
34   * @author <a href="mailto:massimiliano.dessi@gruppoatlantis.it">Dessė Massimiliano</a>
35   * @author <a href="mailto:massimiliano.dessi@gruppoatlantis.com">Dessė Massimiliano</a>
36   * @version $Id: CustomLocalizationTool.java,v 1.5 2004/02/23 03:49:33 jford Exp $
37   */
38  public class CustomLocalizationTool implements ApplicationTool
39  {
40      /***
41       * Static initialization of the logger for this class
42       */    
43      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(CustomLocalizationTool.class.getName());    
44      
45      public CustomLocalizationTool()
46      {
47          refresh();
48      }
49  
50      public String get(String key)
51      {
52          try
53          {
54              String s = CustomLocalization.getString(getBundleName(null), getLocale(), key);
55              return s;
56          }
57          catch (MissingResourceException noKey)
58          {
59              logger.error("Exception", noKey);
60          }
61          return null;
62      }
63  
64      /***
65       * Gets localized key from the default bundle and the provided locale.
66       * Returns <code>null</code> if the resource is missing.
67       *
68       * @param key string to translate.
69       * @param locale prefered locale for translation.
70       * @return localized key.
71       */
72      public String get(String key, Locale locale)
73      {
74          if (locale == null)
75          {
76              locale = getLocale();
77          }
78  
79          String s = null;
80          try
81          {
82              s = CustomLocalization.getString(getBundleName(null), locale, key);
83          }
84          catch (MissingResourceException noKey)
85          {
86              logger.error("Exception", noKey);
87          }
88  
89          return s;
90      }
91  
92  
93      public Locale getLocale()
94      {
95          return locale;
96      }
97  
98      protected String getBundleName(Object data)
99      {
100         return CustomLocalization.getDefaultBundleName();
101     }
102 
103     public final void init(Object data)
104     {
105         if (data instanceof RunData)
106         {
107             locale = CustomLocalization.getLocale((RunData)data);
108             bundleName = getBundleName(data);
109         }
110     }
111 
112     public void refresh()
113     {
114         locale = null;
115         bundle = null;
116         bundleName = null;
117     }
118 
119     protected Locale locale;
120     private ResourceBundle bundle;
121     private String bundleName;
122     boolean debug;
123 }