Example #1
0
  /**
   * This code is currently common to all {@link ViewHandlingStrategy} instances.
   *
   * @see ViewHandler#calculateLocale(javax.faces.context.FacesContext)
   */
  public Locale calculateLocale(FacesContext context) {

    Util.notNull("context", context);

    Locale result = null;
    // determine the locales that are acceptable to the client based on the
    // Accept-Language header and the find the best match among the
    // supported locales specified by the client.
    Iterator<Locale> locales = context.getExternalContext().getRequestLocales();
    while (locales.hasNext()) {
      Locale perf = locales.next();
      result = findMatch(context, perf);
      if (result != null) {
        break;
      }
    }
    // no match is found.
    if (result == null) {
      if (context.getApplication().getDefaultLocale() == null) {
        result = Locale.getDefault();
      } else {
        result = context.getApplication().getDefaultLocale();
      }
    }
    return result;
  }
Example #2
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;
  }