From c04ebab4949bf859aa14878ef29cfd7a174853a6 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Mon, 4 Apr 2016 22:12:12 +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 | 10 +-- .../JobsLocalizationSessionBean.java | 46 +++++++++++-- .../JobsLocalizationSessionController.java | 7 ++ .../localization/bundle_de_DE.properties | 3 + .../localization/bundle_en_US.properties | 3 + web/WEB-INF/faces-config.xml | 2 +- web/WEB-INF/templates/base.tpl | 65 ++++++++++--------- .../generic/locale_selection_box.tpl | 6 +- web/index.xhtml | 11 ++-- web/resources/css/cssLayout.css | 4 +- 10 files changed, 105 insertions(+), 52 deletions(-) diff --git a/nbproject/faces-config.NavData b/nbproject/faces-config.NavData index 5ab655d9..05b77d84 100644 --- a/nbproject/faces-config.NavData +++ b/nbproject/faces-config.NavData @@ -2,26 +2,26 @@ - + + - - + - + + - diff --git a/src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionBean.java b/src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionBean.java index 21e97bbc..6abf1374 100644 --- a/src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionBean.java @@ -16,9 +16,10 @@ */ package org.mxchange.jjobs.beans.localization; +import java.text.MessageFormat; import java.util.Locale; import javax.annotation.PostConstruct; -import javax.faces.bean.SessionScoped; +import javax.enterprise.context.SessionScoped; import javax.faces.context.FacesContext; import javax.inject.Named; import org.mxchange.jcoreee.database.BaseDatabaseBean; @@ -47,35 +48,72 @@ public class JobsLocalizationSessionBean extends BaseDatabaseBean implements Job @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/jjobs/beans/localization/JobsLocalizationSessionController.java b/src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionController.java index b2818729..857dddc1 100644 --- a/src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionController.java +++ b/src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionController.java @@ -54,4 +54,11 @@ public interface JobsLocalizationSessionController extends Serializable { */ void setLanguage (final String language); + /** + * Getter for selectable localizations + *

+ * @return Selectable localizations + */ + Locale[] getSelectableLocalizations (); + } diff --git a/src/java/org/mxchange/localization/bundle_de_DE.properties b/src/java/org/mxchange/localization/bundle_de_DE.properties index ab1a1909..82d0c371 100644 --- a/src/java/org/mxchange/localization/bundle_de_DE.properties +++ b/src/java/org/mxchange/localization/bundle_de_DE.properties @@ -286,3 +286,6 @@ CONTENT_TITLE_LOGIN_CHANGE_EMAIL_ADDRESS=Neue Email-Adresse eingeben: PAGE_TITLE_LOGIN_CHANGE_PERSONAL_DATA=Ihre persoenlichen Daten aendern #TODO: Please fix German umlaut! CONTENT_TITLE_LOGIN_CHANGE_PERSONAL_DATA=Aendern Ihrer Adresse, Telefonnummer usw.: +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 654681f7..3dfe86d6 100644 --- a/src/java/org/mxchange/localization/bundle_en_US.properties +++ b/src/java/org/mxchange/localization/bundle_en_US.properties @@ -260,3 +260,6 @@ PAGE_TITLE_LOGIN_CHANGE_EMAIL_ADDRESS=Change your email address CONTENT_TITLE_LOGIN_CHANGE_EMAIL_ADDRESS=Enter new email address: PAGE_TITLE_LOGIN_CHANGE_PERSONAL_DATA=Change your personal data CONTENT_TITLE_LOGIN_CHANGE_PERSONAL_DATA=Change your address, phone number or others: +SELECT_LANGUAGE=Language: +DE_DE=German (DE) +EN_US=English (US) diff --git a/web/WEB-INF/faces-config.xml b/web/WEB-INF/faces-config.xml index 8c82d2f9..0159679a 100644 --- a/web/WEB-INF/faces-config.xml +++ b/web/WEB-INF/faces-config.xml @@ -5,7 +5,7 @@ xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> - de + de_DE en_US diff --git a/web/WEB-INF/templates/base.tpl b/web/WEB-INF/templates/base.tpl index 9bcf44e7..b2187caf 100644 --- a/web/WEB-INF/templates/base.tpl +++ b/web/WEB-INF/templates/base.tpl @@ -6,48 +6,49 @@ xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://xmlns.jcp.org/jsf/core"> - - + + + - - + - - + + - JJobs - <ui:insert name="title">Default title</ui:insert> - + JJobs - <ui:insert name="title">Default title</ui:insert> + - -

-