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"));
}
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);
}
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);
}
}
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);
}
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"));
}
Aggregations