Java tutorial
/*$Id: LocaleUtil.java 5120 2007-06-10 16:18:30Z jens $*/ /* **************************************************************************** * * * (c) Copyright 2005 ABM-utvikling * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.common.locale; import java.util.Locale; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import no.abmu.common.constants.LocaleTypeNameConst; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.web.servlet.i18n.SessionLocaleResolver; import org.springframework.web.servlet.support.RequestContextUtils; /** * Utilities for handling locale. Currently handling setting session * start locale. * * For info about locale in java se: * http://java.sun.com/j2se/1.4.2/docs/guide/intl/locale.doc.html * * @author Jens Vindvad, ABM-utvikling * @author $Author: jens $ * @version $Rev: 5120 $ * @date $Date: 2007-06-10 18:18:30 +0200 (Sun, 10 Jun 2007) $ * copyright ABM-Utvikling */ public class LocaleUtil { private static final Log logger = (Log) LogFactory.getLog(LocaleUtil.class); /** * Setting session start locale. * * @param request current HTTP request * @param response current HTTP response */ public void settingSessionStartLocale(HttpServletRequest request, HttpServletResponse response) { if (logger.isDebugEnabled()) { logger.debug("Before settingSessionStartLocale"); logLocale(request); } settingSessionStartLocale(request, response, null); if (logger.isDebugEnabled()) { logger.debug("After settingSessionStartLocale"); logLocale(request); } } /** * Setting session start locale. * * Currently support the following languages: bokml and nynorsk. * StartLocale are used when starting application to enschure starting with * bokml or nynorsk. siteLocale is used by inspectors to enable user to switch * locale. These to variables must have different names. * * @param request current HTTP request * @param response current HTTP response * @param startLocaleHttpParameterName Parameter name for start locale * http parameter, default is "startLocale". */ public void settingSessionStartLocale(HttpServletRequest request, HttpServletResponse response, String startLocaleHttpParameterName) { Locale newLocale = null; String parameterName; SessionLocaleResolver sessionLocaleResolver; if (logger.isDebugEnabled()) { logger.debug("Before settingSessionStartLocale"); logLocale(request); } if (null == startLocaleHttpParameterName) { parameterName = "startLocale"; } else { parameterName = startLocaleHttpParameterName; } String startLocaleValue = request.getParameter(parameterName); if (null != startLocaleValue) { if (startLocaleValue.equals("no_NO")) { newLocale = LocaleTypeNameConst.BOKMAAL; } else if (startLocaleValue.equals("no_NO_NY")) { newLocale = LocaleTypeNameConst.NYNORSK; } else if (startLocaleValue.equals("en_GB")) { newLocale = LocaleTypeNameConst.ENGLISH; } if (null != newLocale) { sessionLocaleResolver = new SessionLocaleResolver(); sessionLocaleResolver.setLocale(request, response, newLocale); } } if (logger.isDebugEnabled()) { logger.debug("After settingSessionStartLocale"); logLocale(request); } } /** * Return current Locale of HttpServletRequest request. * * @param request * @return locale - locale of request */ public static Locale getCurrentLocale(HttpServletRequest request) { return RequestContextUtils.getLocale(request); } /** * Method for logging of locale. Used under test and development. * * @param request current HTTP request */ public void logLocale(HttpServletRequest request) { HttpSession session = request.getSession(false); String parameterSiteLocale = request.getParameter("siteLocale"); String parameterStartLocale = request.getParameter("startLocale"); String rcSiteLocale = (String) request.getAttribute("siteLocale"); // String seSiteLocale = (String) session.getAttribute("siteLocale"); Locale locale = RequestContextUtils.getLocale(request); logger.info("request siteLocale: " + rcSiteLocale); logger.info("request parameter siteLocale: " + parameterSiteLocale); logger.info("request parameter startLocale: " + parameterStartLocale); // logger.info("session siteLocale: " + seSiteLocale); logger.info("Locale getDisplayCountry: " + locale.getDisplayCountry()); logger.info("Locale getDisplayLanguage: " + locale.getDisplayLanguage()); logger.info("Locale getDisplayVariant: " + locale.getDisplayVariant()); logger.info("Locale getDisplayName: " + locale.getDisplayName()); logger.info("Locale getISO3Country: " + locale.getISO3Country()); logger.info("Locale getLanguage: " + locale.getLanguage()); logger.info("Locale getVariant: " + locale.getVariant()); logger.info("Locale toString: " + locale.toString()); } }