1 6 package org.roller.presentation; 7 8 import java.security.Principal ; 9 import java.util.Arrays ; 10 import java.util.Locale ; 11 import java.util.StringTokenizer ; 12 import java.util.Vector ; 13 14 import javax.servlet.ServletContext ; 15 import javax.servlet.http.HttpServletRequest ; 16 17 import org.apache.commons.logging.Log; 18 import org.apache.commons.logging.LogFactory; 19 import org.apache.struts.Globals; 20 import org.roller.pojos.WebsiteData; 21 22 29 public class LanguageUtil 30 { 31 private static Log mLogger = 32 LogFactory.getFactory().getInstance(LanguageUtil.class); 33 34 37 public static final String SUPPORTED_LANGUAGES = 38 "org.roller.presentation.supported.languages"; 39 40 47 public static Locale [] getSupportedLanguages(ServletContext ctx) 48 { 49 return (Locale []) ctx.getAttribute(SUPPORTED_LANGUAGES); 50 } 51 52 60 public static boolean isSupported(String lang, ServletContext ctx) 61 { 62 return isSupported(createLocale(lang), ctx); 63 } 64 65 75 public static boolean isSupported(Locale locale, ServletContext ctx) 76 { 77 mLogger.debug("isSupported( locale = "+locale+" )"); 78 boolean isSupported = false; 79 Locale [] supportedLanguages = getSupportedLanguages(ctx); 80 81 if (supportedLanguages == null) 82 { 83 return false; 84 } 85 86 for (int i=0; i<supportedLanguages.length; i++) 87 { 88 Locale l = (Locale ) supportedLanguages[i]; 89 if (l.equals(locale)) { 90 isSupported = true; 91 break; 92 } else { 93 Locale langOnly = new Locale (locale.getLanguage()); 94 if (l.equals(langOnly)) { 95 isSupported = true; 96 break; 97 } 98 } 99 } 100 101 return isSupported; 102 } 103 104 124 public static Locale getViewLocale(HttpServletRequest request) 125 { 126 mLogger.debug("getViewLocale()"); 127 Locale viewLocale = null; 128 129 if (isWebsiteOfPrincipal(request)) 131 { 132 viewLocale = getDefaultLocale(request); 133 mLogger.debug("websiteLocale = "+viewLocale); 134 } 135 else if (request.getSession(false) != null) 136 { 137 viewLocale = (Locale ) request.getSession().getAttribute(Globals.LOCALE_KEY); 139 mLogger.debug("sessionLocale = "+viewLocale); 140 } 141 142 if (viewLocale == null) 144 { 145 viewLocale = request.getLocale(); 147 mLogger.debug("requestLocale = "+viewLocale); 148 149 if (viewLocale == null) 151 { 152 viewLocale = getDefaultLocale(request); 153 mLogger.debug("defaultLocale = "+viewLocale); 154 } 155 } 156 157 164 ServletContext ctx = RollerContext.getServletContext(); 165 if (!LanguageUtil.isSupported(viewLocale, ctx)) 166 { 167 viewLocale = Locale.getDefault(); 168 } 169 mLogger.debug("return Locale = "+viewLocale); 170 171 request.getSession().setAttribute(Globals.LOCALE_KEY, viewLocale); 173 return viewLocale; 174 } 175 176 186 public static Locale getDefaultLocale(HttpServletRequest request) { 187 mLogger.debug("getDefaultLocale()"); 188 RollerRequest rreq = RollerRequest.getRollerRequest(request); 189 WebsiteData website = rreq.getWebsite(); 190 if (website==null) { 191 return null; 192 } else { 193 return website.getLocaleInstance(); 194 } 195 } 196 197 212 public static boolean isWebsiteOfPrincipal(HttpServletRequest request) { 213 boolean result = false; 214 RollerRequest rreq = RollerRequest.getRollerRequest(request); 215 if (rreq != null) { 216 WebsiteData website = rreq.getWebsite(); 217 Principal principal = request.getUserPrincipal(); 218 if (website != null && principal != null) { 219 return website.getUser().getUserName().equals(principal.getName()); 220 } 221 } 222 return result; 223 } 224 225 232 public static Locale createLocale(String lang) { 233 mLogger.debug("createLocale( lang = "+lang+" )"); 234 Locale l = null; 235 if (lang.indexOf("_")>0) { 236 String tmpLang = lang.substring(0, lang.indexOf("_")); 237 String tmpCoun = lang.substring(lang.indexOf("_")+1, lang.length()); 238 l = new Locale (tmpLang, tmpCoun); 239 } else 240 { 241 l = new Locale (lang); 242 } 243 return l; 244 } 245 246 253 public static Locale [] extractLanguages(String supportedLanguages) { 254 mLogger.debug("extractLanguages( lang = "+supportedLanguages+" )"); 255 256 Vector langs = new Vector (); 257 StringTokenizer st = new StringTokenizer (supportedLanguages, ","); 258 while (st.hasMoreTokens()) 259 { 260 Locale l = LanguageUtil.createLocale(st.nextToken()); 261 langs.add(l); 262 } 263 mLogger.debug("supported languages: "+langs); 264 265 return (Locale []) langs.toArray(new Locale [langs.size()]); 266 } 267 268 } 269 | Popular Tags |