Author: nbelaevski
Date: 2008-01-24 20:23:45 -0500 (Thu, 24 Jan 2008)
New Revision: 5617
Modified:
branches/3.1.x/framework/impl/src/main/java/org/richfaces/component/util/MessageUtil.java
Log:
http://jira.jboss.com/jira/browse/RF-2018
Modified:
branches/3.1.x/framework/impl/src/main/java/org/richfaces/component/util/MessageUtil.java
===================================================================
---
branches/3.1.x/framework/impl/src/main/java/org/richfaces/component/util/MessageUtil.java 2008-01-25
01:23:41 UTC (rev 5616)
+++
branches/3.1.x/framework/impl/src/main/java/org/richfaces/component/util/MessageUtil.java 2008-01-25
01:23:45 UTC (rev 5617)
@@ -21,8 +21,15 @@
package org.richfaces.component.util;
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
import javax.faces.application.Application;
+import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
+import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;
@@ -65,4 +72,87 @@
return o;
}
+
+ private static final ResourceBundle getResourceBundle(String baseName, Locale locale,
ClassLoader loader) {
+ if (loader != null) {
+ return ResourceBundle.getBundle(baseName, locale, loader);
+ } else {
+ return ResourceBundle.getBundle(baseName, locale);
+ }
+ }
+
+ private static final FacesMessage getMessage(FacesContext context, String messageId,
+ Object[] parameters, Locale locale) {
+ String summary = null;
+ String detail = null;
+
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+
+ if (context != null) {
+ Application application = context.getApplication();
+ if (application != null) {
+ String messageBundleName = application.getMessageBundle();
+
+ if (messageBundleName != null) {
+ ResourceBundle bundle = getResourceBundle(messageBundleName, locale, loader);
+ if (bundle != null) {
+ try {
+ summary = bundle.getString(messageId);
+ detail = bundle.getString(messageId + "_detail");
+ } catch (MissingResourceException e) {
+ //do nothing
+ }
+ }
+ }
+ }
+ }
+
+ if (summary == null) {
+ ResourceBundle bundle = getResourceBundle(FacesMessage.FACES_MESSAGES, locale,
loader);
+ try {
+ summary = bundle.getString(messageId);
+
+ if (summary == null) {
+ return null;
+ }
+
+ detail = bundle.getString(messageId + "_detail");
+ } catch (MissingResourceException e) {
+ //do nothing
+ }
+ }
+
+ String formattedSummary = MessageFormat.format(summary, parameters);
+ String formattedDetail = null;
+ if (detail != null) {
+ formattedDetail = MessageFormat.format(detail, parameters);
+ }
+
+ return new FacesMessage(formattedSummary, formattedDetail);
+ }
+
+ public static final FacesMessage getMessage(FacesContext context, String messageId,
+ Object[] parameters) {
+
+ Locale locale;
+ FacesMessage result = null;
+
+ if (context != null) {
+ UIViewRoot viewRoot = context.getViewRoot();
+ if (viewRoot != null) {
+ locale = viewRoot.getLocale();
+
+ if (locale != null) {
+ result = getMessage(context, messageId, parameters, locale);
+ }
+ }
+ }
+
+ if (result == null) {
+ locale = Locale.getDefault();
+ result = getMessage(context, messageId, parameters, locale);
+ }
+
+ return result;
+ }
}