Example usage for org.springframework.context.i18n LocaleContextHolder setLocaleContext

List of usage examples for org.springframework.context.i18n LocaleContextHolder setLocaleContext

Introduction

In this page you can find the example usage for org.springframework.context.i18n LocaleContextHolder setLocaleContext.

Prototype

public static void setLocaleContext(@Nullable LocaleContext localeContext) 

Source Link

Document

Associate the given LocaleContext with the current thread, not exposing it as inheritable for child threads.

Usage

From source file:org.musicrecital.webapp.filter.LocaleFilter.java

/**
 * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale
 * and also configures it to work with JSTL.
 * /*from w  w w.ja v a  2s.c o  m*/
 * @param request the current request
 * @param response the current response
 * @param chain the chain
 * @throws IOException when something goes wrong
 * @throws ServletException when a communication failure happens
 */
@SuppressWarnings("unchecked")
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    String locale = request.getParameter("locale");
    Locale preferredLocale = null;

    if (locale != null) {
        int indexOfUnderscore = locale.indexOf('_');
        if (indexOfUnderscore != -1) {
            String language = locale.substring(0, indexOfUnderscore);
            String country = locale.substring(indexOfUnderscore + 1);
            preferredLocale = new Locale(language, country);
        } else {
            preferredLocale = new Locale(locale);
        }
    }

    HttpSession session = request.getSession(false);

    if (session != null) {
        if (preferredLocale == null) {
            preferredLocale = (Locale) session.getAttribute(Constants.PREFERRED_LOCALE_KEY);
        } else {
            session.setAttribute(Constants.PREFERRED_LOCALE_KEY, preferredLocale);
            Config.set(session, Config.FMT_LOCALE, preferredLocale);
        }

        if (preferredLocale != null && !(request instanceof LocaleRequestWrapper)) {
            request = new LocaleRequestWrapper(request, preferredLocale);
            LocaleContextHolder.setLocale(preferredLocale);
        }
    }

    chain.doFilter(request, response);

    // Reset thread-bound LocaleContext.
    LocaleContextHolder.setLocaleContext(null);
}

From source file:org.openmrs.contrib.metadatarepository.webapp.filter.LocaleFilter.java

/**
 * This method looks for a "locale" request parameter. If it finds one, it sets it as the preferred locale
 * and also configures it to work with JSTL.
 * /*from   w w w  .ja  v  a  2  s.com*/
 * @param request the current request
 * @param response the current response
 * @param chain the chain
 * @throws IOException when something goes wrong
 * @throws ServletException when a communication failure happens
 */
@SuppressWarnings("unchecked")
public void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    String locale = request.getParameter("locale");
    Locale preferredLocale = null;

    if (locale != null) {
        int indexOfUnderscore = locale.indexOf('_');
        if (indexOfUnderscore != -1) {
            String language = locale.substring(0, indexOfUnderscore);
            String country = locale.substring(indexOfUnderscore + 1);
            preferredLocale = new Locale(language, country);
        } else {
            preferredLocale = new Locale(locale);
        }
    }

    HttpSession session = request.getSession(false);

    if (session != null) {
        if (preferredLocale == null) {
            preferredLocale = (Locale) session.getAttribute(Constants.PREFERRED_LOCALE_KEY);
        } else {
            session.setAttribute(Constants.PREFERRED_LOCALE_KEY, preferredLocale);
            Config.set(session, Config.FMT_LOCALE, preferredLocale);
        }

        if (preferredLocale != null && !(request instanceof LocaleRequestWrapper)) {
            request = new LocaleRequestWrapper(request, preferredLocale);
            LocaleContextHolder.setLocale(preferredLocale);
        }
    }

    String theme = request.getParameter("theme");
    if (theme != null && request.isUserInRole(Constants.ADMIN_ROLE)) {
        Map<String, Object> config = (Map) getServletContext().getAttribute(Constants.CONFIG);
        config.put(Constants.CSS_THEME, theme);
    }

    chain.doFilter(request, response);

    // Reset thread-bound LocaleContext.
    LocaleContextHolder.setLocaleContext(null);
}

From source file:alpha.portal.webapp.filter.LocaleFilter.java

