javax.servlet.jsp.jstl.core.Config Java Examples

The following examples show how to use javax.servlet.jsp.jstl.core.Config. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JstlUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
 * context-param and creates a corresponding child message source,
 * with the provided Spring-defined MessageSource as parent.
 * @param servletContext the ServletContext we're running in
 * (to check JSTL-related context-params in {@code web.xml})
 * @param messageSource the MessageSource to expose, typically
 * the ApplicationContext of the current DispatcherServlet
 * @return the MessageSource to expose to JSTL; first checking the
 * JSTL-defined bundle, then the Spring-defined MessageSource
 * @see org.springframework.context.ApplicationContext
 */
public static MessageSource getJstlAwareMessageSource(
		@Nullable ServletContext servletContext, MessageSource messageSource) {

	if (servletContext != null) {
		String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
		if (jstlInitParam != null) {
			// Create a ResourceBundleMessageSource for the specified resource bundle
			// basename in the JSTL context-param in web.xml, wiring it with the given
			// Spring-defined MessageSource as parent.
			ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
			jstlBundleWrapper.setBasename(jstlInitParam);
			jstlBundleWrapper.setParentMessageSource(messageSource);
			return jstlBundleWrapper;
		}
	}
	return messageSource;
}
 
Example #2
Source File: JstlUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
 * context-param and creates a corresponding child message source,
 * with the provided Spring-defined MessageSource as parent.
 * @param servletContext the ServletContext we're running in
 * (to check JSTL-related context-params in {@code web.xml})
 * @param messageSource the MessageSource to expose, typically
 * the ApplicationContext of the current DispatcherServlet
 * @return the MessageSource to expose to JSTL; first checking the
 * JSTL-defined bundle, then the Spring-defined MessageSource
 * @see org.springframework.context.ApplicationContext
 */
public static MessageSource getJstlAwareMessageSource(
		ServletContext servletContext, MessageSource messageSource) {

	if (servletContext != null) {
		String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
		if (jstlInitParam != null) {
			// Create a ResourceBundleMessageSource for the specified resource bundle
			// basename in the JSTL context-param in web.xml, wiring it with the given
			// Spring-defined MessageSource as parent.
			ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
			jstlBundleWrapper.setBasename(jstlInitParam);
			jstlBundleWrapper.setParentMessageSource(messageSource);
			return jstlBundleWrapper;
		}
	}
	return messageSource;
}
 
Example #3
Source File: LocalizedEnvironmentFilter.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
private void setTimeZone(Context ctx, User user, HttpServletRequest request) {
    RhnTimeZone tz = null;
    if (user != null) {
        tz = user.getTimeZone();
        LOG.debug("Set timezone from the user.");
    }
    if (tz == null) {
        // Use the Appserver timezone if the User doesn't have one.
        String olsonName = TimeZone.getDefault().getID();
        if (LOG.isDebugEnabled()) {
            LOG.debug("olson name [" + olsonName + "]");
        }
        tz = UserManager.getTimeZone(olsonName);
        // if we're still null set it to a default
        if (tz == null) {
            tz = new RhnTimeZone();
            tz.setOlsonName(olsonName);

            LOG.debug("timezone still null");
        }
    }
    ctx.setTimezone(tz.getTimeZone());
    // Set the timezone on the request so that fmt:formatDate
    // can find it
    Config.set(request, Config.FMT_TIME_ZONE, tz.getTimeZone());
}
 
Example #4
Source File: JstlLocalization.java    From vraptor4 with Apache License 2.0 6 votes vote down vote up
/**
 * Looks up a configuration variable in the request, session and application scopes. If none is found, return by
 * {@link ServletContext#getInitParameter(String)} method.
 */
private Object findByKey(String key) {
	Object value = Config.get(request, key);
	if (value != null) {
		return value;
	}

	value = Config.get(request.getSession(createNewSession()), key);
	if (value != null) {
		return value;
	}

	value = Config.get(request.getServletContext(), key);
	if (value != null) {
		return value;
	}

	return request.getServletContext().getInitParameter(key);
}
 
