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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */16packageorg.apache.jetspeed.services.customlocalization;
1718import java.util.Locale;
1920import org.apache.turbine.services.localization.TurbineLocalizationService;
21import org.apache.turbine.util.RunData;
2223import org.apache.jetspeed.om.security.JetspeedUser;
2425/***26 * <p>This class is the single point of access to all localization27 * resources. It caches different ResourceBundles for different28 * 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 user34 * 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 */42publicclassJetspeedLocalizationServiceextends TurbineLocalizationService implements CustomLocalizationService43 {
4445/***46 * Initialize list of default bundle names.47 * 48 * @param ignored49 *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 else61 {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 */7475/***76 * Call getDefaultBundleName() of turbine77 * 78 * @return 79 */80/*81 public String getDefaultBundleName()82 {83 return bundleNames.length > 0 ? bundleNames[0] : "";84 }85*/8687/***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 data93 * @return 94 */95publicfinal Locale getLocale(RunData data)
96 {
97JetspeedUser user = (JetspeedUser) data.getUser();
98if (user == null)
99 {
100return getLocale(data.getRequest().getHeader(CustomLocalizationService.ACCEPT_LANGUAGE));
101 }
102else103 {
104 String lang = "null";
105106try107 {
108if (user.getPerm("language") == null)
109 {
110return getLocale(data.getRequest().getHeader(CustomLocalizationService.ACCEPT_LANGUAGE));
111 }
112else113 {
114 lang = user.getPerm("language").toString();
115 Locale locale = new Locale(lang, "");
116return locale;
117 }
118 }
119catch (Exception use)
120 {
121return getLocale(data.getRequest().getHeader(CustomLocalizationService.ACCEPT_LANGUAGE));
122 }
123 }
124 }
125126/***127 * Call searchKey(Locale locale, String key) to search the key in the Bundles128 * 129 * @param bundleName130 * @param locale131 * @param key132 * @return 133 *134 public String getString(String bundleName, Locale locale, String key)135 {136 return searchKey(locale,key);137 }138 */139140/***141 * Search the key in the first bundle, if is not found142 * search in the list of bundles143 * 144 * @param locale145 * @param key146 * @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 ;154155 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 }167168169 private String bundleNames[];170 */171172 }