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

changeset 15
bb594abac796
parent 14
2b270c714678
child 24
8137ec335416
equal deleted inserted replaced
14:2b270c714678 15:bb594abac796
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;
33 import de.uapcore.lightpit.Functions; 34 import de.uapcore.lightpit.Functions;
34 import de.uapcore.lightpit.HttpMethod; 35 import de.uapcore.lightpit.HttpMethod;
35 import javax.servlet.annotation.WebServlet; 36 import javax.servlet.annotation.WebServlet;
36 import javax.servlet.http.HttpServletRequest; 37 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse; 38 import javax.servlet.http.HttpServletResponse;
57 ) 58 )
58 public final class LanguageModule extends AbstractLightPITServlet { 59 public final class LanguageModule extends AbstractLightPITServlet {
59 60
60 private static final Logger LOG = LoggerFactory.getLogger(LanguageModule.class); 61 private static final Logger LOG = LoggerFactory.getLogger(LanguageModule.class);
61 62
62 private List<Locale> languages = new ArrayList<>(); 63 private final List<Locale> languages = new ArrayList<>();
63 64
64 @Override 65 @Override
65 public void init() throws ServletException { 66 public void init() throws ServletException {
66 super.init(); 67 super.init();
67 68
82 } else { 83 } else {
83 languages.add(Locale.ENGLISH); 84 languages.add(Locale.ENGLISH);
84 LOG.warn("Context parameter 'available-languges' not found. Only english will be available."); 85 LOG.warn("Context parameter 'available-languges' not found. Only english will be available.");
85 } 86 }
86 } 87 }
88
89 @Override
90 public void destroy() {
91 super.destroy();
92 languages.clear();
93 }
87 94
88 @RequestMapping(method = HttpMethod.GET) 95 @RequestMapping(method = HttpMethod.GET)
89 public ResponseType handle(HttpServletRequest req, HttpServletResponse resp) { 96 public ResponseType handle(HttpServletRequest req, HttpServletResponse resp) {
90 97
91 req.setAttribute("languages", languages); 98 req.setAttribute("languages", languages);
93 100
94 setStylesheet(req, "language"); 101 setStylesheet(req, "language");
95 setDynamicFragment(req, "language"); 102 setDynamicFragment(req, "language");
96 return ResponseType.HTML_FULL; 103 return ResponseType.HTML_FULL;
97 } 104 }
105
106 @RequestMapping(method = HttpMethod.POST)
107 public ResponseType switchLanguage(HttpServletRequest req, HttpServletResponse resp) {
108
109 Optional<Locale> chosenLanguage = Optional.ofNullable(req.getParameter("language"))
110 .map(Locale::forLanguageTag)
111 .filter((l) -> !l.getLanguage().isEmpty());
112
113 chosenLanguage.ifPresent((l) -> req.getSession().setAttribute(Constants.SESSION_ATTR_LANGUAGE, l));
114 chosenLanguage.ifPresent(resp::setLocale);
115
116 return handle(req, resp);
117 }
98 } 118 }

mercurial