TranslationBundle.java

  1. /*
  2.  * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com>
  3.  * and other copyright owners as documented in the project's IP log.
  4.  *
  5.  * This program and the accompanying materials are made available
  6.  * under the terms of the Eclipse Distribution License v1.0 which
  7.  * accompanies this distribution, is reproduced below, and is
  8.  * available at http://www.eclipse.org/org/documents/edl-v10.php
  9.  *
  10.  * All rights reserved.
  11.  *
  12.  * Redistribution and use in source and binary forms, with or
  13.  * without modification, are permitted provided that the following
  14.  * conditions are met:
  15.  *
  16.  * - Redistributions of source code must retain the above copyright
  17.  *   notice, this list of conditions and the following disclaimer.
  18.  *
  19.  * - Redistributions in binary form must reproduce the above
  20.  *   copyright notice, this list of conditions and the following
  21.  *   disclaimer in the documentation and/or other materials provided
  22.  *   with the distribution.
  23.  *
  24.  * - Neither the name of the Eclipse Foundation, Inc. nor the
  25.  *   names of its contributors may be used to endorse or promote
  26.  *   products derived from this software without specific prior
  27.  *   written permission.
  28.  *
  29.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42.  */

  43. package org.eclipse.jgit.nls;

  44. import java.lang.reflect.Field;
  45. import java.util.Locale;
  46. import java.util.MissingResourceException;
  47. import java.util.ResourceBundle;

  48. import org.eclipse.jgit.errors.TranslationBundleLoadingException;
  49. import org.eclipse.jgit.errors.TranslationStringMissingException;

  50. /**
  51.  * Base class for all translation bundles that provides injection of translated
  52.  * texts into public String fields.
  53.  *
  54.  * <p>
  55.  * The usage pattern is shown with the following example. First define a new
  56.  * translation bundle:
  57.  *
  58.  * <pre>
  59.  * public class TransportText extends TranslationBundle {
  60.  *  public static TransportText get() {
  61.  *      return NLS.getBundleFor(TransportText.class);
  62.  *  }
  63.  *
  64.  *  public String repositoryNotFound;
  65.  *
  66.  *  public String transportError;
  67.  * }
  68.  * </pre>
  69.  *
  70.  * Second, define one or more resource bundle property files.
  71.  *
  72.  * <pre>
  73.  * TransportText_en_US.properties:
  74.  *      repositoryNotFound=repository {0} not found
  75.  *      transportError=unknown error talking to {0}
  76.  * TransportText_de.properties:
  77.  *      repositoryNotFound=repository {0} nicht gefunden
  78.  *      transportError=unbekannter Fehler während der Kommunikation mit {0}
  79.  * ...
  80.  * </pre>
  81.  *
  82.  * Then make use of it:
  83.  *
  84.  * <pre>
  85.  * NLS.setLocale(Locale.GERMAN); // or skip this call to stick to the JVM default locale
  86.  * ...
  87.  * throw new TransportException(uri, TransportText.get().transportError);
  88.  * </pre>
  89.  *
  90.  * The translated text is automatically injected into the public String fields
  91.  * according to the locale set with
  92.  * {@link org.eclipse.jgit.nls.NLS#setLocale(Locale)}. However, the
  93.  * {@link org.eclipse.jgit.nls.NLS#setLocale(Locale)} method defines only
  94.  * prefered locale which will be honored only if it is supported by the provided
  95.  * resource bundle property files. Basically, this class will use
  96.  * {@link java.util.ResourceBundle#getBundle(String, Locale)} method to load a
  97.  * resource bundle. See the documentation of this method for a detailed
  98.  * explanation of resource bundle loading strategy. After a bundle is created
  99.  * the {@link #effectiveLocale()} method can be used to determine whether the
  100.  * bundle really corresponds to the requested locale or is a fallback.
  101.  *
  102.  * <p>
  103.  * To load a String from a resource bundle property file this class uses the
  104.  * {@link java.util.ResourceBundle#getString(String)}. This method can throw the
  105.  * {@link java.util.MissingResourceException} and this class is not making any
  106.  * effort to catch and/or translate this exception.
  107.  *
  108.  * <p>
  109.  * To define a concrete translation bundle one has to:
  110.  * <ul>
  111.  * <li>extend this class
  112.  * <li>define a public static get() method like in the example above
  113.  * <li>define public static String fields for each text message
  114.  * <li>make sure the translation bundle class provide public no arg constructor
  115.  * <li>provide one or more resource bundle property files in the same package
  116.  * where the translation bundle class resides
  117.  * </ul>
  118.  */
  119. public abstract class TranslationBundle {

  120.     private Locale effectiveLocale;
  121.     private ResourceBundle resourceBundle;

  122.     /**
  123.      * Get the locale used for loading the resource bundle from which the field
  124.      * values were taken.
  125.      *
  126.      * @return the locale used for loading the resource bundle from which the
  127.      *         field values were taken.
  128.      */
  129.     public Locale effectiveLocale() {
  130.         return effectiveLocale;
  131.     }

  132.     /**
  133.      * Get the resource bundle on which this translation bundle is based.
  134.      *
  135.      * @return the resource bundle on which this translation bundle is based.
  136.      */
  137.     public ResourceBundle resourceBundle() {
  138.         return resourceBundle;
  139.     }

  140.     /**
  141.      * Injects locale specific text in all instance fields of this instance.
  142.      * Only public instance fields of type <code>String</code> are considered.
  143.      * <p>
  144.      * The name of this (sub)class plus the given <code>locale</code> parameter
  145.      * define the resource bundle to be loaded. In other words the
  146.      * <code>this.getClass().getName()</code> is used as the
  147.      * <code>baseName</code> parameter in the
  148.      * {@link ResourceBundle#getBundle(String, Locale)} parameter to load the
  149.      * resource bundle.
  150.      * <p>
  151.      *
  152.      * @param locale
  153.      *            defines the locale to be used when loading the resource bundle
  154.      * @exception TranslationBundleLoadingException
  155.      *                see {@link TranslationBundleLoadingException}
  156.      * @exception TranslationStringMissingException
  157.      *                see {@link TranslationStringMissingException}
  158.      */
  159.     void load(Locale locale)
  160.             throws TranslationBundleLoadingException {
  161.         Class bundleClass = getClass();
  162.         try {
  163.             resourceBundle = ResourceBundle.getBundle(bundleClass.getName(),
  164.                     locale, bundleClass.getClassLoader());
  165.         } catch (MissingResourceException e) {
  166.             throw new TranslationBundleLoadingException(bundleClass, locale, e);
  167.         }
  168.         this.effectiveLocale = resourceBundle.getLocale();

  169.         for (Field field : bundleClass.getFields()) {
  170.             if (field.getType().equals(String.class)) {
  171.                 try {
  172.                     String translatedText = resourceBundle.getString(field.getName());
  173.                     field.set(this, translatedText);
  174.                 } catch (MissingResourceException e) {
  175.                     throw new TranslationStringMissingException(bundleClass, locale, field.getName(), e);
  176.                 } catch (IllegalArgumentException | IllegalAccessException e) {
  177.                     throw new Error(e);
  178.                 }
  179.             }
  180.         }
  181.     }
  182. }