org.andromda.cartridges.jsf.Messages Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of andromda-jsf-cartridge-components Show documentation
Show all versions of andromda-jsf-cartridge-components Show documentation
Constains custom JSF components used by the JSF cartridge.
The newest version!
package org.andromda.cartridges.jsf;
import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.faces.context.FacesContext;
/**
* Provides access to the application resource messages.
*/
public class Messages
{
/**
* The messages bundle name.
*/
private static final String BUNDLE_NAME = "message-resources";
/**
* Gets the message given the key
by substituting any
* required arguments
.
*
* @param key the message key.
* @param arguments any message arguments to substitute.
* @return the message (or key if the message isn't found).
*/
public static String get(
String key,
Object[] arguments)
{
FacesContext context = FacesContext.getCurrentInstance();
String resourceString;
try
{
final ResourceBundle bundle = ResourceBundle.getBundle(
BUNDLE_NAME,
context.getViewRoot().getLocale());
resourceString = bundle.getString(key);
if (arguments != null)
{
final MessageFormat format = new MessageFormat(resourceString,
context.getViewRoot().getLocale());
resourceString = format.format(arguments);
}
}
catch (final MissingResourceException exception)
{
resourceString = key;
}
return resourceString;
}
}