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

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

Introduction

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

Prototype

public final MessageSource getMessageSource() 

Source Link

Document

Return the current WebApplicationContext as MessageSource.

Usage

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   w  w w  . j a  v  a2 s  .  co m*/
 * 
 * @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  w w w. ja  va 2s .  c o m
 * @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()));
    }
}