diff options
author | tknall <tknall@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c> | 2008-11-25 12:48:15 +0000 |
---|---|---|
committer | tknall <tknall@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c> | 2008-11-25 12:48:15 +0000 |
commit | ea60aae139c666c610c1af5cfd0b616d3f4a535c (patch) | |
tree | e9e1556e38d1a68a90185ed7d3074f0e2dccec04 /src/main/java/at/gv/egiz/pdfas/web/i18n | |
parent | 1774241bd1e38eb789609ba443d48f1122fe4819 (diff) | |
download | pdf-as-3-ea60aae139c666c610c1af5cfd0b616d3f4a535c.tar.gz pdf-as-3-ea60aae139c666c610c1af5cfd0b616d3f4a535c.tar.bz2 pdf-as-3-ea60aae139c666c610c1af5cfd0b616d3f4a535c.zip |
Preparation for multilingual support for web application.
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@312 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/web/i18n')
-rw-r--r-- | src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java | 111 | ||||
-rw-r--r-- | src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java | 59 |
2 files changed, 170 insertions, 0 deletions
diff --git a/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java b/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java new file mode 100644 index 0000000..1db7e3b --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/web/i18n/LanguageDecoratorMapper.java @@ -0,0 +1,111 @@ +package at.gv.egiz.pdfas.web.i18n;
+
+import java.io.File;
+import java.util.Locale;
+import java.util.Properties;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.apache.log4j.Logger;
+
+import com.opensymphony.module.sitemesh.Config;
+import com.opensymphony.module.sitemesh.Decorator;
+import com.opensymphony.module.sitemesh.DecoratorMapper;
+import com.opensymphony.module.sitemesh.Page;
+import com.opensymphony.module.sitemesh.mapper.AbstractDecoratorMapper;
+import com.opensymphony.module.sitemesh.mapper.DefaultDecorator;
+
+public class LanguageDecoratorMapper extends AbstractDecoratorMapper {
+
+ public static final String LOCALE_SESSION_KEY = "at.gv.egiz.pdfas.web.i18n:currentLocale";
+
+ private Logger log = Logger.getLogger(LanguageDecoratorMapper.class);
+
+ public void init(Config config, Properties properties, DecoratorMapper parent) throws InstantiationException {
+ super.init(config, properties, parent);
+ }
+
+ public static Object getFirstNotNull(Object[] objects) {
+ if (objects == null) {
+ return null;
+ }
+ for (int i = 0; i < objects.length; i++) {
+ if (objects[i] != null) {
+ return objects[i];
+ }
+ }
+ return null;
+ }
+
+ public Decorator getDecorator(HttpServletRequest request, Page page) {
+ try {
+ Decorator result = null;
+ final Decorator d = super.getDecorator(request, page);
+ HttpSession session = request.getSession();
+ log.trace("Looking for Locale in session.");
+ Locale locale = (Locale) session.getAttribute(LOCALE_SESSION_KEY);
+ if (locale == null) {
+ log.debug("Unable to find Locale in session. Creating new Locale based on Accept-Language header entry.");
+ String acceptLanguage = request.getHeader("Accept-Language");
+ if (acceptLanguage != null) {
+ log.trace("Accept-Language header entry: " + acceptLanguage);
+ } else {
+ log.trace("No header entry found.");
+ }
+ if (acceptLanguage != null && acceptLanguage.length() >= 2) {
+ locale = new Locale(acceptLanguage.substring(0, 2));
+ log.debug("New Locale created: " + locale);
+ }
+ } else {
+ log.debug("Locale found: " + locale);
+ }
+ String path;
+ if (locale != null) {
+ path = modifyPath(d.getPage(), locale.getLanguage());
+ } else {
+ path = d.getPage();
+ }
+
+ File decFile = new File(config.getServletContext().getRealPath(path));
+
+ log.debug("Looking for decorator \"" + path + "\".");
+ if (decFile.isFile()) {
+ result = new DefaultDecorator(d.getName(), path, null) {
+ public String getInitParameter(String paramName) {
+ return d.getInitParameter(paramName);
+ }
+ };
+ }
+
+ if (result != null) {
+ log.debug("Decorator found (\"" + result.getName() + "\", \"" + getFirstNotNull(new Object[] { result.getURIPath(), result.getPage(), "<n/a>"}) + "\").");
+ return result;
+ } else {
+ log.trace("No decorator found. Delegating to super class.");
+ result = super.getDecorator(request, page);
+ if (result != null) {
+ log.debug("Super class returned decorator (\"" + result.getName() + "\", \"" + getFirstNotNull(new Object[] { result.getURIPath(), result.getPage(), "<n/a>"}) + "\").");
+ } else {
+ log.trace("Super class did not return a decorator.");
+ }
+ return result;
+ }
+ } catch (NullPointerException e) {
+ return super.getDecorator(request, page);
+ }
+ }
+
+ private static String modifyPath(String path, String ext) {
+ if (ext == null || ext.length() == 0) {
+ return path;
+ }
+ int dot = path.lastIndexOf('.');
+ if (dot > -1) {
+ return path.substring(0, dot) + '-' + ext + path.substring(dot);
+ } else {
+ return path + '-' + ext;
+ }
+ }
+
+}
diff --git a/src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java b/src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java new file mode 100644 index 0000000..582f765 --- /dev/null +++ b/src/main/java/at/gv/egiz/pdfas/web/i18n/SetLanguageServlet.java @@ -0,0 +1,59 @@ +package at.gv.egiz.pdfas.web.i18n;
+
+import java.io.IOException;
+import java.util.Locale;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
+import org.apache.commons.lang.ArrayUtils;
+import org.apache.commons.lang.StringUtils;
+import org.apache.log4j.Logger;
+
+public class SetLanguageServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ public static final String LANGUAGE_PARAMETER = "code";
+ private Logger log = Logger.getLogger(LanguageDecoratorMapper.class);
+
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ this.doPost(request, response);
+ }
+
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
+ HttpSession session = request.getSession();
+
+ // check manual language override (should be two-letter ISO-639 code)
+ Locale currentManualSetLanguage = (Locale) session.getAttribute(LanguageDecoratorMapper.LOCALE_SESSION_KEY);
+ String language = request.getParameter(LANGUAGE_PARAMETER);
+ if (!StringUtils.isEmpty(language)) {
+ String code = language.trim().toLowerCase();
+ if (!ArrayUtils.contains(Locale.getISOLanguages(), code)) {
+ log.error("Manually set language code \"" + code + "\" is not a valid ISO 639 language. Do not switch language.");
+ } else {
+ currentManualSetLanguage = new Locale(code);
+ log.info("Switching language to " + currentManualSetLanguage.getDisplayLanguage() + ".");
+ session.setAttribute(LanguageDecoratorMapper.LOCALE_SESSION_KEY, currentManualSetLanguage);
+ }
+ }
+
+// if (currentManualSetLanguage != null) {
+// log.info("Forcing language " + currentManualSetLanguage.getDisplayLanguage() + ".");
+// super.getServletContext().setAttribute(LanguageDecoratorMapper.LOCALE_SESSION_KEY, currentManualSetLanguage);
+// }
+
+// response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/"));
+ RequestDispatcher rd = request.getRequestDispatcher("/");
+ rd.forward(request, response);
+
+ }
+
+
+
+}
|