From 940cdf90fe6d2a5017ebc009e5affac29506a2f9 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 24 Dec 2013 21:44:42 +0200 Subject: [PATCH] locale helper --- pom.xml | 12 +++++-- .../eu/svjatoslav/commons/network/Locale.java | 16 +++++++++ .../commons/network/LocaleHelper.java | 36 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/main/java/eu/svjatoslav/commons/network/Locale.java create mode 100644 src/main/java/eu/svjatoslav/commons/network/LocaleHelper.java diff --git a/pom.xml b/pom.xml index 56bed4b..eba92b4 100755 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - + 4.0.0 eu.svjatoslav svjatoslavcommons @@ -72,6 +73,13 @@ 4.8.1 test + + + javax.servlet + servlet-api + 2.5 + provided + @@ -83,7 +91,7 @@ svjatoslav.eu svjatoslav.eu - scp://svjatoslav.eu/var/www/svjatoslav.eu/maven + scp://svjatoslav.eu/var/www/svjatoslav.eu/maven diff --git a/src/main/java/eu/svjatoslav/commons/network/Locale.java b/src/main/java/eu/svjatoslav/commons/network/Locale.java new file mode 100644 index 0000000..a5ef718 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/network/Locale.java @@ -0,0 +1,16 @@ +package eu.svjatoslav.commons.network; + +public enum Locale { + ENG("eng"), EST("est"), RUS("rus"); + + String asString; + + Locale(final String asString) { + this.asString = asString; + } + + public String asString() { + return asString; + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/network/LocaleHelper.java b/src/main/java/eu/svjatoslav/commons/network/LocaleHelper.java new file mode 100644 index 0000000..a073900 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/network/LocaleHelper.java @@ -0,0 +1,36 @@ +package eu.svjatoslav.commons.network; + +import javax.servlet.http.HttpServletRequest; + +public class LocaleHelper { + + public static Locale detectLocale(final HttpServletRequest request) { + + final String sessionLocaleString = (String) request.getSession().getAttribute("locale"); + + Locale sessionLocale = localeFromString(sessionLocaleString); + if (sessionLocale == null) { + sessionLocale = Locale.ENG; + } + + final Locale requestLocale = localeFromString(request.getParameter("locale")); + if (requestLocale != null) { + sessionLocale = requestLocale; + } + + request.getSession().setAttribute("locale", sessionLocale.asString()); + + return sessionLocale; + } + + public static Locale localeFromString(final String localeString) { + for (final Locale locale : Locale.values()) { + if (locale.asString().equals(localeString)) { + return locale; + } + } + + return null; + } + +} -- 2.20.1