You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TranslationBundle.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 {@link NLS#setLocale(Locale)}. However, the
  92. * {@link NLS#setLocale(Locale)} method defines only prefered locale which will
  93. * be honored only if it is supported by the provided resource bundle property
  94. * files. Basically, this class will use
  95. * {@link ResourceBundle#getBundle(String, Locale)} method to load a resource
  96. * bundle. See the documentation of this method for a detailed explanation of
  97. * resource bundle loading strategy. After a bundle is created the
  98. * {@link #effectiveLocale()} method can be used to determine whether the bundle
  99. * really corresponds to the requested locale or is a fallback.
  100. *
  101. * <p>
  102. * To load a String from a resource bundle property file this class uses the
  103. * {@link ResourceBundle#getString(String)}. This method can throw the
  104. * {@link MissingResourceException} and this class is not making any effort to
  105. * catch and/or translate this exception.
  106. *
  107. * <p>
  108. * To define a concrete translation bundle one has to:
  109. * <ul>
  110. * <li>extend this class
  111. * <li>define a public static get() method like in the example above
  112. * <li>define public static String fields for each text message
  113. * <li>make sure the translation bundle class provide public no arg constructor
  114. * <li>provide one or more resource bundle property files in the same package
  115. * where the translation bundle class resides
  116. * </ul>
  117. */
  118. public abstract class TranslationBundle {
  119. private Locale effectiveLocale;
  120. private ResourceBundle resourceBundle;
  121. /**
  122. * @return the locale locale used for loading the resource bundle from which
  123. * the field values were taken
  124. */
  125. public Locale effectiveLocale() {
  126. return effectiveLocale;
  127. }
  128. /**
  129. * @return the resource bundle on which this translation bundle is based
  130. */
  131. public ResourceBundle resourceBundle() {
  132. return resourceBundle;
  133. }
  134. /**
  135. * Injects locale specific text in all instance fields of this instance.
  136. * Only public instance fields of type <code>String</code> are considered.
  137. * <p>
  138. * The name of this (sub)class plus the given <code>locale</code> parameter
  139. * define the resource bundle to be loaded. In other words the
  140. * <code>this.getClass().getName()</code> is used as the
  141. * <code>baseName</code> parameter in the
  142. * {@link ResourceBundle#getBundle(String, Locale)} parameter to load the
  143. * resource bundle.
  144. * <p>
  145. *
  146. * @param locale
  147. * defines the locale to be used when loading the resource bundle
  148. * @exception TranslationBundleLoadingException
  149. * see {@link TranslationBundleLoadingException}
  150. * @exception TranslationStringMissingException
  151. * see {@link TranslationStringMissingException}
  152. */
  153. void load(Locale locale)
  154. throws TranslationBundleLoadingException {
  155. Class bundleClass = getClass();
  156. try {
  157. resourceBundle = ResourceBundle.getBundle(bundleClass.getName(),
  158. locale, bundleClass.getClassLoader());
  159. } catch (MissingResourceException e) {
  160. throw new TranslationBundleLoadingException(bundleClass, locale, e);
  161. }
  162. this.effectiveLocale = resourceBundle.getLocale();
  163. for (Field field : bundleClass.getFields()) {
  164. if (field.getType().equals(String.class)) {
  165. try {
  166. String translatedText = resourceBundle.getString(field.getName());
  167. field.set(this, translatedText);
  168. } catch (MissingResourceException e) {
  169. throw new TranslationStringMissingException(bundleClass, locale, field.getName(), e);
  170. } catch (IllegalArgumentException e) {
  171. throw new Error(e);
  172. } catch (IllegalAccessException e) {
  173. throw new Error(e);
  174. }
  175. }
  176. }
  177. }
  178. }