Пример #1
0
  /**
   * Gets the resource bundle with the given base name, whose locale is determined as follows:
   *
   * <p>Check if a match exists between the ordered set of preferred locales and the available
   * locales, for the given base name. The set of preferred locales consists of a single locale (if
   * the <tt>javax.servlet.jsp.jstl.fmt.locale</tt> configuration setting is present) or is equal to
   * the client's preferred locales determined from the client's browser settings.
   *
   * <p>If no match was found in the previous step, check if a match exists between the fallback
   * locale (given by the <tt>javax.servlet.jsp.jstl.fmt.fallbackLocale</tt> configuration setting)
   * and the available locales, for the given base name.
   *
   * @param pageContext Page in which the resource bundle with the given base name is requested
   * @param basename Resource bundle base name
   * @return Localization context containing the resource bundle with the given base name and the
   *     locale that led to the resource bundle match, or the empty localization context if no
   *     resource bundle match was found
   */
  public static LocalizationContext getLocalizationContext(PageContext pc, String basename) {
    LocalizationContext locCtxt = null;
    ResourceBundle bundle = null;

    if ((basename == null) || basename.equals("")) {
      return new LocalizationContext();
    }

    // Try preferred locales
    Locale pref = SetLocaleSupport.getLocale(pc, Config.FMT_LOCALE);
    if (pref != null) {
      // Preferred locale is application-based
      bundle = findMatch(basename, pref);
      if (bundle != null) {
        locCtxt = new LocalizationContext(bundle, pref);
      }
    } else {
      // Preferred locales are browser-based
      locCtxt = findMatch(pc, basename);
    }

    if (locCtxt == null) {
      // No match found with preferred locales, try using fallback locale
      pref = SetLocaleSupport.getLocale(pc, Config.FMT_FALLBACK_LOCALE);
      if (pref != null) {
        bundle = findMatch(basename, pref);
        if (bundle != null) {
          locCtxt = new LocalizationContext(bundle, pref);
        }
      }
    }

    if (locCtxt == null) {
      // try using the root resource bundle with the given basename
      try {
        bundle =
            ResourceBundle.getBundle(
                basename, EMPTY_LOCALE, Thread.currentThread().getContextClassLoader());
        if (bundle != null) {
          locCtxt = new LocalizationContext(bundle, null);
        }
      } catch (MissingResourceException mre) {
        // do nothing
      }
    }

    if (locCtxt != null) {
      // set response locale
      if (locCtxt.getLocale() != null) {
        SetLocaleSupport.setResponseLocale(pc, locCtxt.getLocale());
      }
    } else {
      // create empty localization context
      locCtxt = new LocalizationContext();
    }

    return locCtxt;
  }
Пример #2
0
  /** Sets the LocalizationContext used to localize labels. */
  public void setBundle(LocalizationContext bundle) {

    super.setBundle(bundle.getResourceBundle());
  }