1 20 package org.enhydra.barracuda.plankton.l10n; 21 22 import java.util.*; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 26 import org.apache.log4j.*; 27 28 import org.enhydra.barracuda.plankton.http.*; 29 30 31 37 public class Locales { 38 39 public static String LANGAUGE_KEY = "$loc_lang"; 41 public static String COUNTRY_KEY = "$loc_cntry"; 42 public static String VARIANT_KEY = "$loc_var"; 43 44 public static final int NONE = 0; 46 public static final int SESSION = 1; 47 public static final int COOKIES_AND_SESSION = 2; 48 49 public static int PERSIST_DEFAULT = SESSION; 51 52 protected static final Logger logger = Logger.getLogger(Locales.class.getName()); 53 private static final String LOCALE = Localize.class.getName()+".Locale"; 54 55 64 public static Locale getClientLocale(HttpServletRequest req, HttpServletResponse resp) { 65 return getClientLocale(req, resp, LANGAUGE_KEY, COUNTRY_KEY, VARIANT_KEY, PERSIST_DEFAULT); 66 } 67 68 97 public static Locale getClientLocale(HttpServletRequest req, HttpServletResponse resp, String languageKey, String countryKey, String variantKey, int persistOption) { 98 Locale loc = null; 99 if (req!=null) { 100 if (loc==null) { 103 String language = req.getParameter(languageKey); 104 String country = req.getParameter(countryKey); 105 String variant = req.getParameter(variantKey); 106 if (language!=null) { 107 if (logger.isDebugEnabled()) logger.debug("Found locale in req params"); 108 loc = getLocale(language, country, variant); 109 saveClientLocale(req, resp, loc, persistOption); 110 } 111 } 112 113 if (loc==null && persistOption>=SESSION) { 116 HttpSession session = SessionServices.getSession(req); 117 if (session!=null) { 118 if (logger.isDebugEnabled()) logger.debug("Found locale in session"); 119 loc = (Locale) session.getAttribute(LOCALE); 120 saveClientLocale(req, resp, loc, persistOption); 121 } 122 } 123 124 if (loc==null && persistOption>=COOKIES_AND_SESSION) { 127 Cookie[] cookies = req.getCookies(); 128 if (cookies!=null) { 129 for (int i=0, max=cookies.length; i<max; i++) { 130 if (cookies[i].getName().equals(LOCALE)) { 131 if (logger.isDebugEnabled()) logger.debug("Found locale in cookie"); 132 loc = readLocaleFromCookie(cookies[i]); 133 saveClientLocale(req, resp, loc, persistOption); 134 } 135 } 136 } 137 } 138 139 if (loc==null) { 143 if (logger.isDebugEnabled()) logger.debug("Getting it from servlet request"); 144 loc = req.getLocale(); 145 saveClientLocale(req, resp, loc, persistOption); 146 } 147 } 148 if (loc==null) { 151 if (logger.isDebugEnabled()) logger.debug("Finding default locale"); 152 loc = Locale.getDefault(); 153 saveClientLocale(req, resp, loc, persistOption); 154 } 155 if (logger.isDebugEnabled()) logger.debug("return locale "+loc); 156 return loc; 157 } 158 159 private static Locale getLocale(String language, String country, String variant) { 160 Locale locale = null; 161 if (language!=null) { 162 if (country==null) country = ""; 163 if (variant!=null) locale = new Locale(language, country, variant); 164 else locale = new Locale(language, country); 165 } 166 return locale; 167 } 168 169 170 private static Locale readLocaleFromCookie(Cookie cookie) { 171 String value = cookie.getValue(); 172 String language = null; 173 String country = null; 174 String variant = null; 175 StringTokenizer st = new StringTokenizer(value,","); 176 if (st.hasMoreTokens()) language = st.nextToken(); 177 if (st.hasMoreTokens()) country = st.nextToken(); 178 if (st.hasMoreTokens()) variant = st.nextToken(); 179 Locale locale = getLocale(language, country, variant); 180 if (logger.isDebugEnabled()) logger.debug("Got locale "+locale+" from cookie! (value="+value+")"); 181 return locale; 182 } 183 184 private static void writeLocaleInSession(HttpServletRequest req, Locale locale) { 185 if (req==null) return; 186 HttpSession session = SessionServices.getSession(req); 187 if (locale!=null) session.setAttribute(LOCALE, locale); 188 else session.removeAttribute(LOCALE); 189 if (logger.isDebugEnabled()) logger.debug("updated locale: "+locale+" in session!"); 190 } 191 192 private static void writeLocaleInCookie(HttpServletResponse resp, Locale locale) { 193 if (resp==null) return; 194 String value = null; 195 if (locale!=null) value = locale.getLanguage()+","+locale.getCountry()+","+locale.getVariant(); 196 Cookie cookie = new Cookie(LOCALE, value); 197 cookie.setMaxAge(locale!=null ? Integer.MAX_VALUE : 0); 198 resp.addCookie(cookie); 199 if (logger.isDebugEnabled()) logger.debug("updated locale: "+locale+" in cookie!"); 200 } 201 202 211 public static void saveClientLocale(HttpServletRequest req, HttpServletResponse resp, Locale loc) { 212 saveClientLocale(req, resp, loc, PERSIST_DEFAULT); 213 } 214 215 225 public static void saveClientLocale(HttpServletRequest req, HttpServletResponse resp, Locale loc, int persistOption) { 226 if (persistOption>=SESSION) writeLocaleInSession(req, loc); 228 229 if (persistOption>=COOKIES_AND_SESSION) writeLocaleInCookie(resp, loc); 231 } 232 233 241 public static void releaseClientLocale(HttpServletRequest req, HttpServletResponse resp) { 242 releaseClientLocale(req, resp, PERSIST_DEFAULT); 243 } 244 245 255 public static void releaseClientLocale(HttpServletRequest req, HttpServletResponse resp, int persistOption) { 256 if (persistOption>=SESSION) writeLocaleInSession(req, null); 258 259 if (persistOption>=COOKIES_AND_SESSION) writeLocaleInCookie(resp, null); 261 } 262 263 273 public static int findClosestLocale(Locale targetLocale, Locale[] locales, int defaultIndex) { 274 int match2 = -1; 275 int match1 = -1; 276 277 for (int i=0, max=locales.length; i<max; i++) { 278 if (targetLocale.equals(locales[i])) return i; 280 281 if (targetLocale.getLanguage().equals(locales[i].getLanguage()) && 283 targetLocale.getCountry().equals(locales[i].getCountry())) match2 = i; 284 285 if (targetLocale.getLanguage().equals(locales[i].getLanguage())) match1 = i; 287 } 288 if (match2!=-1) return match2; 289 else if (match1!=-1) return match1; 290 else return defaultIndex; 291 } 292 293 } 294 | Popular Tags |