Example #5
Source File: LocalizedEnvironmentFilter.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
private void setTimeZone(Context ctx, User user, HttpServletRequest request) {
    RhnTimeZone tz = null;
    if (user != null) {
        tz = user.getTimeZone();
        LOG.debug("Set timezone from the user.");
    }
    if (tz == null) {
        // Use the Appserver timezone if the User doesn't have one.
        String olsonName = TimeZone.getDefault().getID();
        if (LOG.isDebugEnabled()) {
            LOG.debug("olson name [" + olsonName + "]");
        }
        tz = UserManager.getTimeZone(olsonName);
        // if we're still null set it to a default
        if (tz == null) {
            tz = new RhnTimeZone();
            tz.setOlsonName(olsonName);

            LOG.debug("timezone still null");
        }
    }
    ctx.setTimezone(tz.getTimeZone());
    // Set the timezone on the request so that fmt:formatDate
    // can find it
    Config.set(request, Config.FMT_TIME_ZONE, tz.getTimeZone());
}
 
Example #6
Source File: SessionListener.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/** HttpSessionListener interface */
   @Override
   public void sessionCreated(HttpSessionEvent sessionEvent) {
if (sessionEvent == null) {
    return;
}
HttpSession session = sessionEvent.getSession();
session.setMaxInactiveInterval(Configuration.getAsInt(ConfigurationKeys.INACTIVE_TIME));

//set server default locale for STURTS and JSTL. This value should be overwrite
//LocaleFilter class. But this part code can cope with login.jsp Locale.
if (session != null) {
    String defaults[] = LanguageUtil.getDefaultLangCountry();
    Locale preferredLocale = new Locale(defaults[0] == null ? "" : defaults[0],
	    defaults[1] == null ? "" : defaults[1]);
    session.setAttribute(LocaleFilter.PREFERRED_LOCALE_KEY, preferredLocale);
    Config.set(session, Config.FMT_LOCALE, preferredLocale);
}
   }
 
Example #7
Source File: JstlUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
 * context-param and creates a corresponding child message source,
 * with the provided Spring-defined MessageSource as parent.
 * @param servletContext the ServletContext we're running in
 * (to check JSTL-related context-params in {@code web.xml})
 * @param messageSource the MessageSource to expose, typically
 * the ApplicationContext of the current DispatcherServlet
 * @return the MessageSource to expose to JSTL; first checking the
 * JSTL-defined bundle, then the Spring-defined MessageSource
 * @see org.springframework.context.ApplicationContext
 */
public static MessageSource getJstlAwareMessageSource(
		ServletContext servletContext, MessageSource messageSource) {

	if (servletContext != null) {
		String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
		if (jstlInitParam != null) {
			// Create a ResourceBundleMessageSource for the specified resource bundle
			// basename in the JSTL context-param in web.xml, wiring it with the given
			// Spring-defined MessageSource as parent.
			ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
			jstlBundleWrapper.setBasename(jstlInitParam);
			jstlBundleWrapper.setParentMessageSource(messageSource);
			return jstlBundleWrapper;
		}
	}
	return messageSource;
}
 
Example #8
Source File: JstlUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Checks JSTL's "javax.servlet.jsp.jstl.fmt.localizationContext"
 * context-param and creates a corresponding child message source,
 * with the provided Spring-defined MessageSource as parent.
 * @param servletContext the ServletContext we're running in
 * (to check JSTL-related context-params in {@code web.xml})
 * @param messageSource the MessageSource to expose, typically
 * the ApplicationContext of the current DispatcherServlet
 * @return the MessageSource to expose to JSTL; first checking the
 * JSTL-defined bundle, then the Spring-defined MessageSource
 * @see org.springframework.context.ApplicationContext
 */
