1 16 package org.apache.cocoon.i18n; 17 18 import org.apache.avalon.framework.parameters.Parameters; 19 20 import org.apache.cocoon.environment.Cookie; 21 import org.apache.cocoon.environment.ObjectModelHelper; 22 import org.apache.cocoon.environment.Request; 23 import org.apache.cocoon.environment.Session; 24 import org.apache.cocoon.environment.Response; 25 26 import java.util.Enumeration ; 27 import java.util.Locale ; 28 import java.util.Map ; 29 import java.util.StringTokenizer ; 30 31 38 public class I18nUtils { 39 40 private static final String LOCALE_DELIMITER = "_-@."; 42 43 private I18nUtils() { 44 } 46 47 55 public static Locale parseLocale(String localeString, Locale defaultLocale) { 56 if (localeString != null) { 57 StringTokenizer st = new StringTokenizer (localeString, 58 LOCALE_DELIMITER); 59 String l = st.hasMoreElements() ? st.nextToken() 60 : defaultLocale.getLanguage(); 61 String c = st.hasMoreElements() ? st.nextToken() : ""; 62 String v = st.hasMoreElements() ? st.nextToken() : ""; 63 return new Locale (l, c, v); 64 } 65 66 return defaultLocale; 67 } 68 69 79 public static Locale parseLocale(String localeString) { 80 return parseLocale(localeString, Locale.getDefault()); 81 } 82 83 84 89 public interface LocaleValidator { 90 91 96 public boolean test(String name, Locale locale); 97 } 98 99 104 public static Locale findLocale(Map objectModel, 105 String attribute, 106 Parameters parameters, 107 Locale defaultLocale, 108 boolean useLocale, 109 boolean useLocales, 110 boolean useBlankLocale, 111 LocaleValidator test) { 112 String localeStr; 113 Locale locale; 114 115 Request request = ObjectModelHelper.getRequest(objectModel); 116 117 localeStr = request.getParameter(attribute); 119 if (localeStr != null) { 120 locale = parseLocale(localeStr); 121 if (test == null || test.test("request", locale)) { 122 return locale; 123 } 124 } 125 126 Session session = request.getSession(false); 128 if (session != null && 129 ((localeStr = (String ) session.getAttribute(attribute)) != null)) { 130 locale = parseLocale(localeStr); 131 if (test == null || test.test("session", locale)) { 132 return locale; 133 } 134 } 135 136 Cookie[] cookies = request.getCookies(); 138 if (cookies != null) { 139 for (int i = 0; i < cookies.length; i++) { 140 Cookie cookie = cookies[i]; 141 if (cookie.getName().equals(attribute)) { 142 localeStr = cookie.getValue(); 143 locale = parseLocale(localeStr); 144 if (test == null || test.test("cookie", locale)) { 145 return locale; 146 } 147 break; 148 } 149 } 150 } 151 152 if (parameters != null) { 154 localeStr = parameters.getParameter("locale", null); 155 if (localeStr != null) { 156 locale = parseLocale(localeStr); 157 if (test == null || test.test("sitemap", locale)) { 158 return locale; 159 } 160 } 161 } 162 163 if (useLocale && !useLocales) { 165 locale = request.getLocale(); 166 if (test == null || test.test("request", locale)) { 167 return locale; 168 } 169 } 170 if (useLocales) { 171 Enumeration locales = request.getLocales(); 172 while (locales.hasMoreElements()) { 173 locale = (Locale )locales.nextElement(); 174 if (test == null || test.test("request", locale)) { 175 return locale; 176 } 177 } 178 } 179 180 if (defaultLocale != null) { 182 locale = defaultLocale; 183 if (test == null || test.test("default", locale)) { 184 return locale; 185 } 186 } 187 188 if (useBlankLocale) { 190 locale = new Locale ("", ""); if (test == null || test.test("blank", locale)) { 192 return locale; 193 } 194 } 195 196 return null; 198 } 199 200 205 public static Locale findLocale(Map objectModel, 206 String attribute, 207 Parameters parameters, 208 Locale defaultLocale, 209 boolean useLocale) { 210 return findLocale(objectModel, attribute, parameters, defaultLocale, useLocale, false, false, null); 211 } 212 213 217 public static void storeLocale(Map objectModel, 218 String attribute, 219 String locale, 220 boolean storeInRequest, 221 boolean storeInSession, 222 boolean storeInCookie, 223 boolean createSession) { 224 if (storeInRequest) { 226 Request request = ObjectModelHelper.getRequest(objectModel); 227 request.setAttribute(attribute, locale); 228 } 229 230 if (storeInSession) { 232 Request request = ObjectModelHelper.getRequest(objectModel); 233 Session session = request.getSession(createSession); 234 if (session != null) { 235 session.setAttribute(attribute, locale); 236 } 237 } 238 239 if (storeInCookie) { 241 Response response = ObjectModelHelper.getResponse(objectModel); 242 response.addCookie(response.createCookie(attribute, locale)); 243 } 244 } 245 } 246 | Popular Tags |