From: Roland Häder Date: Thu, 12 Oct 2017 20:05:03 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f7c5b24dcf9ac8f3f950e3f5b4c90b3988a6b661;p=jcoreee.git Continued: - let's support both generic and project-specific bundles to have most flexibility - also moved resource bundle list to this class to have it further encapsulated - also the loadResourceBundle() method has been renamed to loadResourceBundles() does no longer return the bundle(s). You have to use BaseFacesBean.getBundles() for now on. 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 ac57b23..15194e3 100644 --- a/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java +++ b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java @@ -18,6 +18,8 @@ package org.mxchange.jcoreee.bean.faces; import java.security.Principal; import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.MissingResourceException; import java.util.ResourceBundle; @@ -32,11 +34,34 @@ import org.mxchange.jcoreee.bean.BaseBean; */ public abstract class BaseFacesBean extends BaseBean { + /** + * Loaded resource bundles ("cached") + */ + private static final List RESOURCE_BUNDLES; + /** * Serial number */ private static final long serialVersionUID = 18_605_498_672_261L; + /** + * Static initializer + */ + static { + // Init resource bundle list + RESOURCE_BUNDLES = new ArrayList<>(2); + } + + /** + * Getter for resource bundle list + *

+ * @return Resource bundle list + */ + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + protected static List getBundles () { + return RESOURCE_BUNDLES; + } + /** * Protected constructor */ @@ -108,17 +133,22 @@ public abstract class BaseFacesBean extends BaseBean { final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); // Get bundle bundle - final ResourceBundle bundle = this.loadResourceBundle(locale); + this.loadResourceBundles(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 + // Loop through all + for (final ResourceBundle bundle : getBundles()) { + // Found message? + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + break; + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } } // Return it @@ -176,16 +206,14 @@ public abstract class BaseFacesBean extends BaseBean { } /** - * Loads resource bundle for given locale. This must be implemented per + * Loads resource bundles for given locale. This must be implemented per * project so all projects can still customize their methods. Calling * ResourceBundleloadBundle() in this class means that also the bundle files * must be present here. *

* @param locale Locale from e.g. FacesContext - *

- * @return Initialized and loaded resource bundle */ - protected abstract ResourceBundle loadResourceBundle (final Locale locale); + protected abstract void loadResourceBundles (final Locale locale); /** * Shows a faces message for given causing exception. The message from the