public static MessageSource getJstlAwareMessageSource(
		@Nullable ServletContext servletContext, MessageSource messageSource) {

	if (servletContext != null) {
		String jstlInitParam = servletContext.getInitParameter(Config.FMT_LOCALIZATION_CONTEXT);
		if (jstlInitParam != null) {
			// Create a ResourceBundleMessageSource for the specified resource bundle
			// basename in the JSTL context-param in web.xml, wiring it with the given
			// Spring-defined MessageSource as parent.
			ResourceBundleMessageSource jstlBundleWrapper = new ResourceBundleMessageSource();
			jstlBundleWrapper.setBasename(jstlInitParam);
			jstlBundleWrapper.setParentMessageSource(messageSource);
			return jstlBundleWrapper;
		}
	}
	return messageSource;
}
 
Example #9
Source File: RequestContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public static TimeZone getJstlTimeZone(HttpServletRequest request, ServletContext servletContext) {
	Object timeZoneObject = Config.get(request, Config.FMT_TIME_ZONE);
	if (timeZoneObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			timeZoneObject = Config.get(session, Config.FMT_TIME_ZONE);
		}
		if (timeZoneObject == null && servletContext != null) {
			timeZoneObject = Config.get(servletContext, Config.FMT_TIME_ZONE);
		}
	}
	return (timeZoneObject instanceof TimeZone ? (TimeZone) timeZoneObject : null);
}
 
Example #10
Source File: JstlUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Locale getLocale() {
	HttpSession session = this.request.getSession(false);
	if (session != null) {
		Object localeObject = Config.get(session, Config.FMT_LOCALE);
		if (localeObject instanceof Locale) {
			return (Locale) localeObject;
		}
	}
	return RequestContextUtils.getLocale(this.request);
}
 
Example #11
Source File: RequestContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static Locale getJstlLocale(HttpServletRequest request, ServletContext servletContext) {
	Object localeObject = Config.get(request, Config.FMT_LOCALE);
	if (localeObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			localeObject = Config.get(session, Config.FMT_LOCALE);
		}
		if (localeObject == null && servletContext != null) {
			localeObject = Config.get(servletContext, Config.FMT_LOCALE);
		}
	}
	return (localeObject instanceof Locale ? (Locale) localeObject : null);
}
 
Example #12
Source File: LoginServlet.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
   public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// Take care about login.jsp locale. At first, get it from client's browser and check whether LAMS 
// supports it. And if not, uses server's default locale.
Locale browserLocale = request.getLocale();
Locale preferredLocale = LanguageUtil.getSupportedLocaleByNameOrLanguageCode(browserLocale);
Config.set(request, Config.FMT_LOCALE, preferredLocale);

request.getRequestDispatcher("/login.jsp").forward(request, response);
   }
 
Example #13
Source File: JstlUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Exposes JSTL-specific request attributes specifying locale
 * and resource bundle for JSTL's formatting and message tags,
 * using Spring's locale and MessageSource.
 * @param request the current HTTP request
 * @param messageSource the MessageSource to expose,
 * typically the current ApplicationContext (may be {@code null})
 * @see #exposeLocalizationContext(RequestContext)
 */
public static void exposeLocalizationContext(HttpServletRequest request, MessageSource messageSource) {
	Locale jstlLocale = RequestContextUtils.getLocale(request);
	Config.set(request, Config.FMT_LOCALE, jstlLocale);
	TimeZone timeZone = RequestContextUtils.getTimeZone(request);
	if (timeZone != null) {
		Config.set(request, Config.FMT_TIME_ZONE, timeZone);
	}
	if (messageSource != null) {
		LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, request);
		Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
	}
}
 
Example #14
Source File: JstlUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Exposes JSTL-specific request attributes specifying locale
 * and resource bundle for JSTL's formatting and message tags,
 * using Spring's locale and MessageSource.
 * @param requestContext the context for the current HTTP request,
 * including the ApplicationContext to expose as MessageSource
 */
