Search in sources :

Example 1 with LocalizationContext

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

the class ViewResolverTests method internalResourceViewResolverWithJstlAndContextParam.

@Test
public void internalResourceViewResolverWithJstlAndContextParam() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
    this.sc.addInitParameter(Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/context/WEB-INF/context-messages");
    this.wac.addMessage("code1", locale, "messageX");
    this.wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(this.wac);
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("example1");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("example2");
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, this.request, this.response);
    assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
    assertThat(this.request.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
    assertThat(Config.get(this.request, Config.FMT_LOCALE)).isEqualTo(locale);
    LocalizationContext lc = (LocalizationContext) Config.get(this.request, Config.FMT_LOCALIZATION_CONTEXT);
    assertThat(lc.getResourceBundle().getString("code1")).isEqualTo("message1");
    assertThat(lc.getResourceBundle().getString("code2")).isEqualTo("message2");
}
Also used : Locale(java.util.Locale) LocalizationContext(jakarta.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) TestBean(org.springframework.beans.testfixture.beans.TestBean) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Example 2 with LocalizationContext

use of jakarta.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(jakarta.servlet.jsp.jstl.fmt.LocalizationContext) ResourceBundleMessageSource(org.springframework.context.support.ResourceBundleMessageSource) MessageSource(org.springframework.context.MessageSource)

Example 3 with LocalizationContext

use of jakarta.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, @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);
    }
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) LocalizationContext(jakarta.servlet.jsp.jstl.fmt.LocalizationContext)

Example 4 with LocalizationContext

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

the class ViewResolverTests method internalResourceViewResolverWithJstl.

@Test
public void internalResourceViewResolverWithJstl() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;
    this.wac.addMessage("code1", locale, "messageX");
    this.wac.refresh();
    InternalResourceViewResolver vr = new InternalResourceViewResolver();
    vr.setViewClass(JstlView.class);
    vr.setApplicationContext(this.wac);
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("example1");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("example2");
    this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    this.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    Map<String, Object> model = new HashMap<>();
    TestBean tb = new TestBean();
    model.put("tb", tb);
    view.render(model, this.request, this.response);
    assertThat(tb.equals(this.request.getAttribute("tb"))).as("Correct tb attribute").isTrue();
    assertThat(this.request.getAttribute("rc") == null).as("Correct rc attribute").isTrue();
    assertThat(Config.get(this.request, Config.FMT_LOCALE)).isEqualTo(locale);
    LocalizationContext lc = (LocalizationContext) Config.get(this.request, Config.FMT_LOCALIZATION_CONTEXT);
    assertThat(lc.getResourceBundle().getString("code1")).isEqualTo("messageX");
}
Also used : Locale(java.util.Locale) LocalizationContext(jakarta.servlet.jsp.jstl.fmt.LocalizationContext) HashMap(java.util.HashMap) TestBean(org.springframework.beans.testfixture.beans.TestBean) FixedLocaleResolver(org.springframework.web.servlet.i18n.FixedLocaleResolver) View(org.springframework.web.servlet.View) Test(org.junit.jupiter.api.Test)

Aggregations

LocalizationContext (jakarta.servlet.jsp.jstl.fmt.LocalizationContext)4 Locale (java.util.Locale)3 HashMap (java.util.HashMap)2 TimeZone (java.util.TimeZone)2 Test (org.junit.jupiter.api.Test)2 TestBean (org.springframework.beans.testfixture.beans.TestBean)2 View (org.springframework.web.servlet.View)2 FixedLocaleResolver (org.springframework.web.servlet.i18n.FixedLocaleResolver)2 MessageSource (org.springframework.context.MessageSource)1 ResourceBundleMessageSource (org.springframework.context.support.ResourceBundleMessageSource)1