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 java.util.ArrayList;
13 import java.util.List;
15 import javax.servlet.http.HttpServletRequest;
17 public class LocaleConfiguration {
21 private final List<Locale> allowedLocales = new ArrayList<Locale>();
23 public LocaleConfiguration(final Locale defaultLocale,
24 final Locale... allowedLocales) {
26 this.defaultLocale = defaultLocale;
28 for (final Locale locale : allowedLocales)
29 this.getAllowedLocales().add(locale);
32 public Locale detectCurrentLocale(final HttpServletRequest request) {
34 final String sessionLocaleString = (String) request.getSession()
35 .getAttribute("locale");
37 Locale result = localeFromString(sessionLocaleString);
39 result = defaultLocale;
41 final Locale requestLocale = localeFromString(request
42 .getParameter("locale"));
43 if (requestLocale != null)
44 result = requestLocale;
46 request.getSession().setAttribute("locale", result.asString());
51 private Locale localeFromString(final String localeString) {
52 for (final Locale locale : getAllowedLocales())
53 if (locale.asString().equals(localeString))
59 public List<Locale> getAllowedLocales() {
60 return allowedLocales;