public static void exposeLocalizationContext(RequestContext requestContext) {
	Config.set(requestContext.getRequest(), Config.FMT_LOCALE, requestContext.getLocale());
	TimeZone timeZone = requestContext.getTimeZone();
	if (timeZone != null) {
		Config.set(requestContext.getRequest(), Config.FMT_TIME_ZONE, timeZone);
	}
	MessageSource messageSource = getJstlAwareMessageSource(
			requestContext.getServletContext(), requestContext.getMessageSource());
	LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, requestContext.getRequest());
	Config.set(requestContext.getRequest(), Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
}
 
Example #15
Source File: JstlUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public ResourceBundle getResourceBundle() {
	HttpSession session = this.request.getSession(false);
	if (session != null) {
		Object lcObject = Config.get(session, Config.FMT_LOCALIZATION_CONTEXT);
		if (lcObject instanceof LocalizationContext) {
			ResourceBundle lcBundle = ((LocalizationContext) lcObject).getResourceBundle();
			return new MessageSourceResourceBundle(this.messageSource, getLocale(), lcBundle);
		}
	}
	return new MessageSourceResourceBundle(this.messageSource, getLocale());
}
 
Example #16
Source File: JstlUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Locale getLocale() {
	HttpSession session = this.request.getSession(false);
	if (session != null) {
		Object localeObject = Config.get(session, Config.FMT_LOCALE);
		if (localeObject instanceof Locale) {
			return (Locale) localeObject;
		}
	}
	return RequestContextUtils.getLocale(this.request);
}
 
Example #17
Source File: RequestContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public static Locale getJstlLocale(HttpServletRequest request, ServletContext servletContext) {
	Object localeObject = Config.get(request, Config.FMT_LOCALE);
	if (localeObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			localeObject = Config.get(session, Config.FMT_LOCALE);
		}
		if (localeObject == null && servletContext != null) {
			localeObject = Config.get(servletContext, Config.FMT_LOCALE);
		}
	}
	return (localeObject instanceof Locale ? (Locale) localeObject : null);
}
 
Example #18
Source File: RequestContext.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static TimeZone getJstlTimeZone(HttpServletRequest request, ServletContext servletContext) {
	Object timeZoneObject = Config.get(request, Config.FMT_TIME_ZONE);
	if (timeZoneObject == null) {
		HttpSession session = request.getSession(false);
		if (session != null) {
			timeZoneObject = Config.get(session, Config.FMT_TIME_ZONE);
		}
		if (timeZoneObject == null && servletContext != null) {
			timeZoneObject = Config.get(servletContext, Config.FMT_TIME_ZONE);
		}
	}
	return (timeZoneObject instanceof TimeZone ? (TimeZone) timeZoneObject : null);
}
 
Example #19
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithJstl() throws Exception {
	Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

	MockServletContext sc = new MockServletContext();
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.addMessage("code1", locale, "messageX");
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
	Map model = new HashMap();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

	assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
	LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
	assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
 
Example #20
Source File: ViewResolverTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testInternalResourceViewResolverWithJstlAndContextParam() throws Exception {
	Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

	MockServletContext sc = new MockServletContext();
	sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
	StaticWebApplicationContext wac = new StaticWebApplicationContext();
	wac.setServletContext(sc);
	wac.addMessage("code1", locale, "messageX");
	wac.refresh();
	InternalResourceViewResolver vr = new InternalResourceViewResolver();
	vr.setViewClass(JstlView.class);
	vr.setApplicationContext(wac);

	View view = vr.resolveViewName("example1", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example1", ((JstlView) view).getUrl());

	view = vr.resolveViewName("example2", Locale.getDefault());
	assertEquals("Correct view class", JstlView.class, view.getClass());
	assertEquals("Correct URL", "example2", ((JstlView) view).getUrl());

	MockHttpServletRequest request = new MockHttpServletRequest(sc);
	HttpServletResponse response = new MockHttpServletResponse();
	request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
	request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
	Map model = new HashMap();
	TestBean tb = new TestBean();
	model.put("tb", tb);
	view.render(model, request, response);

	assertTrue("Correct tb attribute", tb.equals(request.getAttribute("tb")));
	assertTrue("Correct rc attribute", request.getAttribute("rc") == null);

	assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
	LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
	assertEquals("message1", lc.getResourceBundle().getString("code1"));
	assertEquals("message2", lc.getResourceBundle().getString("code2"));
}
 
Example #21
Source File: MamuteLocalization.java    From mamute with Apache License 2.0 5 votes vote down vote up
@Override @Produces
public ResourceBundle getBundle(Locale locale) {
	Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, null);
	ResourceBundle customBundle = super.getBundle(locale);
	Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, "mamute-messages");
	ResourceBundle mamuteBundle = super.getBundle(locale);
	
	return new MamuteResourceBundle(customBundle, mamuteBundle);
}
 
