You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by dl...@apache.org on 2001/10/06 00:21:49 UTC

cvs commit: jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/localization TurbineLocalizationService.java

dlr         01/10/05 15:21:49

  Modified:    src/services/java/org/apache/fulcrum/localization
                        TurbineLocalizationService.java
  Log:
  o Added defaultLocale member to avoid having to instantiate it when it
  is needed.  It could subsume defaultLanguage and defaultCountry.
  
  o Renamed guessBundle() to findClosestBundle() and refactored to make
  it try harder to produce a ResourceBundle.
  
  Revision  Changes    Path
  1.14      +62 -22    jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/localization/TurbineLocalizationService.java
  
  Index: TurbineLocalizationService.java
  ===================================================================
  RCS file: /home/cvs/jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/localization/TurbineLocalizationService.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -u -r1.13 -r1.14
  --- TurbineLocalizationService.java	2001/09/26 01:30:27	1.13
  +++ TurbineLocalizationService.java	2001/10/05 22:21:49	1.14
  @@ -56,6 +56,7 @@
   
   import java.util.HashMap;
   import java.util.Locale;
  +import java.util.Map;
   import java.util.MissingResourceException;
   import java.util.ResourceBundle;
   import java.util.StringTokenizer;
  @@ -92,7 +93,7 @@
    * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    * @author <a href="mailto:novalidemail@foo.com">Frank Y. Kim</a>
    * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
  - * @version $Id: TurbineLocalizationService.java,v 1.13 2001/09/26 01:30:27 dlr Exp $
  + * @version $Id: TurbineLocalizationService.java,v 1.14 2001/10/05 22:21:49 dlr Exp $
    */
   public class TurbineLocalizationService
       extends BaseService
  @@ -107,6 +108,12 @@
       /** The name of the default bundle to use. */
       private String defaultBundle = null;
   
  +    /**
  +     * The name of the default locale to use (includes language and
  +     * country).
  +     */
  +    private Locale defaultLocale = null;
  +
       /** The name of the default language to use. */
       private String defaultLanguage = null;
   
  @@ -140,6 +147,7 @@
           defaultCountry = getConfiguration()
               .getString("locale.default.country",
                          jvmDefault.getCountry()).trim();
  +        defaultLocale = new Locale(defaultLanguage, defaultCountry);
           setInit(true);
       }
   
  @@ -301,7 +309,7 @@
               }
               catch (MissingResourceException e)
               {
  -                rb = guessBundle(bundleName, locale, bundlesByLocale);
  +                rb = findClosestBundle(bundleName, locale, bundlesByLocale);
                   if (rb == null)
                   {
                       throw (MissingResourceException) e.fillInStackTrace();
  @@ -322,16 +330,22 @@
       }
   
       /**
  -     * Some browsers send a HTTP Accept-Language header with a value
  -     * of only the language to use (i.e. "Accept-Language: en"), and
  -     * neglect to include a country.  When there is no bundle for the
  -     * requested language, this method can be called to try the
  -     * default country (checking internally to assure the requested
  -     * language matches the default to avoid disconnects between
  -     * language and country).
  +     * <p>Retrieves the bundle most closely matching first against the
  +     * supplied inputs, then against the defaults.</p>
  +     *
  +     * <p>Use case: some clients send a HTTP Accept-Language header
  +     * with a value of only the language to use
  +     * (i.e. "Accept-Language: en"), and neglect to include a country.
  +     * When there is no bundle for the requested language, this method
  +     * can be called to try the default country (checking internally
  +     * to assure the requested criteria matches the default to avoid
  +     * disconnects between language and country).</p>
  +     *
  +     * <p>Since we're really just guessing at possible bundles to use,
  +     * we don't ever throw <code>MissingResourceException</code>.</p>
        */
  -    private final ResourceBundle guessBundle(String bundleName, Locale locale,
  -                                             HashMap bundlesByLocale)
  +    private ResourceBundle findClosestBundle(String bundleName, Locale locale,
  +                                             Map bundlesByLocale)
       {
           ResourceBundle rb = null;
           if ( !StringUtils.isValid(locale.getCountry()) &&
  @@ -347,22 +361,48 @@
               rb = (ResourceBundle) bundlesByLocale.get(withDefaultCountry);
               if (rb == null)
               {
  -                try
  -                {
  -                    rb = ResourceBundle.getBundle(bundleName,
  -                                                  withDefaultCountry);
  -                }
  -                catch (MissingResourceException ignored)
  -                {
  -                    // Since we're really just guessing, we can't in
  -                    // good conscience throw an exception here.
  -                }
  +                rb = getBundleIgnoreException(bundleName, withDefaultCountry);
  +            }
  +        }
  +        else if ( !StringUtils.isValid(locale.getLanguage()) &&
  +                  defaultCountry.equals(locale.getCountry()) )
  +        {
  +            Locale withDefaultLanguage = new Locale(defaultLanguage, 
  +                                                    locale.getCountry());
  +            rb = (ResourceBundle) bundlesByLocale.get(withDefaultLanguage);
  +            if (rb == null)
  +            {
  +                rb = getBundleIgnoreException(bundleName, withDefaultLanguage);
               }
           }
  +
  +        if (rb == null && !defaultLocale.equals(locale))
  +        {
  +            rb = getBundleIgnoreException(bundleName, defaultLocale);
  +        }
  +
           return rb;
       }
   
       /**
  +     * Retrieves the bundle using the
  +     * <code>ResourceBundle.getBundle(String, Locale)</code> method,
  +     * ignoring <code>MissingResourceException</code>.
  +     */
  +    private final ResourceBundle getBundleIgnoreException(String bundleName,
  +                                                          Locale locale)
  +    {
  +        try
  +        {
  +            return ResourceBundle.getBundle(bundleName, locale);
  +        }
  +        catch (MissingResourceException ignored)
  +        {
  +            return null;
  +        }
  +    }
  +
  +    /**
        * This method sets the name of the defaultBundle.
        *
        * @param defaultBundle Name of default bundle.
  @@ -407,6 +447,6 @@
           }
   
           // Couldn't parse locale.
  -        return new Locale(defaultLanguage, defaultCountry);
  +        return defaultLocale;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org


Re: cvs commit: jakarta-turbine-fulcrum/src/services/java/org/apache/fulcrum/localization TurbineLocalizationService.java

Posted by Jon Stevens <jo...@latchkey.com>.
on 10/5/01 3:21 PM, "dlr@apache.org" <dl...@apache.org> wrote:

> +    private final ResourceBundle getBundleIgnoreException(String bundleName,

What is the point of declaring a private final method?

:-)

Hotspot will inline the method regardless of final.

-jon


---------------------------------------------------------------------
To unsubscribe, e-mail: turbine-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: turbine-dev-help@jakarta.apache.org