# HG changeset patch # User Ingo Weinzierl # Date 1272009305 0 # Node ID 2659a5b1fa1e8dace6cfb246c708b411c5ba1d5b # Parent f550bd27a3f11e42ebf6a36901199909337fa04e Added a link (and a controller) to toggle between german and english language (issue254). gnv/trunk@970 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r f550bd27a3f1 -r 2659a5b1fa1e gnv/ChangeLog --- a/gnv/ChangeLog Thu Apr 22 12:58:44 2010 +0000 +++ b/gnv/ChangeLog Fri Apr 23 07:55:05 2010 +0000 @@ -1,3 +1,35 @@ +2010-04-23 Ingo Weinzierl + + Issue254 + + * src/main/java/de/intevation/gnv/action/SwitchLanguageAction.java: New + controller to toggle between german and english language. + + * src/main/java/de/intevation/gnv/action/NextArtifactStepAction.java, + src/main/java/de/intevation/gnv/action/DescribeUIAction.java: Use the + locale stored in the artifact to create the user interface. At the + beginning, this will be the locale of the request object. If the user + chooses another language by calling the SwitchLanguageAction controller, + this locale becomes the prefered locale, and the browser settings are no + more relevant. + + * src/main/java/de/intevation/gnv/action/sessionmodel/SessionModel.java, + src/main/java/de/intevation/gnv/action/sessionmodel/DefaultSessionModel.java: + Now, the SessionModel has two methods to set and retrieve the current + locale which is used to create a language specific user interface. + + * src/main/java/de/intevation/gnv/action/sessionmodel/SessionModelFactory.java: + Initial creation of the SessionModel gets the locale of the request + object. If this locale is changed by the user via SwitchLanguageAction + controller, changes in the browser settings won't have an effect anymore. + + * src/main/webapp/WEB-INF/config/struts-config.xml: Add the controller to + switch between languages. + + * src/main/webapp/WEB-INF/jsp/header.jsp: The header contains a link to + toggle between german and english language - a click on it will trigger + the SwitchLanguageAction. + 2010-04-22 Ingo Weinzierl Issue221 diff -r f550bd27a3f1 -r 2659a5b1fa1e gnv/src/main/java/de/intevation/gnv/action/DescribeUIAction.java --- a/gnv/src/main/java/de/intevation/gnv/action/DescribeUIAction.java Thu Apr 22 12:58:44 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/DescribeUIAction.java Fri Apr 23 07:55:05 2010 +0000 @@ -15,6 +15,7 @@ import de.intevation.gnv.util.XMLUtils; import de.intevation.gnv.util.XSLTransformer; +import java.util.Locale; import java.util.ResourceBundle; import javax.servlet.http.HttpServletRequest; @@ -73,10 +74,12 @@ // render describe document and create user interface SessionModel sm = SessionModelFactory.getInstance().getSessionModel( request); + Locale tmp = sm.getCurrentLocale(); + Locale locale = tmp != null ? tmp : request.getLocale(); ArtifactDatabaseClient adc = ArtifactDatabaseClientFactory - .getInstance().getArtifactDatabaseClient(request.getLocale()); - request.getSession().setAttribute(Globals.LOCALE_KEY, request.getLocale()); + .getInstance().getArtifactDatabaseClient(locale); + request.getSession().setAttribute(Globals.LOCALE_KEY, locale); ArtifactDescription artifactDescription = adc.getCurrentStepDescription( @@ -87,7 +90,7 @@ Node currentUI = artifactDescription.getCurrentUI(); if (currentUI != null) { ResourceBundle res = ResourceBundle.getBundle( - "applicationMessages", adc.getLocale()); + "applicationMessages", locale); String editText = res.getString("gnviewer.history.back.button"); XSLTransformer transformer = new XSLTransformer(); diff -r f550bd27a3f1 -r 2659a5b1fa1e gnv/src/main/java/de/intevation/gnv/action/NextArtifactStepAction.java --- a/gnv/src/main/java/de/intevation/gnv/action/NextArtifactStepAction.java Thu Apr 22 12:58:44 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/NextArtifactStepAction.java Fri Apr 23 07:55:05 2010 +0000 @@ -15,6 +15,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; +import java.util.Locale; import java.util.Map; import javax.servlet.http.HttpServletRequest; @@ -63,6 +64,10 @@ SessionModel sm = SessionModelFactory.getInstance() .getSessionModel(request); ArtifactDescription ad = sm.getArtifactDescription(); + + Locale tmp = sm.getCurrentLocale(); + Locale locale = tmp != null ? tmp : request.getLocale(); + if (ad != null){ Collection inputParameter = ad.getInputParameter(); Collection ips = null; @@ -77,8 +82,8 @@ } } ArtifactDatabaseClient adc = ArtifactDatabaseClientFactory - .getInstance() - .getArtifactDatabaseClient(getLocale(request)); + .getInstance().getArtifactDatabaseClient(locale); + Map outs = ad.getOutputModes(); if (outs == null || outs.isEmpty()) { // TODO: Woher kommt der zu erreichende Status; @@ -103,8 +108,6 @@ ); } catch (ArtifactDatabaseInputException e) { - log.debug("================ CATCH ME ======================"); - log.debug("===== ERROR MSG: " + e.getMessage()); log.error(e, e); request.setAttribute( CommunicationKeys.REQUEST_EXCEPTION_INPUT_ID, diff -r f550bd27a3f1 -r 2659a5b1fa1e gnv/src/main/java/de/intevation/gnv/action/SwitchLanguageAction.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/SwitchLanguageAction.java Fri Apr 23 07:55:05 2010 +0000 @@ -0,0 +1,62 @@ +package de.intevation.gnv.action; + +import de.intevation.gnv.action.sessionmodel.SessionModel; +import de.intevation.gnv.action.sessionmodel.SessionModelFactory; + +import java.util.Locale; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; + +import org.apache.struts.action.ActionForm; +import org.apache.struts.action.ActionForward; +import org.apache.struts.action.ActionMapping; + +/** + * Toggle the current language used to create the user interface. Two + * languages are available now - german and english. + * + * @author Ingo Weinzierl + */ +public class SwitchLanguageAction extends DescribeUIAction { + + public static Logger logger = Logger.getLogger(SwitchLanguageAction.class); + + @Override + public ActionForward execute( + ActionMapping mapping, + ActionForm form, + HttpServletRequest request, + HttpServletResponse response + ) throws Exception + { + SessionModelFactory factory = SessionModelFactory.getInstance(); + SessionModel sm = factory.getSessionModel(request); + + sm.setCurrentLocale(switchLanguage(sm.getCurrentLocale())); + + return super.execute(mapping, form, request, response); + } + + + /** + * This method toggles between german and english language. + * + * @param currentLocale The locale which is currently used. + * @return an english locale, if the current locale is german - otherwise + * a german locale. + */ + protected Locale switchLanguage(Locale currentLocale) { + if (currentLocale.getLanguage().equals(Locale.GERMAN.getLanguage())) { + logger.info("Switch from german to english locale."); + return Locale.ENGLISH; + } + else { + logger.info("Switch from english to german locale."); + return Locale.GERMAN; + } + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : diff -r f550bd27a3f1 -r 2659a5b1fa1e gnv/src/main/java/de/intevation/gnv/action/sessionmodel/DefaultSessionModel.java --- a/gnv/src/main/java/de/intevation/gnv/action/sessionmodel/DefaultSessionModel.java Thu Apr 22 12:58:44 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/sessionmodel/DefaultSessionModel.java Fri Apr 23 07:55:05 2010 +0000 @@ -7,6 +7,7 @@ import java.util.Collection; import java.util.Iterator; +import java.util.Locale; import org.apache.log4j.Logger; @@ -39,11 +40,22 @@ private Collection statistics = null; + private Locale currentLocale; + /** * Constructor */ public DefaultSessionModel() { - super(); + this(null); + } + + /** + * Constructor + * + * @param currentLocale A locale used to create the user interface. + */ + public DefaultSessionModel(Locale currentLocale) { + this.currentLocale = currentLocale; } @@ -141,5 +153,13 @@ public void setStatistics(Collection statistics) { this.statistics = statistics; } + + public Locale getCurrentLocale() { + return currentLocale; + } + + public void setCurrentLocale(Locale currentLocale) { + this.currentLocale = currentLocale; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : diff -r f550bd27a3f1 -r 2659a5b1fa1e gnv/src/main/java/de/intevation/gnv/action/sessionmodel/SessionModel.java --- a/gnv/src/main/java/de/intevation/gnv/action/sessionmodel/SessionModel.java Thu Apr 22 12:58:44 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/sessionmodel/SessionModel.java Fri Apr 23 07:55:05 2010 +0000 @@ -8,6 +8,7 @@ import java.io.Serializable; import java.util.Collection; +import java.util.Locale; /** * This interface describe basic methods to store artifacts and diagram options. @@ -43,5 +44,9 @@ void setStatistics(Collection statistics); Collection getStatistics(); + + void setCurrentLocale(Locale locale); + + Locale getCurrentLocale(); } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : diff -r f550bd27a3f1 -r 2659a5b1fa1e gnv/src/main/java/de/intevation/gnv/action/sessionmodel/SessionModelFactory.java --- a/gnv/src/main/java/de/intevation/gnv/action/sessionmodel/SessionModelFactory.java Thu Apr 22 12:58:44 2010 +0000 +++ b/gnv/src/main/java/de/intevation/gnv/action/sessionmodel/SessionModelFactory.java Fri Apr 23 07:55:05 2010 +0000 @@ -54,7 +54,7 @@ if (obj instanceof SessionModel) { sm = (SessionModel) obj; } else { - sm = new DefaultSessionModel(); + sm = new DefaultSessionModel(request.getLocale()); request.getSession().setAttribute(SESSION_MODEL_ID, sm); } diff -r f550bd27a3f1 -r 2659a5b1fa1e gnv/src/main/webapp/WEB-INF/config/struts-config.xml --- a/gnv/src/main/webapp/WEB-INF/config/struts-config.xml Thu Apr 22 12:58:44 2010 +0000 +++ b/gnv/src/main/webapp/WEB-INF/config/struts-config.xml Fri Apr 23 07:55:05 2010 +0000 @@ -7,7 +7,7 @@ - + + + + + + diff -r f550bd27a3f1 -r 2659a5b1fa1e gnv/src/main/webapp/WEB-INF/jsp/header.jsp --- a/gnv/src/main/webapp/WEB-INF/jsp/header.jsp Thu Apr 22 12:58:44 2010 +0000 +++ b/gnv/src/main/webapp/WEB-INF/jsp/header.jsp Fri Apr 23 07:55:05 2010 +0000 @@ -17,7 +17,7 @@ · - + ·