From f7c5b24dcf9ac8f3f950e3f5b4c90b3988a6b661 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Thu, 12 Oct 2017 22:05:03 +0200 Subject: [PATCH] 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. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../jcoreee/bean/faces/BaseFacesBean.java | 50 +++++++++++++++---- 1 file changed, 39 insertions(+), 11 deletions(-) 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 -- 2.39.5