LanguageSelector

Speichern der ausgewählten Sprache in der HttpSession

git-svn-id: https://svn.libreccm.org/ccm/trunk@1149 8810af33-2d31-482b-a856-94f89814c4df
master
quasi 2011-10-07 16:51:35 +00:00
parent 4e670ab7a8
commit 2c2d02c64b
1 changed files with 48 additions and 9 deletions

View File

@ -11,6 +11,8 @@ import java.util.Enumeration;
import javax.servlet.ServletRequest; import javax.servlet.ServletRequest;
import java.util.Locale; import java.util.Locale;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/** /**
* *
@ -90,19 +92,56 @@ public class GlobalizationHelper {
* @return the selected locale as java.util.Locale or null if not defined * @return the selected locale as java.util.Locale or null if not defined
*/ */
public static Locale getSelectedLocale(ServletRequest request) { public static Locale getSelectedLocale(ServletRequest request) {
// ServletRequest request = ((ServletRequest) DispatcherHelper.getRequest());
String paramValue = request.getParameter(LANG_PARAM); // Return value
java.util.Locale selectedLocale = null; java.util.Locale selectedLocale = null;
if (paramValue != null) { // Access current HttpSession or create a new one, if none exist
StringTokenizer paramValues = new StringTokenizer(paramValue, "_"); HttpSession session = ((HttpServletRequest) request).getSession(true);
if (paramValues.countTokens() > 1) { // Get the session stored language string
selectedLocale = new java.util.Locale(paramValues.nextToken(), paramValues.nextToken()); String selectedSessionLang = (String) session.getAttribute(LANG_PARAM);
} else { // Get the request langauge string
selectedLocale = new java.util.Locale(paramValues.nextToken()); String selectedRequestLang = request.getParameter(LANG_PARAM);
// If there is a request language string, then this will have priority
// because this will only be the case, if someone selected another
// language with the language selector
if(selectedRequestLang != null) {
// Get the Locale object for the param
if((selectedLocale = scanLocale(selectedRequestLang)) != null) {
// Save the request parameter as session value
session.setAttribute(LANG_PARAM, selectedRequestLang);
}
} else {
// If there is a session stored language, use it
if(selectedSessionLang != null) {
selectedLocale = scanLocale(selectedSessionLang);
} }
} }
return selectedLocale; return selectedLocale;
} }
/**
* Create a Locale from a browser provides language string
*
* @param lang A string encoded locale, as provided by browsers
* @return A java.util.Locale representation of the language string
*/
private static java.util.Locale scanLocale(String lang) {
// Protect against empty lang string
if (lang != null) {
// Split the string and create the Locale object
StringTokenizer paramValues = new StringTokenizer(lang, "_");
if (paramValues.countTokens() > 1) {
return new java.util.Locale(paramValues.nextToken(), paramValues.nextToken());
} else {
return new java.util.Locale(paramValues.nextToken());
}
}
return null;
}
} }