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  
20  import org.apache.turbine.services.localization.TurbineLocalizationService;
21  import org.apache.turbine.util.RunData;
22  
23  import org.apache.jetspeed.om.security.JetspeedUser;
24  
25  /***
26   * <p>This class is the single point of access to all localization
27   * resources.  It caches different ResourceBundles for different
28   * Locales.</p>
29   * 
30   * Work in the same way of turbine except for getLocale(RunData data),
31   * turbine read the accept-language header in a http request,
32   * instead this method read the user.getPerm("language")
33   * from the RunData to obtain the language choice by the user
34   * without the browser language rule.
35   * If a user not change the language with a ChangeLanguagePortlet,
36   * and a user.getPerm("language")are not set,
37   * the "Accept-Language" header are read.
38   * 
39   * @author <a href="mailto:desmax74@yahoo.it">Dessė Massimiliano</a>
40   * @version $Id: JetspeedLocalizationService.java,v 1.8 2004/02/23 03:49:33 jford Exp $
41   */
42  public class JetspeedLocalizationService extends TurbineLocalizationService implements CustomLocalizationService
43  {
44  
45      /***
46       * Initialize list of default bundle names.
47       * 
48       * @param ignored
49       *
50      protected void initBundleNames(String ignored[])
51      {
52          bundleNames = TurbineResources.getStringArray("locale.default.bundles");
53          String name = TurbineResources.getString("locale.default.bundle");
54          if (name != null && name.length() > 0)
55          {
56              if (bundleNames == null || bundleNames.length <= 0)
57              {
58                  bundleNames = (new String[] {name});
59              }
60              else
61              {
62                  String array[] = new String[bundleNames.length + 1];
63                  array[0] = name;
64                  System.arraycopy(bundleNames, 0, array, 1, bundleNames.length);
65                  bundleNames = array;
66              }
67          }
68          if (bundleNames == null)
69          {
70              bundleNames = new String[0];
71          }
72      }
73      */
74      
75      /***
76       * Call getDefaultBundleName() of turbine
77       * 
78       * @return 
79       */
80  /*    
81      public String getDefaultBundleName()
82      {
83          return bundleNames.length > 0 ? bundleNames[0] : "";
84      }
85  */
86  
87      /***
88       * This method read if a user has set getPerm("language")
89       * to use another language or not.
90       * If not set , accept-language of the request are returned.
91       * 
92       * @param data
93       * @return 
94       */
95      public final Locale getLocale(RunData data)
96      {
97          JetspeedUser user = (JetspeedUser) data.getUser();
98          if (user == null)
99          {
100             return getLocale(data.getRequest().getHeader(CustomLocalizationService.ACCEPT_LANGUAGE));
101         }
102         else
103         {
104             String lang = "null";
105 
106             try
107             {
108                 if (user.getPerm("language") == null)
109                 {
110                     return getLocale(data.getRequest().getHeader(CustomLocalizationService.ACCEPT_LANGUAGE));
111                 }
112                 else
113                 {
114                     lang = user.getPerm("language").toString();
115                     Locale locale = new Locale(lang, "");
116                     return locale;
117                 }
118             }
119             catch (Exception use)
120             {
121                 return getLocale(data.getRequest().getHeader(CustomLocalizationService.ACCEPT_LANGUAGE));
122             }
123         }
124     }
125 
126     /***
127      * Call searchKey(Locale locale, String key) to search the key in the Bundles
128      * 
129      * @param bundleName
130      * @param locale
131      * @param key
132      * @return 
133      *
134     public String getString(String bundleName, Locale locale, String key)
135     {
136         return searchKey(locale,key);
137     }
138     */
139 
140     /***
141      * Search the key in the first bundle, if is not found
142      * search in the list of bundles
143      * 
144      * @param locale
145      * @param key
146      * @return 
147      *
148     private  String searchKey(Locale locale, String key)
149     {
150         String keyTemp=null;
151         int i=0;
152         boolean find=false;
153         ResourceBundle rb ;
154 
155         while ((null==keyTemp)&&(!find)&&(i<bundleNames.length))
156         {
157             rb = getBundle(bundleNames[i], locale);
158             keyTemp=super.getStringOrNull(rb,key);
159             if (keyTemp!=null)
160             {
161                 find=true;
162             }
163             else i++;
164         }
165         return keyTemp;
166     }
167 
168 
169     private String bundleNames[];
170     */
171 
172 }