language selector completed

Tue, 26 Dec 2017 19:45:31 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 26 Dec 2017 19:45:31 +0100
changeset 15
bb594abac796
parent 14
2b270c714678
child 16
4e0998805276

language selector completed

src/java/de/uapcore/lightpit/AbstractLightPITServlet.java file | annotate | diff | comparison | revisions
src/java/de/uapcore/lightpit/modules/LanguageModule.java file | annotate | diff | comparison | revisions
src/java/de/uapcore/lightpit/resources/localization/language.properties file | annotate | diff | comparison | revisions
src/java/de/uapcore/lightpit/resources/localization/language_de.properties file | annotate | diff | comparison | revisions
web/WEB-INF/dynamic_fragments/language.jsp file | annotate | diff | comparison | revisions
web/WEB-INF/jsp/full.jsp file | annotate | diff | comparison | revisions
web/WEB-INF/jsp/html_full.jsp file | annotate | diff | comparison | revisions
web/language.css file | annotate | diff | comparison | revisions
     1.1 --- a/src/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Tue Dec 26 19:18:32 2017 +0100
     1.2 +++ b/src/java/de/uapcore/lightpit/AbstractLightPITServlet.java	Tue Dec 26 19:45:31 2017 +0100
     1.3 @@ -243,7 +243,9 @@
     1.4              session.setAttribute(Constants.SESSION_ATTR_LANGUAGE, sessionLocale);
     1.5              LOG.debug("Settng language for new session {}: {}", session.getId(), sessionLocale.getDisplayLanguage());
     1.6          } else {
     1.7 -            LOG.trace("Continuing session {} with language {}", session.getId(), session.getAttribute(Constants.SESSION_ATTR_LANGUAGE));
     1.8 +            Locale sessionLocale = (Locale) session.getAttribute(Constants.SESSION_ATTR_LANGUAGE);
     1.9 +            resp.setLocale(sessionLocale);
    1.10 +            LOG.trace("Continuing session {} with language {}", session.getId(), sessionLocale);
    1.11          }
    1.12          
    1.13          req.setAttribute(Constants.REQ_ATTR_PATH, Functions.fullPath(req));
     2.1 --- a/src/java/de/uapcore/lightpit/modules/LanguageModule.java	Tue Dec 26 19:18:32 2017 +0100
     2.2 +++ b/src/java/de/uapcore/lightpit/modules/LanguageModule.java	Tue Dec 26 19:45:31 2017 +0100
     2.3 @@ -30,6 +30,7 @@
     2.4  
     2.5  import de.uapcore.lightpit.LightPITModule;
     2.6  import de.uapcore.lightpit.AbstractLightPITServlet;
     2.7 +import de.uapcore.lightpit.Constants;
     2.8  import de.uapcore.lightpit.Functions;
     2.9  import de.uapcore.lightpit.HttpMethod;
    2.10  import javax.servlet.annotation.WebServlet;
    2.11 @@ -59,7 +60,7 @@
    2.12      
    2.13      private static final Logger LOG = LoggerFactory.getLogger(LanguageModule.class);
    2.14      
    2.15 -    private List<Locale> languages = new ArrayList<>();
    2.16 +    private final List<Locale> languages = new ArrayList<>();
    2.17  
    2.18      @Override
    2.19      public void init() throws ServletException {
    2.20 @@ -84,6 +85,12 @@
    2.21              LOG.warn("Context parameter 'available-languges' not found. Only english will be available.");
    2.22          }
    2.23      }
    2.24 +
    2.25 +    @Override
    2.26 +    public void destroy() {
    2.27 +        super.destroy();
    2.28 +        languages.clear();
    2.29 +    }
    2.30      
    2.31      @RequestMapping(method = HttpMethod.GET)
    2.32      public ResponseType handle(HttpServletRequest req, HttpServletResponse resp) {
    2.33 @@ -95,4 +102,17 @@
    2.34          setDynamicFragment(req, "language");
    2.35          return ResponseType.HTML_FULL;
    2.36      }
    2.37 +    
    2.38 +    @RequestMapping(method = HttpMethod.POST)
    2.39 +    public ResponseType switchLanguage(HttpServletRequest req, HttpServletResponse resp) {
    2.40 +        
    2.41 +        Optional<Locale> chosenLanguage = Optional.ofNullable(req.getParameter("language"))
    2.42 +                .map(Locale::forLanguageTag)
    2.43 +                .filter((l) -> !l.getLanguage().isEmpty());
    2.44 +        
    2.45 +        chosenLanguage.ifPresent((l) -> req.getSession().setAttribute(Constants.SESSION_ATTR_LANGUAGE, l));
    2.46 +        chosenLanguage.ifPresent(resp::setLocale);
    2.47 +        
    2.48 +        return handle(req, resp);
    2.49 +    }
    2.50  }
     3.1 --- a/src/java/de/uapcore/lightpit/resources/localization/language.properties	Tue Dec 26 19:18:32 2017 +0100
     3.2 +++ b/src/java/de/uapcore/lightpit/resources/localization/language.properties	Tue Dec 26 19:45:31 2017 +0100
     3.3 @@ -24,3 +24,4 @@
     3.4  menuLabel = Languages
     3.5  submit = Switch language
     3.6  browserLanguage = Browser language
     3.7 +browserLanguageNotAvailable = Browser language not available.
     4.1 --- a/src/java/de/uapcore/lightpit/resources/localization/language_de.properties	Tue Dec 26 19:18:32 2017 +0100
     4.2 +++ b/src/java/de/uapcore/lightpit/resources/localization/language_de.properties	Tue Dec 26 19:45:31 2017 +0100
     4.3 @@ -24,3 +24,4 @@
     4.4  menuLabel = Sprache
     4.5  submit = Sprache ausw\u00e4hlen
     4.6  browserLanguage = Browsersprache
     4.7 +browserLanguageNotAvailable = Browsersprache nicht verf\u00fcgbar.
     5.1 --- a/web/WEB-INF/dynamic_fragments/language.jsp	Tue Dec 26 19:18:32 2017 +0100
     5.2 +++ b/web/WEB-INF/dynamic_fragments/language.jsp	Tue Dec 26 19:45:31 2017 +0100
     5.3 @@ -31,16 +31,17 @@
     5.4  
     5.5  <c:set scope="page" var="currentLanguage" value="${sessionScope[Constants.SESSION_ATTR_LANGUAGE]}" />
     5.6  
     5.7 -<fmt:bundle basename="${requestScope[Constants.REQ_ATTR_MODULE_INFO].bundleBaseName}">
     5.8  <form method="POST"id="lang-selector">
     5.9      <c:forEach items="${languages}" var="l">
    5.10          <label>
    5.11              <input type="radio" name="language" value="${l.language}"
    5.12                     <c:if test="${l.language eq currentLanguage.language}">checked</c:if>/>
    5.13              ${l.displayLanguage}
    5.14 -            (${l.getDisplayLanguage(currentLanguage)}<c:if test="${not empty browserLanguage and l.language eq browserLanguage.language}">&nbsp;-&nbsp;<fmt:message key="browserLanguage"/></c:if>)
    5.15 +            (${l.getDisplayLanguage(currentLanguage)}<c:if test="${not empty browserLanguage and l.language eq browserLanguage.language}"><c:set var="browserLanguagePresent" value="true"/>&nbsp;-&nbsp;<fmt:message key="browserLanguage"/></c:if>)
    5.16          </label>
    5.17      </c:forEach>
    5.18 +    <c:if test="${not browserLanguagePresent}">
    5.19 +        <span class="blNA"><fmt:message key="browserLanguageNotAvailable" /></span>
    5.20 +    </c:if>
    5.21      <input type="submit" value="<fmt:message key="submit" />"/>
    5.22  </form>
    5.23 -</fmt:bundle>
     6.1 --- a/web/WEB-INF/jsp/full.jsp	Tue Dec 26 19:18:32 2017 +0100
     6.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.3 @@ -1,78 +0,0 @@
     6.4 -<%-- 
     6.5 -DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     6.6 -
     6.7 -Copyright 2017 Mike Becker. All rights reserved.
     6.8 -
     6.9 -Redistribution and use in source and binary forms, with or without
    6.10 -modification, are permitted provided that the following conditions are met:
    6.11 -
    6.12 -1. Redistributions of source code must retain the above copyright
    6.13 -notice, this list of conditions and the following disclaimer.
    6.14 -
    6.15 -2. Redistributions in binary form must reproduce the above copyright
    6.16 -notice, this list of conditions and the following disclaimer in the
    6.17 -documentation and/or other materials provided with the distribution.
    6.18 -
    6.19 -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    6.20 -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    6.21 -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    6.22 -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    6.23 -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    6.24 -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    6.25 -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    6.26 -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    6.27 -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    6.28 -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
    6.29 ---%>
    6.30 -<%@page contentType="text/html" pageEncoding="UTF-8" trimDirectiveWhitespaces="true" %>
    6.31 -<%@page import="de.uapcore.lightpit.Constants" %>
    6.32 -<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    6.33 -<%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
    6.34 -<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
    6.35 -
    6.36 -<%-- Define an alias for the main menu --%>
    6.37 -<c:set scope="page" var="mainMenu" value="${requestScope[Constants.REQ_ATTR_MENU]}"/>
    6.38 -
    6.39 -<%-- Define an alias for the module info --%>
    6.40 -<c:set scope="page" var="moduleInfo" value="${requestScope[Constants.REQ_ATTR_MODULE_INFO]}"/>
    6.41 -
    6.42 -<!DOCTYPE html>
    6.43 -<html>
    6.44 -    <head>
    6.45 -        <base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
    6.46 -        <title>LightPIT -
    6.47 -            <fmt:bundle basename="${moduleInfo.bundleBaseName}">
    6.48 -                <fmt:message key="${moduleInfo.titleKey}" />
    6.49 -            </fmt:bundle>
    6.50 -        </title>
    6.51 -        <meta charset="UTF-8">
    6.52 -        <link rel="stylesheet" href="lightpit.css" type="text/css">
    6.53 -    </head>
    6.54 -    <body>
    6.55 -        <div id="mainMenu">
    6.56 -            <c:forEach var="menu" items="${mainMenu}">
    6.57 -                <div class="menuEntry"
    6.58 -                     <c:if test="${requestScope[Constants.REQ_ATTR_MODULE_CLASSNAME] eq menu.moduleClassName}">
    6.59 -                         data-active
    6.60 -                     </c:if>
    6.61 -                >
    6.62 -                    <a href="${menu.pathName}">
    6.63 -                    <fmt:bundle basename="${menu.resourceKey.bundle}">
    6.64 -                        <fmt:message key="${menu.resourceKey.key}" />
    6.65 -                    </fmt:bundle>
    6.66 -                    </a>
    6.67 -                </div>
    6.68 -            </c:forEach>
    6.69 -            
    6.70 -        </div>
    6.71 -        <div id="subMenu">
    6.72 -            
    6.73 -        </div>
    6.74 -        <div id="content-area">
    6.75 -            <%-- Resource keys should be rooted in the specific bundle for this module. --%>
    6.76 -            <fmt:bundle basename="${moduleInfo.bundleBaseName}">
    6.77 -                TODO: load fragment
    6.78 -            </fmt:bundle>
    6.79 -        </div>
    6.80 -    </body>
    6.81 -</html>
     7.1 --- a/web/WEB-INF/jsp/html_full.jsp	Tue Dec 26 19:18:32 2017 +0100
     7.2 +++ b/web/WEB-INF/jsp/html_full.jsp	Tue Dec 26 19:45:31 2017 +0100
     7.3 @@ -42,6 +42,11 @@
     7.4  <%-- Define an alias for the module info --%>
     7.5  <c:set scope="page" var="moduleInfo" value="${requestScope[Constants.REQ_ATTR_MODULE_INFO]}"/>
     7.6  
     7.7 +<%-- Apply the session locale (should always be present, but check nevertheless) --%>
     7.8 +<c:if test="${not empty sessionScope[Constants.SESSION_ATTR_LANGUAGE]}">
     7.9 +<fmt:setLocale scope="request" value="${sessionScope[Constants.SESSION_ATTR_LANGUAGE]}"/>
    7.10 +</c:if>
    7.11 +
    7.12  <!DOCTYPE html>
    7.13  <html>
    7.14      <head>
    7.15 @@ -78,7 +83,8 @@
    7.16          </div>
    7.17          <div id="content-area">
    7.18              <c:if test="${not empty fragment}">
    7.19 -            <c:import url="${fragment}" />
    7.20 +                <fmt:setBundle scope="request" basename="${moduleInfo.bundleBaseName}"/>
    7.21 +                <c:import url="${fragment}" />
    7.22              </c:if>
    7.23          </div>
    7.24      </body>
     8.1 --- a/web/language.css	Tue Dec 26 19:18:32 2017 +0100
     8.2 +++ b/web/language.css	Tue Dec 26 19:45:31 2017 +0100
     8.3 @@ -37,3 +37,10 @@
     8.4  input {
     8.5      margin: .5em;
     8.6  }
     8.7 +
     8.8 +span.blNA {
     8.9 +    margin: .5em;
    8.10 +    color: red;
    8.11 +    font-style: italic;
    8.12 +    font-size: smaller;
    8.13 +}

mercurial