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; |
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 } |