Java 类javax.servlet.jsp.jstl.fmt.LocalizationContext 实例源码

项目:Portofino    文件:I18nUtils.java   
public static void setupTextProvider(ServletContext servletContext, ServletRequest request) {
    Locale locale = request.getLocale();
    ResourceBundleManager resourceBundleManager =
            (ResourceBundleManager) servletContext.getAttribute(BaseModule.RESOURCE_BUNDLE_MANAGER);
    ResourceBundle portofinoResourceBundle = resourceBundleManager.getBundle(locale);

    LocalizationContext localizationContext =
            new LocalizationContext(portofinoResourceBundle, locale);
    request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".request", localizationContext);

    //Setup Elements I18n
    ResourceBundle elementsResourceBundle =
            ResourceBundle.getBundle(SimpleTextProvider.DEFAULT_MESSAGE_RESOURCE, locale);

    TextProvider textProvider =
            new MultipleTextProvider(
                    portofinoResourceBundle, elementsResourceBundle);
    ElementsThreadLocals.setTextProvider(textProvider);
}
项目:manydesigns.cn    文件:I18nFilter.java   
@Override
public void doFilter(
        ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    //I18n
    Locale locale = request.getLocale();
    ResourceBundleManager resourceBundleManager =
            (ResourceBundleManager) servletContext.getAttribute(BaseModule.RESOURCE_BUNDLE_MANAGER);
    ResourceBundle portofinoResourceBundle = resourceBundleManager.getBundle(locale);

    LocalizationContext localizationContext =
            new LocalizationContext(portofinoResourceBundle, locale);
    request.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".request", localizationContext);

    //Setup Elements I18n
    ResourceBundle elementsResourceBundle =
            ResourceBundle.getBundle(SimpleTextProvider.DEFAULT_MESSAGE_RESOURCE, locale);

    TextProvider textProvider =
            new MultipleTextProvider(
                    portofinoResourceBundle, elementsResourceBundle);
    ElementsThreadLocals.setTextProvider(textProvider);
    filterChain.doFilter(request, response);
}
项目:stripes-stuff    文件:JstlBundleInterceptor.java   
/**
    * Sets the Stripes error and field resource bundles in the request, if their names have been configured.
    */
