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

changeset 13
f4608ad6c947
parent 12
005d27918b57
child 14
2b270c714678
equal deleted inserted replaced
12:005d27918b57 13:f4608ad6c947
28 */ 28 */
29 package de.uapcore.lightpit.modules; 29 package de.uapcore.lightpit.modules;
30 30
31 import de.uapcore.lightpit.LightPITModule; 31 import de.uapcore.lightpit.LightPITModule;
32 import de.uapcore.lightpit.AbstractLightPITServlet; 32 import de.uapcore.lightpit.AbstractLightPITServlet;
33 import de.uapcore.lightpit.Constants;
34 import de.uapcore.lightpit.Functions;
33 import de.uapcore.lightpit.HttpMethod; 35 import de.uapcore.lightpit.HttpMethod;
34 import javax.servlet.annotation.WebServlet; 36 import javax.servlet.annotation.WebServlet;
35 import javax.servlet.http.HttpServletRequest; 37 import javax.servlet.http.HttpServletRequest;
36 import javax.servlet.http.HttpServletResponse; 38 import javax.servlet.http.HttpServletResponse;
37 import de.uapcore.lightpit.RequestMapping; 39 import de.uapcore.lightpit.RequestMapping;
38 import de.uapcore.lightpit.ResponseType; 40 import de.uapcore.lightpit.ResponseType;
41 import java.util.ArrayList;
42 import java.util.IllformedLocaleException;
43 import java.util.List;
44 import java.util.Locale;
45 import java.util.Optional;
46 import javax.servlet.ServletException;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
39 49
40 50
41 @LightPITModule( 51 @LightPITModule(
42 bundleBaseName = "de.uapcore.lightpit.resources.localization.language", 52 bundleBaseName = "de.uapcore.lightpit.resources.localization.language",
43 modulePath = "language" 53 modulePath = "language"
46 name = "LanguageModule", 56 name = "LanguageModule",
47 urlPatterns = "/language/*" 57 urlPatterns = "/language/*"
48 ) 58 )
49 public final class LanguageModule extends AbstractLightPITServlet { 59 public final class LanguageModule extends AbstractLightPITServlet {
50 60
61 private static final Logger LOG = LoggerFactory.getLogger(LanguageModule.class);
62
63 private List<Locale> languages = new ArrayList<>();
64
65 @Override
66 public void init() throws ServletException {
67 super.init();
68
69 Optional<String[]> langs = Functions.availableLanguages(getServletContext());
70 if (langs.isPresent()) {
71 for (String lang : langs.get()) {
72 try {
73 Locale locale = Locale.forLanguageTag(lang);
74 if (locale.getLanguage().isEmpty()) {
75 throw new IllformedLocaleException();
76 }
77 languages.add(locale);
78 } catch (IllformedLocaleException ex) {
79 LOG.warn("Specified lanaguge {} in context parameter cannot be mapped to an existing locale - skipping.", lang);
80 }
81 }
82
83 } else {
84 languages.add(Locale.ENGLISH);
85 LOG.warn("Context parameter 'available-languges' not found. Only english will be available.");
86 }
87 }
88
51 @RequestMapping(method = HttpMethod.GET) 89 @RequestMapping(method = HttpMethod.GET)
52 public ResponseType handle(HttpServletRequest req, HttpServletResponse resp) { 90 public ResponseType handle(HttpServletRequest req, HttpServletResponse resp) {
91
92 req.setAttribute("languages", languages);
93 req.setAttribute("browserLanguage", req.getLocale());
53 94
95 setStylesheet(req, "language");
96 setDynamicFragment(req, "language");
54 return ResponseType.HTML_FULL; 97 return ResponseType.HTML_FULL;
55 } 98 }
56 } 99 }

mercurial