1 package org.tigris.scarab.pipeline; 2 3 48 49 import java.io.IOException ; 50 import java.util.Locale ; 51 52 import javax.servlet.http.HttpServletRequest ; 53 54 import org.apache.fulcrum.localization.Localization; 55 import org.apache.fulcrum.localization.LocalizationService; 56 import org.apache.log4j.Logger; 57 import org.apache.turbine.RunData; 58 import org.apache.turbine.TurbineException; 59 import org.apache.turbine.pipeline.AbstractValve; 60 import org.apache.turbine.ValveContext; 61 import org.tigris.scarab.om.ScarabUser; 62 import org.tigris.scarab.om.UserPreference; 63 import org.tigris.scarab.om.UserPreferenceManager; 64 65 72 public class SetLocaleValve extends AbstractValve 73 { 74 private static final Logger LOG = Logger.getLogger(SetLocaleValve.class); 75 76 79 public void invoke(RunData data, ValveContext context) 80 throws IOException , TurbineException 81 { 82 ScarabUser user = (ScarabUser) data.getUser(); 83 84 if (user != null && user.getUserId() != null) 86 { 87 try 88 { 89 String acceptLanguage = LocalizationService.ACCEPT_LANGUAGE; 90 HttpServletRequest request = data.getRequest(); 91 String browserLocaleAsString = 92 request.getHeader(acceptLanguage); 93 94 Locale userLocale = user.getLocale(); 95 Locale browserLocale = 96 Localization.getLocale(browserLocaleAsString); 97 98 if (!userLocale.equals(browserLocale)) 99 { 100 storePreferredLocale(user, browserLocaleAsString); 101 } 102 } 103 catch (Exception e) 104 { 105 LOG.warn( 106 "Could not set locale info for user [" 107 + user.getName() 108 + "]"); 109 } 110 } 111 context.invokeNext(data); 113 } 114 115 122 private void storePreferredLocale(ScarabUser user, String localeAsString) 123 throws Exception 124 { 125 UserPreference pref = 127 UserPreferenceManager.getInstance(user.getUserId()); 128 pref.setLocale(localeAsString); 129 pref.save(); 130 131 Locale locale = Localization.getLocale(localeAsString); 134 user.setLocale(locale); 135 } 136 } 137 | Popular Tags |