Exemplo n.º 1
0
  /**
   * Attempts to find a matching locale based on <code>pref</code> and list of supported locales,
   * using the matching algorithm as described in JSTL 8.3.2.
   *
   * @param context the <code>FacesContext</code> for the current request
   * @param pref the preferred locale
   * @return the Locale based on pref and the matching alogritm specified in JSTL 8.3.2
   */
  protected Locale findMatch(FacesContext context, Locale pref) {

    Locale result = null;
    Iterator<Locale> it = context.getApplication().getSupportedLocales();
    while (it.hasNext()) {
      Locale supportedLocale = it.next();

      if (pref.equals(supportedLocale)) {
        // exact match
        result = supportedLocale;
        break;
      } else {
        // Make sure the preferred locale doesn't have country
        // set, when doing a language match, For ex., if the
        // preferred locale is "en-US", if one of supported
        // locales is "en-UK", even though its language matches
        // that of the preferred locale, we must ignore it.
        if (pref.getLanguage().equals(supportedLocale.getLanguage())
            && supportedLocale.getCountry().length() == 0) {
          result = supportedLocale;
        }
      }
    }
    // if it's not in the supported locales,
    if (null == result) {
      Locale defaultLocale = context.getApplication().getDefaultLocale();
      if (defaultLocale != null) {
        if (pref.equals(defaultLocale)) {
          // exact match
          result = defaultLocale;
        } else {
          // Make sure the preferred locale doesn't have country
          // set, when doing a language match, For ex., if the
          // preferred locale is "en-US", if one of supported
          // locales is "en-UK", even though its language matches
          // that of the preferred locale, we must ignore it.
          if (pref.getLanguage().equals(defaultLocale.getLanguage())
              && defaultLocale.getCountry().length() == 0) {
            result = defaultLocale;
          }
        }
      }
    }

    return result;
  }