Example #22
Source File: LocaleFilter.java    From Openfire with Apache License 2.0 5 votes vote down vote up
/**
 * Ssets the locale context-wide based on a call to {@link JiveGlobals#getLocale()}.
 */
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    final String pathInfo = ((HttpServletRequest)request).getPathInfo();

    if (pathInfo == null) {
        // Note, putting the locale in the application at this point is a little overkill
        // (ie, every user who hits this filter will do this). Eventually, it might make
        // sense to just set the locale in the user's session and if that's done we might
        // want to honor a preference to get the user's locale based on request headers.
        // For now, this is just a convenient place to set the locale globally.
        Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
    }
    else {
        try {
            String[] parts = pathInfo.split("/");
            String pluginName = parts[1];
            ResourceBundle bundle = LocaleUtils.getPluginResourceBundle(pluginName);
            LocalizationContext ctx = new LocalizationContext(bundle, JiveGlobals.getLocale());
            Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, ctx);
        }
        catch (Exception e) {
            // Note, putting the locale in the application at this point is a little overkill
            // (ie, every user who hits this filter will do this). Eventually, it might make
            // sense to just set the locale in the user's session and if that's done we might
            // want to honor a preference to get the user's locale based on request headers.
            // For now, this is just a convenient place to set the locale globally.
            Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
        }
    }
    // Move along:
    chain.doFilter(request, response);
}
 
Example #23
Source File: ChangeLocaleController.java    From hurraa with GNU General Public License v2.0 5 votes vote down vote up
@Path("/change-location/{lenguageName}/{country}")
public void changeLocale(String lenguageName , String country , Result result, HttpSession session , HttpServletRequest request ){
    Locale locale = new Locale( lenguageName , country );
    Config.set( session, Config.FMT_FALLBACK_LOCALE, locale);
    Config.set (session, Config.FMT_LOCALE, locale);
    redirectBack(result , request);
}
 
Example #24
Source File: JstlLocalizationTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetLocaleFromRequestFirst() {
	when(request.getAttribute(FMT_LOCALE + ".request")).thenReturn(PT_BR);
	when(session.getAttribute(FMT_LOCALE + ".session")).thenReturn(Locale.ENGLISH);
	when(servletContext.getAttribute(FMT_LOCALE + ".application")).thenReturn(Locale.ENGLISH);
	when(servletContext.getInitParameter(Config.FMT_LOCALE)).thenReturn(Locale.ENGLISH.toString());
	when(request.getLocale()).thenReturn(Locale.ENGLISH);
	assumeThat(Locale.getDefault(), is(Locale.ENGLISH));

	assertThat(localization.getLocale(), equalTo(PT_BR));
}
 
