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

changeset 13
f4608ad6c947
parent 12
005d27918b57
child 14
2b270c714678
--- a/src/java/de/uapcore/lightpit/modules/LanguageModule.java	Sat Dec 23 17:28:19 2017 +0100
+++ b/src/java/de/uapcore/lightpit/modules/LanguageModule.java	Tue Dec 26 17:36:47 2017 +0100
@@ -30,12 +30,22 @@
 
 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;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import de.uapcore.lightpit.RequestMapping;
 import de.uapcore.lightpit.ResponseType;
+import java.util.ArrayList;
+import java.util.IllformedLocaleException;
+import java.util.List;
+import java.util.Locale;
+import java.util.Optional;
+import javax.servlet.ServletException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 @LightPITModule(
@@ -48,9 +58,42 @@
 )
 public final class LanguageModule extends AbstractLightPITServlet {
     
+    private static final Logger LOG = LoggerFactory.getLogger(LanguageModule.class);
+    
+    private List<Locale> languages = new ArrayList<>();
+
+    @Override
+    public void init() throws ServletException {
+        super.init();
+        
+        Optional<String[]> langs = Functions.availableLanguages(getServletContext());
+        if (langs.isPresent()) {
+            for (String lang : langs.get()) {
+                try {
+                    Locale locale = Locale.forLanguageTag(lang);
+                    if (locale.getLanguage().isEmpty()) {
+                        throw new IllformedLocaleException();
+                    }
+                    languages.add(locale);
+                } catch (IllformedLocaleException ex) {
+                    LOG.warn("Specified lanaguge {} in context parameter cannot be mapped to an existing locale - skipping.", lang);
+                }
+            }
+            
+        } else {
+            languages.add(Locale.ENGLISH);
+            LOG.warn("Context parameter 'available-languges' not found. Only english will be available.");
+        }
+    }
+    
     @RequestMapping(method = HttpMethod.GET)
     public ResponseType handle(HttpServletRequest req, HttpServletResponse resp) {
+
+        req.setAttribute("languages", languages);
+        req.setAttribute("browserLanguage", req.getLocale());
         
+        setStylesheet(req, "language");
+        setDynamicFragment(req, "language");
         return ResponseType.HTML_FULL;
     }
 }

mercurial