2 * Svjatoslav Commons - shared library of common functionality.
3 * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 3 of the GNU Lesser General Public License
7 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.commons.network;
12 import javax.servlet.http.HttpServletRequest;
14 public class LocaleHelper {
16 public static Locale detectLocale(final HttpServletRequest request) {
18 final String sessionLocaleString = (String) request.getSession()
19 .getAttribute("locale");
21 Locale sessionLocale = localeFromString(sessionLocaleString);
22 if (sessionLocale == null)
23 sessionLocale = Locale.ENG;
25 final Locale requestLocale = localeFromString(request
26 .getParameter("locale"));
27 if (requestLocale != null)
28 sessionLocale = requestLocale;
30 request.getSession().setAttribute("locale", sessionLocale.asString());
35 public static Locale localeFromString(final String localeString) {
36 for (final Locale locale : Locale.values())
37 if (locale.asString().equals(localeString))