1 16 package org.apache.cocoon.acting; 17 18 import org.apache.avalon.framework.configuration.Configurable; 19 import org.apache.avalon.framework.configuration.Configuration; 20 import org.apache.avalon.framework.configuration.ConfigurationException; 21 import org.apache.avalon.framework.parameters.Parameters; 22 import org.apache.avalon.framework.thread.ThreadSafe; 23 24 import org.apache.cocoon.environment.Redirector; 25 import org.apache.cocoon.environment.SourceResolver; 26 import org.apache.cocoon.i18n.I18nUtils; 27 28 import java.util.HashMap ; 29 import java.util.Locale ; 30 import java.util.Map ; 31 32 120 public class LocaleAction extends ServiceableAction implements ThreadSafe, Configurable { 121 122 private static final String DEFAULT_DEFAULT_LANG = "en"; 123 private static final String DEFAULT_DEFAULT_COUNTRY = "US"; 124 private static final String DEFAULT_DEFAULT_VARIANT = ""; 125 126 129 public static final String LOCALE = "locale"; 130 131 134 public static final String LOCALE_ATTR = "locale-attribute"; 135 136 137 140 public static final String STORE_REQUEST = "store-in-request"; 141 142 145 public static final String CREATE_SESSION = "create-session"; 146 147 150 public static final String STORE_SESSION = "store-in-session"; 151 152 155 public static final String STORE_COOKIE = "store-in-cookie"; 156 157 158 161 private String localeAttribute; 162 163 166 private boolean useLocale; 167 168 171 private Locale defaultLocale; 172 173 176 private boolean storeInRequest; 177 178 181 private boolean storeInSession; 182 183 186 private boolean createSession; 187 188 191 private boolean storeInCookie; 192 193 198 public void configure(Configuration config) 199 throws ConfigurationException { 200 this.storeInRequest = config.getChild(STORE_REQUEST).getValueAsBoolean(false); 201 this.createSession = config.getChild(CREATE_SESSION).getValueAsBoolean(false); 202 this.storeInSession = config.getChild(STORE_SESSION).getValueAsBoolean(false); 203 this.storeInCookie = config.getChild(STORE_COOKIE).getValueAsBoolean(false); 204 if (getLogger().isDebugEnabled()) { 205 getLogger().debug((this.storeInRequest ? "will" : "won't") + " set values in request"); 206 getLogger().debug((this.createSession ? "will" : "won't") + " create session"); 207 getLogger().debug((this.storeInSession ? "will" : "won't") + " set values in session"); 208 getLogger().debug((this.storeInCookie ? "will" : "won't") + " set values in cookies"); 209 } 210 211 this.localeAttribute = config.getChild(LOCALE_ATTR).getValue(LOCALE); 212 this.useLocale = config.getChild("use-locale").getValueAsBoolean(true); 213 214 Configuration child = config.getChild("default-locale", false); 215 if (child != null) { 216 this.defaultLocale = new Locale (child.getAttribute("language", DEFAULT_DEFAULT_LANG), 217 child.getAttribute("country", DEFAULT_DEFAULT_COUNTRY), 218 child.getAttribute("variant", DEFAULT_DEFAULT_VARIANT)); 219 } 220 221 if (getLogger().isDebugEnabled()) { 222 getLogger().debug("Locale attribute name is " + this.localeAttribute); 223 getLogger().debug((this.useLocale ? "will" : "won't") + " use request locale"); 224 getLogger().debug("default locale " + this.defaultLocale); 225 } 226 } 227 228 232 public Map act(Redirector redirector, 233 SourceResolver resolver, 234 Map objectModel, 235 String source, 236 Parameters params) 237 throws Exception { 238 Locale locale = I18nUtils.findLocale(objectModel, 240 localeAttribute, 241 params, 242 defaultLocale, 243 useLocale); 244 245 if (locale == null) { 246 if (getLogger().isDebugEnabled()) { 247 getLogger().debug("No locale found."); 248 } 249 250 return null; 251 } 252 253 String localeStr = locale.toString(); 254 if (getLogger().isDebugEnabled()) { 255 getLogger().debug("Found locale: " + localeStr); 256 } 257 258 I18nUtils.storeLocale(objectModel, 259 localeAttribute, 260 localeStr, 261 storeInRequest, 262 storeInSession, 263 storeInCookie, 264 createSession); 265 266 Map map = new HashMap (); 268 map.put("language", locale.getLanguage()); 269 map.put("country", locale.getCountry()); 270 map.put("variant", locale.getVariant()); 271 map.put("locale", localeStr); 272 return map; 273 } 274 275 283 public static String getLocaleAttribute(Map objectModel, 284 String localeAttrName) { 285 Locale locale = I18nUtils.findLocale(objectModel, 286 localeAttrName, 287 null, 288 null, 289 true); 290 return locale.toString(); 291 } 292 } 293 | Popular Tags |