Tue, 26 Dec 2017 19:45:31 +0100
language selector completed
--- a/src/java/de/uapcore/lightpit/AbstractLightPITServlet.java Tue Dec 26 19:18:32 2017 +0100 +++ b/src/java/de/uapcore/lightpit/AbstractLightPITServlet.java Tue Dec 26 19:45:31 2017 +0100 @@ -243,7 +243,9 @@ session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale); LOG.debug("Settng language for new session {}: {}", session.getId(), sessionLocale.getDisplayLanguage()); } else { - LOG.trace("Continuing session {} with language {}", session.getId(), session.getAttribute(Constants.SESSION_ATTR_LANGUAGE)); + Locale sessionLocale = (Locale) session.getAttribute(Constants.SESSION_ATTR_LANGUAGE); + resp.setLocale(sessionLocale); + LOG.trace("Continuing session {} with language {}", session.getId(), sessionLocale); } req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
--- a/src/java/de/uapcore/lightpit/modules/LanguageModule.java Tue Dec 26 19:18:32 2017 +0100 +++ b/src/java/de/uapcore/lightpit/modules/LanguageModule.java Tue Dec 26 19:45:31 2017 +0100 @@ -30,6 +30,7 @@ import de.uapcore.lightpit.LightPITModule; import de.uapcore.lightpit.AbstractLightPITServlet; +import de.uapcore.lightpit.Constants; import de.uapcore.lightpit.Functions; import de.uapcore.lightpit.HttpMethod; import javax.servlet.annotation.WebServlet; @@ -59,7 +60,7 @@ private static final Logger LOG = LoggerFactory.getLogger(LanguageModule.class); - private List<Locale> languages = new ArrayList<>(); + private final List<Locale> languages = new ArrayList<>(); @Override public void init() throws ServletException { @@ -84,6 +85,12 @@ LOG.warn("Context parameter 'available-languges' not found. Only english will be available."); } } + + @Override + public void destroy() { + super.destroy(); + languages.clear(); + } @RequestMapping(method = HttpMethod.GET) public ResponseType handle(HttpServletRequest req, HttpServletResponse resp) { @@ -95,4 +102,17 @@ setDynamicFragment(req, "language"); return ResponseType.HTML_FULL; } + + @RequestMapping(method = HttpMethod.POST) + public ResponseType switchLanguage(HttpServletRequest req, HttpServletResponse resp) { + + Optional<Locale> chosenLanguage = Optional.ofNullable(req.getParameter("language")) + .map(Locale::forLanguageTag) + .filter((l) -> !l.getLanguage().isEmpty()); + + chosenLanguage.ifPresent((l) -> req.getSession().setAttribute(Constants.SESSION_ATTR_LANGUAGE, l)); + chosenLanguage.ifPresent(resp::setLocale); + + return handle(req, resp); + } }
--- a/src/java/de/uapcore/lightpit/resources/localization/language.properties Tue Dec 26 19:18:32 2017 +0100 +++ b/src/java/de/uapcore/lightpit/resources/localization/language.properties Tue Dec 26 19:45:31 2017 +0100 @@ -24,3 +24,4 @@ menuLabel = Languages submit = Switch language browserLanguage = Browser language +browserLanguageNotAvailable = Browser language not available.
--- a/src/java/de/uapcore/lightpit/resources/localization/language_de.properties Tue Dec 26 19:18:32 2017 +0100 +++ b/src/java/de/uapcore/lightpit/resources/localization/language_de.properties Tue Dec 26 19:45:31 2017 +0100 @@ -24,3 +24,4 @@ menuLabel = Sprache submit = Sprache ausw\u00e4hlen browserLanguage = Browsersprache +browserLanguageNotAvailable = Browsersprache nicht verf\u00fcgbar.
--- a/web/WEB-INF/dynamic_fragments/language.jsp Tue Dec 26 19:18:32 2017 +0100 +++ b/web/WEB-INF/dynamic_fragments/language.jsp Tue Dec 26 19:45:31 2017 +0100 @@ -31,16 +31,17 @@ <c:set scope="page" var="currentLanguage" value="${sessionScope[Constants.SESSION_ATTR_LANGUAGE]}" /> -<fmt:bundle basename="${requestScope[Constants.REQ_ATTR_MODULE_INFO].bundleBaseName}"> <form method="POST"id="lang-selector"> <c:forEach items="${languages}" var="l"> <label> <input type="radio" name="language" value="${l.language}" <c:if test="${l.language eq currentLanguage.language}">checked</c:if>/> ${l.displayLanguage} - (${l.getDisplayLanguage(currentLanguage)}<c:if test="${not empty browserLanguage and l.language eq browserLanguage.language}"> - <fmt:message key="browserLanguage"/></c:if>) + (${l.getDisplayLanguage(currentLanguage)}<c:if test="${not empty browserLanguage and l.language eq browserLanguage.language}"><c:set var="browserLanguagePresent" value="true"/> - <fmt:message key="browserLanguage"/></c:if>) </label> </c:forEach> + <c:if test="${not browserLanguagePresent}"> + <span class="blNA"><fmt:message key="browserLanguageNotAvailable" /></span> + </c:if> <input type="submit" value="<fmt:message key="submit" />"/> </form> -</fmt:bundle>
--- a/web/WEB-INF/jsp/full.jsp Tue Dec 26 19:18:32 2017 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -<%-- -DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - -Copyright 2017 Mike Becker. All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright -notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright -notice, this list of conditions and the following disclaimer in the -documentation and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ---%> -<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %> -<%@page import="de.uapcore.lightpit.Constants" %> -<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> -<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> -<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> - -<%-- Define an alias for the main menu --%> -<c:set scope="page" var="mainMenu" value="${requestScope[Constants.REQ_ATTR_MENU]}"/> - -<%-- Define an alias for the module info --%> -<c:set scope="page" var="moduleInfo" value="${requestScope[Constants.REQ_ATTR_MODULE_INFO]}"/> - -<!DOCTYPE html> -<html> - <head> - <base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/"> - <title>LightPIT - - <fmt:bundle basename="${moduleInfo.bundleBaseName}"> - <fmt:message key="${moduleInfo.titleKey}" /> - </fmt:bundle> - </title> - <meta charset="UTF-8"> - <link rel="stylesheet" href="lightpit.css" type="text/css"> - </head> - <body> - <div id="mainMenu"> - <c:forEach var="menu" items="${mainMenu}"> - <div class="menuEntry" - <c:if test="${requestScope[Constants.REQ_ATTR_MODULE_CLASSNAME] eq menu.moduleClassName}"> - data-active - </c:if> - > - <a href="${menu.pathName}"> - <fmt:bundle basename="${menu.resourceKey.bundle}"> - <fmt:message key="${menu.resourceKey.key}" /> - </fmt:bundle> - </a> - </div> - </c:forEach> - - </div> - <div id="subMenu"> - - </div> - <div id="content-area"> - <%-- Resource keys should be rooted in the specific bundle for this module. --%> - <fmt:bundle basename="${moduleInfo.bundleBaseName}"> - TODO: load fragment - </fmt:bundle> - </div> - </body> -</html>
--- a/web/WEB-INF/jsp/html_full.jsp Tue Dec 26 19:18:32 2017 +0100 +++ b/web/WEB-INF/jsp/html_full.jsp Tue Dec 26 19:45:31 2017 +0100 @@ -42,6 +42,11 @@ <%-- Define an alias for the module info --%> <c:set scope="page" var="moduleInfo" value="${requestScope[Constants.REQ_ATTR_MODULE_INFO]}"/> +<%-- Apply the session locale (should always be present, but check nevertheless) --%> +<c:if test="${not empty sessionScope[Constants.SESSION_ATTR_LANGUAGE]}"> +<fmt:setLocale scope="request" value="${sessionScope[Constants.SESSION_ATTR_LANGUAGE]}"/> +</c:if> + <!DOCTYPE html> <html> <head> @@ -78,7 +83,8 @@ </div> <div id="content-area"> <c:if test="${not empty fragment}"> - <c:import url="${fragment}" /> + <fmt:setBundle scope="request" basename="${moduleInfo.bundleBaseName}"/> + <c:import url="${fragment}" /> </c:if> </div> </body>