@Override
protected void setOtherResourceBundles(HttpServletRequest request)
{
    Locale locale = request.getLocale();

       if (errorBundleName != null)
       {
           ResourceBundle errorBundle = getLocalizationBundleFactory().getErrorMessageBundle(locale);
           Config.set(request, errorBundleName, new LocalizationContext(errorBundle, locale));
           LOGGER.debug("Loaded Stripes error message bundle ", errorBundle, " as ", errorBundleName);
       }

       if (fieldBundleName != null)
       {
           ResourceBundle fieldBundle = getLocalizationBundleFactory().getFormFieldBundle(locale);
           Config.set(request, fieldBundleName, new LocalizationContext(fieldBundle, locale));
           LOGGER.debug("Loaded Stripes field name bundle ", fieldBundle, " as ", fieldBundleName);
       }
}
项目:vraptor4    文件:JstlLocalization.java   
private ResourceBundle extractUnsafeBundle(Object bundle, Locale locale) {
    if (bundle instanceof String || bundle == null) {
        String baseName = firstNonNull((String) bundle, DEFAULT_BUNDLE_NAME);

        try {
            return ResourceBundle.getBundle(baseName, locale);
        } catch (MissingResourceException e) {
            logger.warn("Couldn't find message bundle for base name '{}' and locale '{}', creating an empty one", baseName, locale);
            return new EmptyBundle();
        }
    }

    if (bundle instanceof LocalizationContext) {
        return ((LocalizationContext) bundle).getResourceBundle();
    }

    logger.warn("Can't handle bundle '{}'. Please report this bug. Using an empty bundle", bundle);
    return new EmptyBundle();
}
项目:vraptor4    文件:JstlLocalizationTest.java   
@Before
public void setUp() {
    request = mock(HttpServletRequest.class);
    servletContext = mock(ServletContext.class);
    session = mock(HttpSession.class);

    localization = new JstlLocalization(request);

    ResourceBundle bundle = new ListResourceBundle() {
        @Override
        protected Object[][] getContents() {
            return new Object[][] { { "my.key", "abc" } };
        }
    };

    LocalizationContext context = new LocalizationContext(bundle);
    when(request.getAttribute(FMT_LOCALIZATION_CONTEXT + ".request")).thenReturn(context);

    when(request.getSession(false)).thenReturn(session);
    when(request.getServletContext()).thenReturn(servletContext);
}
项目:ironsites    文件:I18nHelperTag.java   
public int doEndTag() throws JspException {
    try {
        init();

        ValueMap valueMap = request.getResource().adaptTo(ValueMap.class);
        I18nResourceBundle bundle = new I18nResourceBundle(valueMap,
                getResourceBundle(getBaseName(), request));

        // make this resource bundle & i18n available as ValueMap for TEI
        pageContext.setAttribute(getMessagesName(),
                bundle.adaptTo(ValueMap.class), 
                PageContext.PAGE_SCOPE);
        pageContext.setAttribute(getI18nName(), 
                new I18n(bundle), 
                PageContext.PAGE_SCOPE);

        // make this resource bundle available for fmt functions
        Config.set(pageContext, "javax.servlet.jsp.jstl.fmt.localizationContext", new LocalizationContext(bundle, getLocale(request)), 1);
    } catch(Exception e) {
        LOG.error(e.getMessage());
        throw new JspException(e);
    }
    return EVAL_PAGE;
}
项目:spring4-understanding    文件:JstlUtils.java   
/**
 * 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);
    }
}
项目:spring4-understanding    文件:JstlUtils.java   
/**
 * 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);
}
项目:spring4-understanding    文件:JstlUtils.java   
@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());
}
项目:spring4-understanding    文件:ViewResolverTests.java   
@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"));
}
项目:spring4-understanding    文件:ViewResolverTests.java   
@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"));
}
项目:Openfire    文件:LocaleFilter.java   
/**
 * 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);
}
项目:class-guard    文件:JstlUtils.java   
/**
 * 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());
    MessageSource messageSource = getJstlAwareMessageSource(
            requestContext.getServletContext(), requestContext.getMessageSource());
    LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, requestContext.getRequest());
    Config.set(requestContext.getRequest(), Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
}
项目:class-guard    文件:JstlUtils.java   
@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());
}
项目:class-guard    文件:ViewResolverTests.java   
@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"));
}
项目:class-guard    文件:ViewResolverTests.java   
@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"));
}
项目:class-guard    文件:TilesViewTests.java   
@Test
public void tilesJstlView() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

    StaticWebApplicationContext wac = prepareWebApplicationContext();

    InternalResourceViewResolver irvr = new InternalResourceViewResolver();
    irvr.setApplicationContext(wac);
    irvr.setViewClass(TilesJstlView.class);
    View view = irvr.resolveViewName("testTile", new Locale("nl", ""));

    MockHttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));
    wac.addMessage("code1", locale, "messageX");
    view.render(new HashMap<String, Object>(), request, response);

    assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
    ComponentContext cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
    assertNotNull(cc);
    PathAttribute attr = (PathAttribute) cc.getAttribute("content");
    assertEquals("/WEB-INF/jsp/content.jsp", attr.getValue());

    assertEquals(locale, Config.get(request, Config.FMT_LOCALE));
    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("messageX", lc.getResourceBundle().getString("code1"));
}
项目:class-guard    文件:TilesViewTests.java   
@Test
public void tilesJstlViewWithContextParam() throws Exception {
    Locale locale = !Locale.GERMAN.equals(Locale.getDefault()) ? Locale.GERMAN : Locale.FRENCH;

    StaticWebApplicationContext wac = prepareWebApplicationContext();
    ((MockServletContext) wac.getServletContext()).addInitParameter(
            Config.FMT_LOCALIZATION_CONTEXT, "org/springframework/web/servlet/view/tiles/context-messages");

    InternalResourceViewResolver irvr = new InternalResourceViewResolver();
    irvr.setApplicationContext(wac);
    irvr.setViewClass(TilesJstlView.class);
    View view = irvr.resolveViewName("testTile", new Locale("nl", ""));

    MockHttpServletRequest request = new MockHttpServletRequest(wac.getServletContext());
    MockHttpServletResponse response = new MockHttpServletResponse();
    wac.addMessage("code1", locale, "messageX");
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(locale));

    view.render(new HashMap<String, Object>(), request, response);
    assertEquals("/WEB-INF/jsp/layout.jsp", response.getForwardedUrl());
    ComponentContext cc = (ComponentContext) request.getAttribute(ComponentConstants.COMPONENT_CONTEXT);
    assertNotNull(cc);
    PathAttribute attr = (PathAttribute) cc.getAttribute("content");
    assertEquals("/WEB-INF/jsp/content.jsp", attr.getValue());

    LocalizationContext lc = (LocalizationContext) Config.get(request, Config.FMT_LOCALIZATION_CONTEXT);
    assertEquals("message1", lc.getResourceBundle().getString("code1"));
    assertEquals("message2", lc.getResourceBundle().getString("code2"));
}
项目:stratos    文件:TooltipsGenerator.java   
/**
 * This method is used to get the resource bundle define in a jsp page.
 * @return return the resource bundle
 */