/**
 * This method looks for a "locale" request parameter. If it finds one, it
 * sets it as the preferred locale and also configures it to work with JSTL.
 * //from ww  w.j a v a2 s . c o  m
 * @param request
 *            the current request
 * @param response
 *            the current response
 * @param chain
 *            the chain
 * @throws IOException
 *             when something goes wrong
 * @throws ServletException
 *             when a communication failure happens
 */
@Override
@SuppressWarnings("unchecked")
public void doFilterInternal(HttpServletRequest request, final HttpServletResponse response,
        final FilterChain chain) throws IOException, ServletException {

    final String locale = request.getParameter("locale");
    Locale preferredLocale = null;

    if (locale != null) {
        final int indexOfUnderscore = locale.indexOf('_');
        if (indexOfUnderscore != -1) {
            final String language = locale.substring(0, indexOfUnderscore);
            final String country = locale.substring(indexOfUnderscore + 1);
            preferredLocale = new Locale(language, country);
        } else {
            preferredLocale = new Locale(locale);
        }
    }

    final HttpSession session = request.getSession(false);

    if (session != null) {
        if (preferredLocale == null) {
            preferredLocale = (Locale) session.getAttribute(Constants.PREFERRED_LOCALE_KEY);
        } else {
            session.setAttribute(Constants.PREFERRED_LOCALE_KEY, preferredLocale);
            Config.set(session, Config.FMT_LOCALE, preferredLocale);
        }

        if ((preferredLocale != null) && !(request instanceof LocaleRequestWrapper)) {
            request = new LocaleRequestWrapper(request, preferredLocale);
            LocaleContextHolder.setLocale(preferredLocale);
        }
    }

    final String theme = request.getParameter("theme");
    if ((theme != null) && request.isUserInRole(Constants.ADMIN_ROLE)) {
        final Map<String, Object> config = (Map) this.getServletContext().getAttribute(Constants.CONFIG);
        config.put(Constants.CSS_THEME, theme);
    }

    chain.doFilter(request, response);

    // Reset thread-bound LocaleContext.
    LocaleContextHolder.setLocaleContext(null);
}

From source file:ejportal.webapp.filter.LocaleFilter.java

/**
 * This method looks for a "locale" request parameter. If it finds one, it
 * sets it as the preferred locale and also configures it to work with JSTL.
 * //  w  w  w  . j a v  a 2  s  .  c om
 * @param request
 *            the current request
 * @param response
 *            the current response
 * @param chain
 *            the chain
 * @throws IOException
 *             when something goes wrong
 * @throws ServletException
 *             when a communication failure happens
 */
@Override
@SuppressWarnings("unchecked")
public void doFilterInternal(HttpServletRequest request, final HttpServletResponse response,
        final FilterChain chain) throws IOException, ServletException {

    final String locale = request.getParameter("locale");
    Locale preferredLocale = null;

    if (locale != null) {
        final int indexOfUnderscore = locale.indexOf('_');
        if (indexOfUnderscore != -1) {
            final String language = locale.substring(0, indexOfUnderscore);
            final String country = locale.substring(indexOfUnderscore + 1);
            preferredLocale = new Locale(language, country);
        } else {
            preferredLocale = new Locale(locale);
        }
    }

    final HttpSession session = request.getSession(false);

    if (session != null) {
        if (preferredLocale == null) {
            preferredLocale = (Locale) session.getAttribute(Constants.PREFERRED_LOCALE_KEY);
        } else {
            session.setAttribute(Constants.PREFERRED_LOCALE_KEY, preferredLocale);
            Config.set(session, Config.FMT_LOCALE, preferredLocale);
        }

        if ((preferredLocale != null) && !(request instanceof LocaleRequestWrapper)) {
            request = new LocaleRequestWrapper(request, preferredLocale);
            LocaleContextHolder.setLocale(preferredLocale);
        }
    }

    final String theme = request.getParameter("theme");
    // if (theme != null && request.isUserInRole(Constants.ADMIN_ROLE)) {
    // TOD hartkodiert
    if ((theme != null) && request.isUserInRole("ROLE_SYSTEMADMIN")) {
        final Map<String, Object> config = (Map) this.getServletContext().getAttribute(Constants.CONFIG);
        config.put(Constants.CSS_THEME, theme);
    }

    chain.doFilter(request, response);

    // Reset thread-bound LocaleContext.
    LocaleContextHolder.setLocaleContext(null);
}