Example usage for javax.servlet.jsp.jstl.fmt LocalizationContext getLocale

List of usage examples for javax.servlet.jsp.jstl.fmt LocalizationContext getLocale

Introduction

In this page you can find the example usage for javax.servlet.jsp.jstl.fmt LocalizationContext getLocale.

Prototype

public Locale getLocale() 

Source Link

Document

Gets the locale of this I18N localization context.

Usage

From source file:com.sun.faces.util.Util.java

/**
 * Return a Locale instance using the following algorithm: <P>
 * <p/>/*from  ww w . j a v a  2s . co m*/
 * <UL>
 * <p/>
 * <LI>
 * <p/>
 * If this component instance has an attribute named "bundle",
 * interpret it as a model reference to a LocalizationContext
 * instance accessible via FacesContext.getModelValue().
 * <p/>
 * </LI>
 * <p/>
 * <LI>
 * <p/>
 * If FacesContext.getModelValue() returns a LocalizationContext
 * instance, return its Locale.
 * <p/>
 * </LI>
 * <p/>
 * <LI>
 * <p/>
 * If FacesContext.getModelValue() doesn't return a
 * LocalizationContext, return the FacesContext's Locale.
 * <p/>
 * </LI>
 * <p/>
 * </UL>
 */

public static Locale getLocaleFromContextOrComponent(FacesContext context, UIComponent component) {
    Locale result = null;
    String bundleName = null, bundleAttr = "bundle";

    Util.parameterNonNull(context);
    Util.parameterNonNull(component);

    // verify our component has the proper attributes for bundle.
    if (null != (bundleName = (String) component.getAttributes().get(bundleAttr))) {
        // verify there is a Locale for this localizationContext
        javax.servlet.jsp.jstl.fmt.LocalizationContext locCtx = null;
        if (null != (locCtx = (javax.servlet.jsp.jstl.fmt.LocalizationContext) (Util
                .getValueBinding(bundleName)).getValue(context))) {
            result = locCtx.getLocale();
            Util.doAssert(null != result);
        }
    }
    if (null == result) {
        result = context.getViewRoot().getLocale();
    }

    return result;
}