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

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

Introduction

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

Prototype

public static Locale getLocale(HttpServletRequest request) 

Source Link

Document

Retrieve the current locale from the given request, using the LocaleResolver bound to the request by the DispatcherServlet (if available), falling back to the request's accept-header Locale.

Usage

From source file:alfio.util.LocaleUtil.java

public static Locale getTicketLanguage(Ticket t, HttpServletRequest request) {
    return Optional.ofNullable(t.getUserLanguage()).filter(StringUtils::isNotBlank).map(Locale::forLanguageTag)
            .orElseGet(() -> RequestContextUtils.getLocale(request));
}

From source file:jetx.ext.springmvc.SpringMvcTags.java

public static void i18n(JetTagContext ctx, String code, Object[] args, String defaultMessage)
        throws IOException {

    HttpServletRequest request = ExtendUtils.getHttpServletRequest(ctx);

    MessageSource messageSource = RequestContextUtils.getWebApplicationContext(request);
    Locale locale = RequestContextUtils.getLocale(request);
    String value = null;//from w  w  w. j  av a2  s  . c  om

    try {
        value = messageSource.getMessage(code, args, defaultMessage, locale);
    } catch (NoSuchMessageException e) {
        throw ExceptionUtils.uncheck(e);
    }

    if (value != null) {
        ctx.getWriter().print(value);
    }
}

From source file:com.aspose.showcase.qrcodegen.web.api.controller.BaseController.java

protected String getLocalizedMessage(String messageKey, HttpServletRequest request) {

    LOGGER.debug("BaseController::getLocalizedMessage");

    return messageSource.getMessage(messageKey, ArrayUtils.EMPTY_STRING_ARRAY, messageKey,
            RequestContextUtils.getLocale(request));
}

From source file:org.openmrs.module.openhmis.backboneforms.web.controller.BackboneMessageRenderController.java

@RequestMapping(method = RequestMethod.GET)
public ModelAndView render(HttpServletRequest request) {
    Locale locale = RequestContextUtils.getLocale(request);
    ResourceBundle resourceBundle = ResourceBundle.getBundle("messages", locale);
    return new ModelAndView(BackboneWebConstants.MESSAGE_PAGE, "keys", resourceBundle.getKeys());
}

From source file:com.jeanchampemont.notedown.utils.PrettyTimeInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView modelAndView) throws Exception {
    if (modelAndView != null) {
        PrettyTime prettyTime = new PrettyTime();
        prettyTime.setLocale(RequestContextUtils.getLocale(request));
        modelAndView.getModel().put("prettyTime", prettyTime);
    }/* w w w  .  j  a va2s  .c om*/
}

From source file:org.displaytag.localization.I18nSpringAdapter.java

/**
 * @see LocaleResolver#resolveLocale(HttpServletRequest)
 */
public Locale resolveLocale(HttpServletRequest request) {
    return RequestContextUtils.getLocale(request);
}

From source file:net.sourceforge.subsonic.taglib.FormatBytesTag.java

public int doEndTag() throws JspException {
    Locale locale = RequestContextUtils.getLocale((HttpServletRequest) pageContext.getRequest());
    String result = StringUtil.formatBytes(bytes, locale);

    try {/*from w  ww  . j a  va 2s  .  c om*/
        pageContext.getOut().print(result);
    } catch (IOException x) {
        throw new JspTagException(x);
    }
    return EVAL_PAGE;
}

From source file:org.openmrs.module.openhmis.inventory.web.controller.InventoryMessageRenderController.java

@RequestMapping(method = RequestMethod.GET)
public ModelAndView render(HttpServletRequest request) {
    // object to store keys from inventory and backboneforms
    Vector<String> keys = new Vector<String>();

    Locale locale = RequestContextUtils.getLocale(request);
    ResourceBundle resourceBundle = ResourceBundle.getBundle("messages", locale);

    // store inventory message keys in the vector object
    keys.addAll(resourceBundle.keySet());

    // retrieve backboneforms messages
    BackboneMessageRenderController backboneController = new BackboneMessageRenderController();
    ModelAndView modelAndView = backboneController.render(request);

    // store backboneforms message keys in the vector object
    for (Map.Entry<String, Object> messageKeys : modelAndView.getModel().entrySet()) {
        Enumeration<String> messageKey = (Enumeration<String>) messageKeys.getValue();
        while (messageKey.hasMoreElements()) {
            String key = messageKey.nextElement();
            if (!keys.contains(key))
                keys.add(key);/*ww w.java  2  s .co  m*/
        }
    }

    return new ModelAndView(ModuleWebConstants.MESSAGE_PAGE, "keys", keys.elements());
}

From source file:org.openmrs.module.openhmis.cashier.web.controller.CashierMessageRenderController.java

@RequestMapping(method = RequestMethod.GET)
public ModelAndView render(HttpServletRequest request) {
    // object to store keys from cashier and backboneforms
    Vector<String> keys = new Vector<String>();

    // locate and retrieve cashier messages
    Locale locale = RequestContextUtils.getLocale(request);
    ResourceBundle resourceBundle = ResourceBundle.getBundle("messages", locale);

    // store cashier message keys in the vector object
    keys.addAll(resourceBundle.keySet());

    // retrieve backboneforms messages
    BackboneMessageRenderController backboneController = new BackboneMessageRenderController();
    ModelAndView modelAndView = backboneController.render(request);

    // store backboneforms message keys in the vector object
    for (Map.Entry<String, Object> messageKeys : modelAndView.getModel().entrySet()) {
        Enumeration<String> messageKey = (Enumeration<String>) messageKeys.getValue();
        while (messageKey.hasMoreElements()) {
            String key = messageKey.nextElement();
            if (!keys.contains(key))
                keys.add(key);/*www . j  a  va 2 s. c  o  m*/
        }
    }

    return new ModelAndView(CashierWebConstants.MESSAGE_PAGE, "keys", keys.elements());
}

From source file:net.sourceforge.subsonic.controller.AbstractChartController.java

private Color getColor(String code, HttpServletRequest request) {
    Theme theme = RequestContextUtils.getTheme(request);
    Locale locale = RequestContextUtils.getLocale(request);
    String colorHex = theme.getMessageSource().getMessage(code, new Object[0], locale);
    return new Color(Integer.parseInt(colorHex, 16));
}