Example usage for org.springframework.web.servlet.support RequestContext getLocale

List of usage examples for org.springframework.web.servlet.support RequestContext getLocale

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support RequestContext getLocale.

Prototype

public final Locale getLocale() 

Source Link

Document

Return the current Locale (falling back to the request locale; never null ).

Usage

From source file:com.surfs.storage.web.utils.WebUtils.java

public static String getPropertiesMessage(HttpServletRequest request, String messageId, Object[] params) {
    RequestContext requestContext = new RequestContext(request);

    WebApplicationContext webContext = requestContext.getWebApplicationContext();
    String message = webContext.getMessage(messageId, params, "", requestContext.getLocale());
    return message;
}

From source file:org.parancoe.web.LanguageInterceptor.java

@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse res, Object object) throws Exception {
    RequestContext rc = new RequestContext(req);
    req.setAttribute("requestContext", rc);
    req.setAttribute("lang", rc.getLocale().getLanguage());
    logger.debug("LanguageInterceptor.preHandle()");
    return true;/*  w  w  w  .  j av a2s.c  o m*/
}

From source file:gov.nih.nci.calims2.taglib.form.SingleSelectTag.java

private List<Option> getOptionsFromEnums(RequestContext requestContext,
        Collection<I18nEnumeration> enumerations) {
    List<Option> result = new ArrayList<Option>();
    for (I18nEnumeration option : enumerations) {
        Option newOption = new Option(option.getName(), option.getLocalizedValue(requestContext.getLocale()));
        result.add(newOption);/*from   ww  w .  ja v  a 2  s  .  co  m*/
    }
    return result;
}

From source file:gov.nih.nci.calims2.taglib.form.OptionManager.java

/**
 * Wrap the options if necessary.//from  ww  w  . ja  va 2s .  c  om
 * 
 * @param <T> The type of options.
 * @param readOnly True if the tag is readOnly
 * @param requestContext The current request context.
 */
@SuppressWarnings("unchecked")
public <T> void wrapOptions(boolean readOnly, RequestContext requestContext) {
    String finalSelectKey = (readOnly) ? null : selectKey;
    if (optionCreatorCallback != null || finalSelectKey != null) {
        SelectHelper helper = new SelectHelperImpl();
        boolean enumeration = !options.isEmpty() && options.iterator().next() instanceof I18nEnumeration;
        if (enumeration) {
            options = helper.getEnumOptions((Collection<? extends I18nEnumeration>) options, null,
                    finalSelectKey, requestContext.getLocale());
        } else {
            options = helper.getOptions((Collection<T>) options,
                    (OptionCreatorCallback<T>) optionCreatorCallback, finalSelectKey);
        }
    }
}

From source file:gov.nih.nci.calims2.taglib.form.OptionManager.java

/**
 * Generates a new Option object for the given option that may be of any class.
 * /*from  w  w  w.j  a  v a 2 s .  c o  m*/
 * @param option The given option
 * @param requestContext The current request context.
 * @return The new Option generated.
 */
Option getOptionForOption(Object option, RequestContext requestContext) {
    if (option instanceof Option) {
        Option newOption = ((Option) option).clone();
        if (newOption.getLabelKey() != null) {
            newOption.setLabel(requestContext.getMessage(newOption.getLabelKey()));
        }
        return newOption;
    }
    if (option instanceof I18nEnumeration) {
        I18nEnumeration enumValue = (I18nEnumeration) option;
        return new Option(enumValue.getName(), enumValue.getLocalizedValue(requestContext.getLocale()));
    }
    BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(option);
    return new Option(wrapper.getPropertyValue(idProperty).toString(),
            wrapper.getPropertyValue(labelProperty).toString());
}

From source file:org.gvnix.datatables.tags.SpringContextHelper.java

/**
 * Returns the message translation for a code, in the {@link Locale} of the
 * current request.//from  www.  j a  v  a2 s. c om
 * 
 * @param pageContext of the current request.
 * @param code to get the message
 * @return the translated message
 * @throws JspException if there is an error getting the message
 */
public String resolveMessage(PageContext pageContext, String code) throws JspException {
    RequestContext requestContext = getRequestContext(pageContext);
    if (requestContext == null) {
        throw new JspTagException("No corresponding RequestContext found");
    }
    MessageSource messageSource = requestContext.getMessageSource();
    if (messageSource == null) {
        throw new JspTagException("No corresponding MessageSource found");
    }

    String resolvedCode = ExpressionEvaluationUtils.evaluateString("code", code, pageContext);

    if (resolvedCode != null) {
        // We have no fallback text to consider.
        try {
            return messageSource.getMessage(resolvedCode, null, requestContext.getLocale());
        } catch (NoSuchMessageException e) {
            LOG.warn("Unable to get message with code " + resolvedCode, e);
        }
    }

    return resolvedCode;
}

From source file:ua.com.manometer.jasperreports.AbstractJasperReportsView.java

/**
 * Expose current Spring-managed Locale and MessageSource to JasperReports i18n
 * ($R expressions etc). The MessageSource should only be exposed as JasperReports
 * resource bundle if no such bundle is defined in the report itself.
 * <p>The default implementation exposes the Spring RequestContext Locale and a
 * MessageSourceResourceBundle adapter for the Spring ApplicationContext,
 * analogous to the <code>JstlUtils.exposeLocalizationContext</code> method.
 * @see org.springframework.web.servlet.support.RequestContextUtils#getLocale
 * @see org.springframework.context.support.MessageSourceResourceBundle
 * @see #getApplicationContext()/*from   ww w  .ja v a2 s .c om*/
 * @see net.sf.jasperreports.engine.JRParameter#REPORT_LOCALE
 * @see net.sf.jasperreports.engine.JRParameter#REPORT_RESOURCE_BUNDLE
 * @see org.springframework.web.servlet.support.JstlUtils#exposeLocalizationContext
 */
protected void exposeLocalizationContext(Map<String, Object> model, HttpServletRequest request) {
    RequestContext rc = new RequestContext(request, getServletContext());
    if (!model.containsKey(JRParameter.REPORT_LOCALE)) {
        model.put(JRParameter.REPORT_LOCALE, rc.getLocale());
    }
    JasperReport report = getReport();
    if ((report == null || report.getResourceBundle() == null)
            && !model.containsKey(JRParameter.REPORT_RESOURCE_BUNDLE)) {
        model.put(JRParameter.REPORT_RESOURCE_BUNDLE,
                new MessageSourceResourceBundle(rc.getMessageSource(), rc.getLocale()));
    }
}