Skip to content

Commit

Permalink
Added Faces#getMessageBundle().
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauke Scholtz committed Jun 28, 2014
1 parent ec490a8 commit 06e8ec7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
import static java.lang.Boolean.TRUE;
import static java.util.ResourceBundle.getBundle;
import static org.omnifaces.util.Faces.getLocale;
import static org.omnifaces.util.Faces.getMessageBundle;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponent;
Expand All @@ -30,7 +32,6 @@
import javax.faces.context.FacesContext;

import org.omnifaces.util.Components;
import org.omnifaces.util.Faces;
import org.omnifaces.util.Messages;
import org.omnifaces.util.State;
import org.omnifaces.validator.MultiFieldValidator;
Expand Down Expand Up @@ -163,16 +164,13 @@ private enum PropertyKeys {
*/
public ValidateMultipleFields() {
String componentType = getClass().getAnnotation(FacesComponent.class).value();
String messageBundle = Faces.getApplication().getMessageBundle();
ResourceBundle messageBundle = getMessageBundle();

if (messageBundle != null) {
defaultMessage = getBundle(messageBundle, getLocale()).getString(componentType);
}

if (defaultMessage == null) {
defaultMessage = getBundle(DEFAULT_MESSAGE_BUNDLE, getLocale()).getString(componentType);
if (messageBundle == null) {
messageBundle = getBundle(DEFAULT_MESSAGE_BUNDLE, getLocale());
}

defaultMessage = messageBundle.getString(componentType);
setRendererType(null);
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/omnifaces/util/Faces.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;

import javax.el.ELContext;
Expand Down Expand Up @@ -472,6 +474,19 @@ public static void setLocale(Locale locale) {
FacesLocal.setLocale(getContext(), locale);
}

/**
* Returns the message bundle as identified by <code>&lt;message-bundle&gt;</code> in <code>faces-config.xml</code>.
* The instance is already localized via {@link Faces#getLocale()}. If there is no
* <code>&lt;message-bundle&gt;</code>, then this method just returns <code>null</code>.
* @return The message bundle as identified by <code>&lt;message-bundle&gt;</code> in <code>faces-config.xml</code>.
* @since 2.0
* @throws MissingResourceException When the <code>&lt;message-bundle&gt;</code> in <code>faces-config.xml</code>
* does not refer an existing resource in the classpath.
*/
public static ResourceBundle getMessageBundle() {
return FacesLocal.getMessageBundle(getContext());
}

/**
* Perform the JSF navigation to the given outcome.
* @param outcome The navigation outcome.
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/omnifaces/util/FacesLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import javax.el.ELContext;
Expand Down Expand Up @@ -80,6 +81,7 @@ public final class FacesLocal {
private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
private static final int DEFAULT_SENDFILE_BUFFER_SIZE = 10240;
private static final String ERROR_NO_VIEW = "There is no view.";
private static final String ERROR_NO_MESSAGE_BUNDLE = "There is no message bundle.";
private static final String[] FACELET_CONTEXT_KEYS = {
FaceletContext.FACELET_CONTEXT_KEY, // Compiletime constant, may fail when compiled against EE6 and run on EE7.
"com.sun.faces.facelets.FACELET_CONTEXT", // JSF 2.0/2.1.
Expand Down Expand Up @@ -351,6 +353,19 @@ public static void setLocale(FacesContext context, Locale locale) {
viewRoot.setLocale(locale);
}

/**
* @see Faces#getMessageBundle()
*/
public static ResourceBundle getMessageBundle(FacesContext context) {
String messageBundle = context.getApplication().getMessageBundle();

if (messageBundle == null) {
return null;
}

return ResourceBundle.getBundle(messageBundle, getLocale(context));
}

/**
* @see Faces#navigate(String)
*/
Expand Down

0 comments on commit 06e8ec7

Please sign in to comment.