public ResourceBundle getCommonResourceBundleForJspPage() {
    Tag t = findAncestorWithClass(this, BundleSupport.class);
    LocalizationContext localizationContext;
    if (t != null) {
        // use resource bundle from parent <bundle> tag
        BundleSupport parent = (BundleSupport) t;
        localizationContext = parent.getLocalizationContext();

 }  else {
        localizationContext = BundleSupport.getLocalizationContext(pageContext);
    }
    return localizationContext.getResourceBundle();
}
项目:g3server    文件:LocaleFilter.java   
/**
 * Ssets the locale context-wide based on a call to {@link JiveGlobals#getLocale()}.
 */
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);
}
项目:openfire    文件:LocaleFilter.java   
/**
 * Ssets the locale context-wide based on a call to {@link JiveGlobals#getLocale()}.
 */
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);
}
项目:stripes-stuff    文件:JstlBundleInterceptor.java   
/**
    * Sets the message resource bundle in the request using the JSTL's mechanism.
    */
@Override
   protected void setMessageResourceBundle(HttpServletRequest request, ResourceBundle bundle)
{
    Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, new LocalizationContext(bundle, request.getLocale()));
       LOGGER.debug("Enabled JSTL localization using: ", bundle);
       LOGGER.debug("Loaded resource bundle ", bundle, " as default bundle");
   }
项目:dandelion-datatables    文件:JstlMessageResolver.java   
public String getResource(String messageKey, String defaultValue, Object... params) {

      PageContext pageContext = (PageContext) params[0];
      String message = null;
      ResourceBundle bundle = null;

      LocalizationContext localizationContext = BundleSupport.getLocalizationContext(pageContext);

      if (localizationContext != null) {
         bundle = localizationContext.getResourceBundle();
      }

      if (bundle != null) {

         if (messageKey == null || StringUtils.isBlank(messageKey) && StringUtils.isNotBlank(defaultValue)) {
            message = StringUtils.capitalize(defaultValue);
         }
         else {
            try {
               message = bundle.getString(messageKey);
            }
            catch (MissingResourceException e) {
               logger.warn("No message found with the key The message key {} and locale {}.", messageKey,
                     localizationContext.getLocale());
               message = UNDEFINED_KEY + messageKey + UNDEFINED_KEY;
            }
         }
      }
      else {
         logger.warn("The bundle hasn't been retrieved. Please check your i18n configuration.");
         message = UNDEFINED_KEY + messageKey + UNDEFINED_KEY;
      }

      return message;
   }
