From 200157d1b49581843adbf22376503d3e9da6f84a Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 4 Apr 2016 22:12:15 +0200 Subject: [PATCH] Localization support almost finished: - Locale setting from browser comes first - User selected locale from web interfaces overrides it (in session bean) TODO/FIXME: - Add locale support to juser-core - Still the selected locale is not re-selected in box --- nbproject/faces-config.NavData | 44 +++++------ .../localization/bundle_de_DE.properties | 3 + .../localization/bundle_en_US.properties | 3 + .../PizzaLocalizationSessionBean.java | 46 ++++++++++- .../PizzaLocalizationSessionController.java | 7 ++ web/WEB-INF/faces-config.xml | 6 ++ web/WEB-INF/templates/base.tpl | 77 ++++++++++--------- .../generic/locale_selection_box.tpl | 6 +- web/resources/css/cssLayout.css | 52 ++++++++++--- 9 files changed, 165 insertions(+), 79 deletions(-) diff --git a/nbproject/faces-config.NavData b/nbproject/faces-config.NavData index dc560dfa..69a5d2ec 100644 --- a/nbproject/faces-config.NavData +++ b/nbproject/faces-config.NavData @@ -1,25 +1,25 @@ - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/java/org/mxchange/localization/bundle_de_DE.properties b/src/java/org/mxchange/localization/bundle_de_DE.properties index 8e05451c..f15191e0 100644 --- a/src/java/org/mxchange/localization/bundle_de_DE.properties +++ b/src/java/org/mxchange/localization/bundle_de_DE.properties @@ -193,3 +193,6 @@ TERMS_CONDITIONS=Allgemeine Gesch\u00e4ftsbestimmungen NONE_SELECTED=k.A. ENTERED_HOUSE_NUMBER_INVALID=Die eingegebene Hausnummer ist ung\u00fcltig. ENTERED_ZIP_CODE_INVALID=Die eingegebene Postleitzahl ist ung\u00fcltig. +SELECT_LANGUAGE=Sprache: +DE_DE=Deutsch (DE) +EN_US=Englisch (US) diff --git a/src/java/org/mxchange/localization/bundle_en_US.properties b/src/java/org/mxchange/localization/bundle_en_US.properties index f3f9db31..be73f232 100644 --- a/src/java/org/mxchange/localization/bundle_en_US.properties +++ b/src/java/org/mxchange/localization/bundle_en_US.properties @@ -193,3 +193,6 @@ TERMS_CONDITIONS=Terms & Conditions NONE_SELECTED=Empty ENTERED_HOUSE_NUMBER_INVALID=The entered house number is invalid. ENTERED_ZIP_CODE_INVALID=The entered ZIP code is invalid. +SELECT_LANGUAGE=Language: +DE_DE=German (DE) +EN_US=English (US) diff --git a/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java index 1e98b41e..bcd72c06 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java @@ -16,6 +16,7 @@ */ package org.mxchange.pizzaapplication.beans.localization; +import java.text.MessageFormat; import java.util.Locale; import javax.annotation.PostConstruct; import javax.faces.bean.SessionScoped; @@ -38,7 +39,7 @@ public class PizzaLocalizationSessionBean extends BaseDatabaseBean implements Pi /** * Serial number */ - private static final long serialVersionUID = 1_867_671_657_629_601_528L; + private static final long serialVersionUID = 639_184_671_562_195L; /** * Current Locale @@ -47,35 +48,72 @@ public class PizzaLocalizationSessionBean extends BaseDatabaseBean implements Pi @Override public String getLanguage () { - return this.getLocale().getLanguage(); + this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N + return this.getLocale().getLanguage().toLowerCase(); } @Override public void setLanguage (final String language) { - this.setLocale(new Locale(language)); - FacesContext.getCurrentInstance().getViewRoot().setLocale(this.getLocale()); + // Log trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N + + // Language splits + String[] splits = language.split("_"); + if (null == splits[1]) { + splits[1] = ""; + } + + // Get new locale with upper-case country code + Locale loc = new Locale(splits[0], splits[1]); + + // Log debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N + + // Set it here and in the JSF context + this.setLocale(loc); + FacesContext.getCurrentInstance().getViewRoot().setLocale(loc); + + // Log trace message + this.getLoggerBeanLocal().logTrace("JobsLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N } @Override public Locale getLocale () { + this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N return this.locale; } @Override public void setLocale (final Locale locale) { + this.getLoggerBeanLocal().logTrace(MessageFormat.format("JobsLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N this.locale = locale; } + @Override + public Locale[] getSelectableLocalizations () { + Locale[] locales = {Locale.GERMANY, Locale.US}; + return locales; + } + /** * Initializer for this bean */ @PostConstruct public void init () { + // Log trace message + this.getLoggerBeanLocal().logTrace("JobsLocalizationSessionBean::init: CALLED!"); //NOI18N + // Create locale instance from context Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale(); + // Log debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("JobsLocalizationSessionBean::init: loc={0}", loc)); //NOI18N + // Set it here this.setLocale(loc); + + // Log trace message + this.getLoggerBeanLocal().logTrace("JobsLocalizationSessionBean::init: EXIT!"); //NOI18N } } diff --git a/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionController.java b/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionController.java index 430923d0..7150b330 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionController.java @@ -54,4 +54,11 @@ public interface PizzaLocalizationSessionController extends Serializable { */ void setLanguage (final String language); + /** + * Getter for selectable localizations + *

+ * @return Selectable localizations + */ + Locale[] getSelectableLocalizations (); + } diff --git a/web/WEB-INF/faces-config.xml b/web/WEB-INF/faces-config.xml index c1187b0f..d181a563 100644 --- a/web/WEB-INF/faces-config.xml +++ b/web/WEB-INF/faces-config.xml @@ -3,6 +3,12 @@ xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> + + + de_DE + en_US + + PrivacyTermsCheckboxValidator org.mxchange.jcoreee.validator.bool.privacy_terms.PrivacyTermsCheckboxValidator diff --git a/web/WEB-INF/templates/base.tpl b/web/WEB-INF/templates/base.tpl index 712904cf..96b05e21 100644 --- a/web/WEB-INF/templates/base.tpl +++ b/web/WEB-INF/templates/base.tpl @@ -6,55 +6,56 @@ xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://xmlns.jcp.org/jsf/core"> - - + + + - - + - - + + - Pizza-Service - <ui:insert name="title">Default title</ui:insert> - + Pizza-Service - <ui:insert name="title">Default title</ui:insert> + - -

-