src/java/de/uapcore/lightpit/modules/LanguageModule.java

changeset 15
bb594abac796
parent 14
2b270c714678
child 24
8137ec335416
--- 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);
+    }
 }

mercurial