项目:dandelion-datatables    文件:JstlMessageResolverTest.java   
@Test
public void should_return_error_message_when_the_key_is_undefined() throws UnsupportedEncodingException, IOException{
    // Setup
    InputStream stream = getClass().getClassLoader().getResourceAsStream("com/github/dandelion/datatables/jsp/i18n/datatables_en.properties");
    ResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
    pageContext = new MockPageContext();
    pageContext.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".page", new LocalizationContext(bundle, new Locale("en", "US")));
    JstlMessageResolver messageResolver = new JstlMessageResolver(request);

    // Test
    String message = messageResolver.getResource("undefinedKey", "default", pageContext);
    assertThat(message).isEqualTo(MessageResolver.UNDEFINED_KEY + "undefinedKey" + MessageResolver.UNDEFINED_KEY);
}
项目:dandelion-datatables    文件:JstlMessageResolverTest.java   
@Test
public void should_return_blank_when_the_key_is_defined_but_message_is_blank() throws UnsupportedEncodingException, IOException{
    // Setup
    InputStream stream = getClass().getClassLoader().getResourceAsStream("com/github/dandelion/datatables/jsp/i18n/datatables_en.properties");
    ResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
    pageContext = new MockPageContext();
    pageContext.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".page", new LocalizationContext(bundle, new Locale("en", "US")));
    JstlMessageResolver messageResolver = new JstlMessageResolver(request);

    // Test
    String message = messageResolver.getResource("global.blank.value", "default", pageContext);
    assertThat(message).isEqualTo("");
}
项目:dandelion-datatables    文件:JstlMessageResolverTest.java   
@Test
public void should_return_the_capitalized_default_message_when_the_key_is_null() throws UnsupportedEncodingException, IOException{
    // Setup
    InputStream stream = getClass().getClassLoader().getResourceAsStream("com/github/dandelion/datatables/jsp/i18n/datatables_en.properties");
    ResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
    pageContext = new MockPageContext();
    pageContext.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".page", new LocalizationContext(bundle, new Locale("en", "US")));
    JstlMessageResolver messageResolver = new JstlMessageResolver(request);

    // Test
    String message = messageResolver.getResource(null, "default", pageContext);
    assertThat(message).isEqualTo("Default");
}
项目:dandelion-datatables    文件:JstlMessageResolverTest.java   
@Test
public void should_return_the_message_when_the_key_is_present_in_the_bundle() throws UnsupportedEncodingException, IOException{
    // Setup
    InputStream stream = getClass().getClassLoader().getResourceAsStream("com/github/dandelion/datatables/jsp/i18n/datatables_en.properties");
    ResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
    pageContext = new MockPageContext();
    pageContext.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".page", new LocalizationContext(bundle, new Locale("en", "US")));
    JstlMessageResolver messageResolver = new JstlMessageResolver(request);

    // Test
    String message = messageResolver.getResource("global.msg.info", "default", pageContext);
    assertThat(message).isEqualTo("My infos");
}
项目:dandelion-datatables    文件:JstlMessageResolverTest.java   
@Test
public void should_return_the_capitalized_default_message_if_the_key_is_null() throws UnsupportedEncodingException, IOException{
    // Setup
    InputStream stream = getClass().getClassLoader().getResourceAsStream("com/github/dandelion/datatables/jsp/i18n/datatables_en.properties");
    ResourceBundle bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
    pageContext = new MockPageContext();
    pageContext.setAttribute(Config.FMT_LOCALIZATION_CONTEXT + ".page", new LocalizationContext(bundle, new Locale("en", "US")));
    JstlMessageResolver messageResolver = new JstlMessageResolver(request);

    // Test
    String message = messageResolver.getResource(null, "default", pageContext);
    assertThat(message).isEqualTo("Default");
}
项目:openfire-bespoke    文件:LocaleFilter.java   
/**
 * Ssets the locale context-wide based on a call to {@link JiveGlobals#getLocale()}.
 */
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);
}
项目:jsi18nresourcebundle    文件:JSConstantObjectGenerator.java   
public int doStartTag() throws JspException
{
   String renderedJsObjectName = jsObjectName == null?"constants":jsObjectName;

   JspWriter out = pageContext.getOut();
   try
   {
      LocalizationContext locCtxt = BundleSupport.getLocalizationContext(pageContext, resourceBundleBaseName);

      ResourceBundle resourceBundle = locCtxt.getResourceBundle();

      out.write("<script type='text/javascript'>");
      if (resourceBundle != null)
      {
         Enumeration<String> keys = resourceBundle.getKeys();

         out.write("var " + renderedJsObjectName + " = {");
         while (keys.hasMoreElements())
         {
            String key = keys.nextElement();
            String value = resourceBundle.getString(key);

            String jsKey = removePeriodFromKey(key);
            String jsValue = StringEscapeUtils.escapeJavaScript(value);

            out.write(jsKey);
            out.write(":");
            out.write("'");
            out.write(jsValue);
            out.write("'");
            if (keys.hasMoreElements())
               out.write(",");
         }
         out.write("};");
      }
      out.write("</script>");
   }
   catch (java.io.IOException e)
   {
      throw new JspException("IOException while writing to client: " + e);
   }

   return SKIP_BODY;
}
项目:class-guard    文件:JstlUtils.java   
/**
 * 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);
    if (messageSource != null) {
        LocalizationContext jstlContext = new SpringLocalizationContext(messageSource, request);
        Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, jstlContext);
    }
}