locale helper
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 24 Dec 2013 19:44:42 +0000 (21:44 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Tue, 24 Dec 2013 19:44:42 +0000 (21:44 +0200)
pom.xml
src/main/java/eu/svjatoslav/commons/network/Locale.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/commons/network/LocaleHelper.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index 56bed4b..eba92b4 100755 (executable)
--- a/pom.xml
+++ b/pom.xml
@@ -1,4 +1,5 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>eu.svjatoslav</groupId>
        <artifactId>svjatoslavcommons</artifactId>
                        <version>4.8.1</version>
                        <scope>test</scope>
                </dependency>
+
+               <dependency>
+                       <groupId>javax.servlet</groupId>
+                       <artifactId>servlet-api</artifactId>
+                       <version>2.5</version>
+                       <scope>provided</scope>
+               </dependency>
        </dependencies>
 
        <distributionManagement>
@@ -83,7 +91,7 @@
                <repository>
                        <id>svjatoslav.eu</id>
                        <name>svjatoslav.eu</name>
-                       <url>scp://svjatoslav.eu/var/www/svjatoslav.eu/maven</url>              
+                       <url>scp://svjatoslav.eu/var/www/svjatoslav.eu/maven</url>
                </repository>
        </distributionManagement>
 
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 (file)
index 0000000..a5ef718
--- /dev/null
@@ -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 (file)
index 0000000..a073900
--- /dev/null
@@ -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;
+    }
+
+}