View Javadoc

    1   package org.apache.turbine.services.localization;
    2
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *   http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21
    22  import java.util.Locale;
    23  import java.util.MissingResourceException;
    24
    25  import org.apache.commons.logging.Log;
    26  import org.apache.commons.logging.LogFactory;
    27  import org.apache.fulcrum.localization.LocalizationService;
    28  import org.apache.turbine.services.InstantiationException;
    29  import org.apache.turbine.services.TurbineServices;
    30  import org.apache.turbine.services.pull.ApplicationTool;
    31  import org.apache.turbine.util.RunData;
    32  /**
    33   * A pull tool which provides lookups for localized text by delegating
    34   * to the configured Fulcrum <code>LocalizationService</code>.
    35   *
    36   * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a>
    37   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
    38   * @author <a href="mailto:jon@collab.net">Jon Stevens</a>
    39   */
    40  public class LocalizationTool implements ApplicationTool
    41  {
    42      /** Logging */
    43      private static Log log = LogFactory.getLog(LocalizationTool.class);
    44      /** Fulcrum Localization component */
    45      private LocalizationService localizationService;
    46      /**
    47       * The language and country information parsed from the request's
    48       * <code>Accept-Language</code> header.  Reset on each request.
    49       */
    50      protected Locale locale;
    51
    52      /**
    53       * Lazy load the LocalizationService.
    54       * @return a fulcrum LocalizationService
    55       */
    56      public LocalizationService getLocalizationService()
    57      {
    58          if (localizationService == null)
    59          {
    60              try
    61              {
    62                  localizationService = (LocalizationService)TurbineServices.getInstance()
    63                      .getService(LocalizationService.ROLE);
    64              }
    65              catch (Exception e)
    66              {
    67                  throw new InstantiationException("Problem looking up Localization Service:"+e.getMessage());
    68              }
    69          }
    70          return localizationService;
    71      }
    72
    73      /**
    74       * Creates a new instance.  Used by <code>PullService</code>.
    75       */
    76      public LocalizationTool()
    77      {
    78          refresh();
    79      }
    80
    81      /**
    82       * <p>Performs text lookups for localization.</p>
    83       *
    84       * <p>Assuming there is a instance of this class with a HTTP
    85       * request set in your template's context named <code>l10n</code>,
    86       * the VTL <code>$l10n.HELLO</code> would render to
    87       * <code>hello</code> for English requests and <code>hola</code>
    88       * in Spanish (depending on the value of the HTTP request's
    89       * <code>Accept-Language</code> header).</p>
    90       *
    91       * @param key The identifier for the localized text to retrieve.
    92       * @return The localized text.
    93       */
    94      public String get(String key)
    95      {
    96          try
    97          {
    98              return getLocalizationService().getString(getBundleName(null), getLocale(), key);
    99          }
    100         catch (MissingResourceException noKey)
    101         {
    102             log.error(noKey);
    103             return null;
    104         }
    105     }
    106
    107     /**
    108      * Gets the current locale.
    109      *
    110      * @return The locale currently in use.
    111      */
    112     public Locale getLocale()
    113     {
    114         return locale;
    115     }
    116
    117     /**
    118      * The return value of this method is used to set the name of the
    119      * bundle used by this tool.  Useful as a hook for using a
    120      * different bundle than specified in your
    121      * <code>LocalizationService</code> configuration.
    122      *
    123      * @param data The inputs passed from {@link #init(Object)}.
    124      * (ignored by this implementation).
    125      */
    126     protected String getBundleName(Object data)
    127     {
    128         return getLocalizationService().getDefaultBundleName();
    129     }
    130
    131     /**
    132      * Formats a localized value using the provided object.
    133      *
    134      * @param key The identifier for the localized text to retrieve,
    135      * @param arg1 The object to use as {0} when formatting the localized text.
    136      * @return Formatted localized text.
    137      * @see #format(String, Locale, String, Object[])
    138      */
    139     public String format(String key, Object arg1)
    140     {
    141         return getLocalizationService()
    142                 .format(getBundleName(null), getLocale(), key, arg1);
    143     }
    144
    145     /**
    146      * Formats a localized value using the provided objects.
    147      *
    148      * @param key The identifier for the localized text to retrieve,
    149      * @param arg1 The object to use as {0} when formatting the localized text.
    150      * @param arg2 The object to use as {1} when formatting the localized text.
    151      * @return Formatted localized text.
    152      * @see #format(String, Locale, String, Object[])
    153      */
    154     public String format(String key, Object arg1, Object arg2)
    155     {
    156         return getLocalizationService()
    157                 .format(getBundleName(null), getLocale(), key, arg1, arg2);
    158     }
    159
    160     /**
    161      * Formats a localized value using the provided objects.
    162      *
    163      * @param key The identifier for the localized text to retrieve,
    164      * @param args The objects to use as {0}, {1}, etc. when
    165      *             formatting the localized text.
    166      * @return Formatted localized text.
    167      */
    168     public String format(String key, Object[] args)
    169     {
    170         return getLocalizationService()
    171                 .format(getBundleName(null), getLocale(), key, args);
    172     }
    173
    174     // ApplicationTool implementation
    175
    176     /**
    177      * Sets the request to get the <code>Accept-Language</code> header
    178      * from (reset on each request).
    179      */
    180     public void init(Object data)
    181     {
    182         if (data instanceof RunData)
    183         {
    184             // Pull necessary information out of RunData while we have
    185             // a reference to it.
    186             locale = getLocalizationService().getLocale(((RunData) data).getRequest());
    187         }
    188     }
    189
    190     /**
    191      * No-op.
    192      */
    193     public void refresh()
    194     {
    195         locale = null;
    196     }
    197 }