From acde4973ab5eb9f8d7ec5ef2cba2d5f3dc6cdb1c Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Fri, 20 Mar 2015 23:48:49 +0200 Subject: [PATCH] Removed Locale enum to make code usable for every possible language. --- .../eu/svjatoslav/commons/network/Locale.java | 25 ------------ .../commons/network/LocaleConfiguration.java | 40 +++++++++---------- .../network/navigation/Navigation.java | 3 +- .../network/navigation/NavigationItem.java | 7 ++-- 4 files changed, 22 insertions(+), 53 deletions(-) delete mode 100755 src/main/java/eu/svjatoslav/commons/network/Locale.java diff --git a/src/main/java/eu/svjatoslav/commons/network/Locale.java b/src/main/java/eu/svjatoslav/commons/network/Locale.java deleted file mode 100755 index e05cb5a..0000000 --- a/src/main/java/eu/svjatoslav/commons/network/Locale.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Svjatoslav Commons - shared library of common functionality. - * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. - */ - -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/LocaleConfiguration.java b/src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java index 2230885..2bb15e2 100755 --- a/src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java +++ b/src/main/java/eu/svjatoslav/commons/network/LocaleConfiguration.java @@ -16,48 +16,44 @@ import javax.servlet.http.HttpServletRequest; public class LocaleConfiguration { - Locale defaultLocale; + String defaultLocale; - private final List allowedLocales = new ArrayList(); + private final List allowedLocales = new ArrayList(); - public LocaleConfiguration(final Locale defaultLocale, - final Locale... allowedLocales) { + public LocaleConfiguration(final String defaultLocale, + final String... allowedLocales) { this.defaultLocale = defaultLocale; - for (final Locale locale : allowedLocales) - this.getAllowedLocales().add(locale); + for (final String locale : allowedLocales) + getAllowedLocales().add(locale); } - public Locale detectCurrentLocale(final HttpServletRequest request) { + public String detectCurrentLocale(final HttpServletRequest request) { final String sessionLocaleString = (String) request.getSession() .getAttribute("locale"); - Locale result = localeFromString(sessionLocaleString); - if (result == null) - result = defaultLocale; + String result = defaultLocale; + if (isAllowedLocale(sessionLocaleString)) + result = sessionLocaleString; - final Locale requestLocale = localeFromString(request - .getParameter("locale")); - if (requestLocale != null) + final String requestLocale = request.getParameter("locale"); + + if (isAllowedLocale(requestLocale)) result = requestLocale; - request.getSession().setAttribute("locale", result.asString()); + request.getSession().setAttribute("locale", result); return result; } - private Locale localeFromString(final String localeString) { - for (final Locale locale : getAllowedLocales()) - if (locale.asString().equals(localeString)) - return locale; - - return null; + public List getAllowedLocales() { + return allowedLocales; } - public List getAllowedLocales() { - return allowedLocales; + public boolean isAllowedLocale(final String locale) { + return allowedLocales.contains(locale); } } diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java index 6ef5e68..4dffa36 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java @@ -15,7 +15,6 @@ import java.util.List; import javax.servlet.http.HttpServletRequest; -import eu.svjatoslav.commons.network.Locale; import eu.svjatoslav.commons.network.LocaleConfiguration; public class Navigation { @@ -70,7 +69,7 @@ public class Navigation { public String getTopMenu(final HttpServletRequest request) { - final Locale currentLocale = localeConfiguration + final String currentLocale = localeConfiguration .detectCurrentLocale(request); final NI selectedItem = getSelectedItem(request); diff --git a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java index 57a7649..0152c26 100755 --- a/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java +++ b/src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java @@ -14,14 +14,13 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import eu.svjatoslav.commons.network.Locale; import eu.svjatoslav.commons.string.WildCardMatcher; public class NavigationItem { private String matchingPattern; - private final Map localeToTitle = new HashMap(); + private final Map localeToTitle = new HashMap(); private final ArrayList subElements = new ArrayList(); private final NavigationItem parent; private final Navigation navigation; @@ -90,12 +89,12 @@ public class NavigationItem { return subElements; } - public String getTitle(final Locale locale) { + public String getTitle(final String locale) { return localeToTitle.get(locale); } private void initializeLocalizedTitles(final String... titles) { - final List locales = getNavigation().getLocaleConfiguration() + final List locales = getNavigation().getLocaleConfiguration() .getAllowedLocales(); if (locales.size() != titles.length) throw new RuntimeException("There should be exactly " -- 2.20.1