From: Roland Häder Date: Sat, 23 Sep 2017 20:12:46 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=47913981faa72280c41559f066eb09f69cb85bcc;p=jcore-utils.git Continued: - introduced getMessageFromBundle() which will return a localized message from message bundle Signed-off-by: Roland Häder --- diff --git a/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java index 5496e29..ac57b23 100644 --- a/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java +++ b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java @@ -53,7 +53,7 @@ public abstract class BaseFacesBean extends BaseBean { */ protected String determinePrincipalName () { // Get principal - Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal(); + final Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal(); // Init with null String principalName = null; @@ -80,11 +80,51 @@ public abstract class BaseFacesBean extends BaseBean { */ protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { // Get context parameter - Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + final Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it return contextValue; } + /** + * Returns a message based on given i18nKey or puts it into three question + * marks each side when not found. + *

+ * @param i18nKey I18n key + *

+ * @return Localized message + */ + protected String getMessageFromBundle (final String i18nKey) { + // Validate parameter + if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + + // Get current locale + final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + + // Get bundle bundle + final ResourceBundle bundle = this.loadResourceBundle(locale); + + // Default is i18nKey + String message = MessageFormat.format("???{0}???", i18nKey); //NOI18N + + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + + // Return it + return message; + } + /** * Returns given property key or throws an exception if not found. *

@@ -96,12 +136,14 @@ public abstract class BaseFacesBean extends BaseBean { */ protected String getStringContextParameter (final String parameterKey) throws NullPointerException { // Get context parameter - String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + final String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? if (null == contextValue) { // Throw NPE throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N } + // Return it return contextValue; } @@ -122,10 +164,13 @@ public abstract class BaseFacesBean extends BaseBean { // Is empty throw new IllegalArgumentException("controllerName is empty"); //NOI18N } + // Try to get context parameter - String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + final String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? - boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + final boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it return isEnabled; } @@ -178,19 +223,10 @@ public abstract class BaseFacesBean extends BaseBean { // Is empty throw new IllegalArgumentException("i18nKey is null"); //NOI18N } - // Get current locale - Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); - // Get bundle bundle - ResourceBundle bundle = this.loadResourceBundle(locale); - // Default is i18nKey - String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N - // Try it - try { - // Get message - message = bundle.getString(i18nKey); - } catch (final MissingResourceException ex) { - // Did not find it, ignored - } + + // Get message from bundle + final String message = this.getMessageFromBundle(i18nKey); + // Get context and add message FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); }