Search in sources :

Example 1 with LocalizationContext

use of javax.servlet.jsp.jstl.fmt.LocalizationContext in project spring-framework by spring-projects.

the class ViewResolverTests method testInternalResourceViewResolverWithJstlAndContextParam.

@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"));
}
Also used : Locale(java.util.Locale) LocalizationContext(javax.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) TestBean(org.springframework.tests.sample.beans.TestBean) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 2 with LocalizationContext

use of javax.servlet.jsp.jstl.fmt.LocalizationContext in project Openfire by igniterealtime.

the class LocaleFilter method doFilter.

/**
     * 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);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) LocalizationContext(javax.servlet.jsp.jstl.fmt.LocalizationContext) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException)

Example 3 with LocalizationContext

use of javax.servlet.jsp.jstl.fmt.LocalizationContext in project spring-framework by spring-projects.

the class JstlUtils method exposeLocalizationContext.

/**
	 * 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);
    }
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) LocalizationContext(javax.servlet.jsp.jstl.fmt.LocalizationContext)

Example 4 with LocalizationContext

use of javax.servlet.jsp.jstl.fmt.LocalizationContext in project spring-framework by spring-projects.

the class JstlUtils method exposeLocalizationContext.

/**
	 * 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);
}
Also used : TimeZone(java.util.TimeZone) LocalizationContext(javax.servlet.jsp.jstl.fmt.LocalizationContext) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) MessageSource(org.springframework.context.MessageSource)

Example 5 with LocalizationContext

use of javax.servlet.jsp.jstl.fmt.LocalizationContext in project spring-framework by spring-projects.

the class ViewResolverTests method testInternalResourceViewResolverWithJstl.

@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"));
}
Also used : Locale(java.util.Locale) LocalizationContext(javax.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) View(org.springframework.web.servlet.View) MockServletContext(org.springframework.mock.web.test.MockServletContext) TestBean(org.springframework.tests.sample.beans.TestBean) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

LocalizationContext (javax.servlet.jsp.jstl.fmt.LocalizationContext)5 Locale (java.util.Locale)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TimeZone (java.util.TimeZone)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 Test (org.junit.Test)2 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)2 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)2 MockServletContext (org.springframework.mock.web.test.MockServletContext)2 TestBean (org.springframework.tests.sample.beans.TestBean)2 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)2 View (org.springframework.web.servlet.View)2 FixedLocaleResolver (org.springframework.web.servlet.i18n.FixedLocaleResolver)2 IOException (java.io.IOException)1 ResourceBundle (java.util.ResourceBundle)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MessageSource (org.springframework.context.MessageSource)1 ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)1