View Javadoc

    1   /*
    2    * $Id: I18n.java 497654 2007-01-19 00:21:57Z rgielen $
    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  package org.apache.struts2.components;
    22
    23  import java.io.Writer;
    24  import java.util.Locale;
    25  import java.util.ResourceBundle;
    26
    27  import org.apache.struts2.views.annotations.StrutsTag;
    28  import org.apache.struts2.views.annotations.StrutsTagAttribute;
    29  import org.apache.struts2.StrutsException;
    30
    31  import com.opensymphony.xwork2.ActionContext;
    32  import com.opensymphony.xwork2.LocaleProvider;
    33  import com.opensymphony.xwork2.TextProviderSupport;
    34  import com.opensymphony.xwork2.util.LocalizedTextUtil;
    35  import com.opensymphony.xwork2.util.ValueStack;
    36
    37  /***
    38   * <!-- START SNIPPET: javadoc -->
    39   *
    40   * Gets a resource bundle and place it on the value stack. This allows
    41   * the text tag to access messages from any bundle, and not just the bundle
    42   * associated with the current action.
    43   *
    44   * <!-- END SNIPPET: javadoc -->
    45   *
    46   * <p/>
    47   *
    48   * <!-- START SNIPPET: params-->
    49   *
    50   * <ul>
    51   *      <li>name* - the resource bundle's name (eg foo/bar/customBundle)</li>
    52   * </ul>
    53   *
    54   * <!-- END SNIPPET: params -->
    55   *
    56   * <p/>
    57   *
    58   * Example:
    59   *
    60   * <pre>
    61   * <!-- START SNIPPET: example -->
    62   *
    63   * &lt;s:i18n name="myCustomBundle"&gt;
    64   *    The i18n value for key aaa.bbb.ccc in myCustomBundle is &lt;s:property value="text('aaa.bbb.ccc')" /&gt;
    65   * &lt;/s:i18n&gt;
    66   *
    67   * <!-- END SNIPPET: example -->
    68   * </pre>
    69   *
    70   *
    71   * <pre>
    72   * <!-- START SNIPPET: i18nExample -->
    73   *
    74   * &lt;s:i18n name="some.package.bundle" &gt;
    75   *      &lt;s:text name="some.key" /&gt;
    76   * &lt;/s:i18n&gt;
    77   *
    78   * <!-- END SNIPPET: i18nExample -->
    79   * </pre>
    80   *
    81   */
    82  @StrutsTag(name="i18n", tldTagClass="org.apache.struts2.views.jsp.I18nTag", description="Get a resource bundle" +
    83                  " and place it on the value stack")
    84  public class I18n extends Component {
    85      protected boolean pushed;
    86      protected String name;
    87
    88      public I18n(ValueStack stack) {
    89          super(stack);
    90      }
    91
    92      public boolean start(Writer writer) {
    93          boolean result = super.start(writer);
    94
    95          try {
    96              String name = this.findString(this.name, "name", "Resource bundle name is required. Example: foo or foo_en");
    97              ResourceBundle bundle = (ResourceBundle) findValue("texts('" + name + "')");
    98
    99              if (bundle == null) {
    100                 bundle = LocalizedTextUtil.findResourceBundle(name, (Locale) getStack().getContext().get(ActionContext.LOCALE));
    101             }
    102
    103             if (bundle != null) {
    104                 final Locale locale = (Locale) getStack().getContext().get(ActionContext.LOCALE);
    105                 getStack().push(new TextProviderSupport(bundle, new LocaleProvider() {
    106                     public Locale getLocale() {
    107                         return locale;
    108                     }
    109                 }));
    110                 pushed = true;
    111             }
    112         } catch (Exception e) {
    113             String msg = "Could not find the bundle " + name;
    114             throw new StrutsException(msg, e);
    115         }
    116
    117         return result;
    118     }
    119
    120     public boolean end(Writer writer, String body) {
    121         if (pushed) {
    122             getStack().pop();
    123         }
    124
    125         return super.end(writer, body);
    126     }
    127
    128     @StrutsTagAttribute(description="Name of ressource bundle to use (eg foo/bar/customBundle)", required=true, defaultValue="String")
    129     public void setName(String name) {
    130         this.name = name;
    131     }
    132 }