dlr         01/10/20 14:20:09

  Modified:    src/tool/org/apache/turbine/tool LocalizationTool.java
  Log:
  Beginning of refactoring discussed with Jacopo Cappellato
  <[EMAIL PROTECTED]> on [EMAIL PROTECTED] mailing list:
  
  o Added support for setting a prefix to prepend to key names used when
  retrieving localized text.  Need a reasonable way to auto-negotiate
  this in init(Object) (perhaps best handled via subclassing?).
  
  o Added support for formatting an error message to return something
  other than null.  Need to add a TR.props file variable for formatting
  the error message.  Currently, alterante values can be returned by
  overriding formatErrorMessage (perhaps better named as
  formatMissingKeyMessage?).
  
  Revision  Changes    Path
  1.8       +53 -1     
jakarta-turbine-3/src/tool/org/apache/turbine/tool/LocalizationTool.java
  
  Index: LocalizationTool.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-turbine-3/src/tool/org/apache/turbine/tool/LocalizationTool.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -u -r1.7 -r1.8
  --- LocalizationTool.java     2001/10/13 22:44:48     1.7
  +++ LocalizationTool.java     2001/10/20 21:20:09     1.8
  @@ -89,6 +89,11 @@
       private String bundleName;
   
       /**
  +     * The prefix to prepend to localization keys.
  +     */
  +    private String keyPrefix;
  +
  +    /**
        * Creates a new instance.  Used by <code>PullService</code>.
        */
       public LocalizationTool()
  @@ -119,11 +124,16 @@
   
           try
           {
  +            String prefix = getPrefix();
  +            if (prefix != null)
  +            {
  +                key = prefix + key;
  +            }
               return bundle.getString(key);
           }
           catch (MissingResourceException noKey)
           {
  -            return null;
  +            return formatErrorMessage(key, noKey);
           }
       }
   
  @@ -141,6 +151,47 @@
           return Localization.getDefaultBundleName();
       }
   
  +    /**
  +     * Returns the prefix prepended to keys used to retrieve localized
  +     * text.
  +     *
  +     * @return The prefix to prepend to localization keys.
  +     */
  +    protected String getPrefix()
  +    {
  +        return keyPrefix;
  +    }
  +
  +    /**
  +     * Sets the prefix prepended to keys used to retrieve localized text.
  +     *
  +     * @param keyPrefix The prefix to prepend to localization keys, or
  +     * <code>null</code> for no prefix.
  +     */
  +    public void setPrefix(String keyPrefix)
  +    {
  +        this.keyPrefix = keyPrefix;
  +    }
  +
  +    /**
  +     * Returns <code>null</code> by default, which causes Velocity to
  +     * render your reference literally (i.e. <code>$l10n.FooKey</code>
  +     * will render as exactly that).  Useful as a hook for providing a
  +     * formatted error message.
  +     *
  +     * <p>TODO: Pull alternate error text from a TR.props value, or
  +     * return <code>null</code> if not available.</p>
  +     *
  +     * @param key The name of the key whose attempted retrieval caused
  +     * an error.
  +     * @param e The exception thrown when the named key was not
  +     * available.
  +     */
  +    protected String formatErrorMessage(String key, MissingResourceException e)
  +    {
  +        return null;
  +    }
  +
   
       // ApplicationTool implmentation
   
  @@ -167,5 +218,6 @@
           locale = null;
           bundle = null;
           bundleName = null;
  +        setPrefix(null);
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to