Example #25
Source File: JstlLocalizationTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetLocaleFromSessionWhenNotFoundInRequest() {
	when(session.getAttribute(FMT_LOCALE + ".session")).thenReturn(PT_BR);
	when(servletContext.getAttribute(FMT_LOCALE + ".application")).thenReturn(Locale.ENGLISH);
	when(servletContext.getInitParameter(Config.FMT_LOCALE)).thenReturn(Locale.ENGLISH.toString());
	when(request.getLocale()).thenReturn(Locale.ENGLISH);
	assumeThat(Locale.getDefault(), is(Locale.ENGLISH));

	assertThat(localization.getLocale(), equalTo(PT_BR));
}
 
Example #26
Source File: JstlLocalizationTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetLocaleFromServletContextWhenNotFoundInSession() {
	when(servletContext.getAttribute(FMT_LOCALE + ".application")).thenReturn(PT_BR);
	when(servletContext.getInitParameter(Config.FMT_LOCALE)).thenReturn(Locale.ENGLISH.toString());
	when(request.getLocale()).thenReturn(Locale.ENGLISH);
	assumeThat(Locale.getDefault(), is(Locale.ENGLISH));

	assertThat(localization.getLocale(), equalTo(PT_BR));
}
 
Example #27
Source File: JstlLocalizationTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetLocaleFromInitParameterWhenNotFoundInServletContext() {
	when(servletContext.getInitParameter(Config.FMT_LOCALE)).thenReturn(PT_BR.toString());
	when(request.getLocale()).thenReturn(Locale.ENGLISH);
	assumeThat(Locale.getDefault(), is(Locale.ENGLISH));

	assertThat(localization.getLocale(), equalTo(PT_BR));
}
 
Example #28
Source File: JstlLocalizationTest.java    From vraptor4 with Apache License 2.0 5 votes vote down vote up
@Test
public void parseLocaleWithLanguageAndCountryAndVariant() {
	when(servletContext.getInitParameter(Config.FMT_LOCALE)).thenReturn("pt_BR_POSIX");
	assertThat(localization.getLocale().getLanguage(), equalTo("pt"));
	assertThat(localization.getLocale().getCountry(), equalTo("BR"));
	assertThat(localization.getLocale().getVariant(), equalTo("POSIX"));
}
 
Example #29
Source File: JstlUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Exposes JSTL-specific request attributes specifying locale
 * and resource bundle for JSTL's formatting and message tags,
 * using Spring's locale and MessageSource.
 * @param requestContext the context for the current HTTP request,
 * including the ApplicationContext to expose as MessageSource
 */
public static void exposeLocalizationContext(RequestContext requestContext) {
	Config.set(requestContext.getRequest(), Config.FMT_LOCALE, requestContext.getLocale());
	TimeZone timeZone = requestContext.getTimeZone();
	if (timeZone != null) {
		Config.set(requestContext.getRequest(), Config.FMT_TIME_ZONE, timeZone);
	}
	MessageSource messageSource = getJstlAwareMessageSource(
			requestContext.getServletContext(), requestContext.getMessageSource());
	LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, requestContext.getRequest());
	Config.set(requestContext.getRequest(), Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
}
 
Example #30
Source File: JstlUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Exposes JSTL-specific request attributes specifying locale
 * and resource bundle for JSTL's formatting and message tags,
 * using Spring's locale and MessageSource.
 * @param request the current HTTP request
 * @param messageSource the MessageSource to expose,
 * typically the current ApplicationContext (may be {@code null})
 * @see #exposeLocalizationContext(RequestContext)
 */
public static void exposeLocalizationContext(HttpServletRequest request, @Nullable MessageSource messageSource) {
	Locale jstlLocale = RequestContextUtils.getLocale(request);
	Config.set(request, Config.FMT_LOCALE, jstlLocale);
	TimeZone timeZone = RequestContextUtils.getTimeZone(request);
	if (timeZone != null) {
		Config.set(request, Config.FMT_TIME_ZONE, timeZone);
	}
	if (messageSource != null) {
		LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, request);
		Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
	}
}