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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2010, Sasa Zivkov <sasa.zivkov@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.nls;
  11. import java.lang.reflect.Field;
  12. import java.util.Locale;
  13. import java.util.MissingResourceException;
  14. import java.util.ResourceBundle;
  15. import org.eclipse.jgit.errors.TranslationBundleLoadingException;
  16. import org.eclipse.jgit.errors.TranslationStringMissingException;
  17. /**
  18. * Base class for all translation bundles that provides injection of translated
  19. * texts into public String fields.
  20. *
  21. * <p>
  22. * The usage pattern is shown with the following example. First define a new
  23. * translation bundle:
  24. *
  25. * <pre>
  26. * public class TransportText extends TranslationBundle {
  27. * public static TransportText get() {
  28. * return NLS.getBundleFor(TransportText.class);
  29. * }
  30. *
  31. * public String repositoryNotFound;
  32. *
  33. * public String transportError;
  34. * }
  35. * </pre>
  36. *
  37. * Second, define one or more resource bundle property files.
  38. *
  39. * <pre>
  40. * TransportText_en_US.properties:
  41. * repositoryNotFound=repository {0} not found
  42. * transportError=unknown error talking to {0}
  43. * TransportText_de.properties:
  44. * repositoryNotFound=repository {0} nicht gefunden
  45. * transportError=unbekannter Fehler während der Kommunikation mit {0}
  46. * ...
  47. * </pre>
  48. *
  49. * Then make use of it:
  50. *
  51. * <pre>
  52. * NLS.setLocale(Locale.GERMAN); // or skip this call to stick to the JVM default locale
  53. * ...
  54. * throw new TransportException(uri, TransportText.get().transportError);
  55. * </pre>
  56. *
  57. * The translated text is automatically injected into the public String fields
  58. * according to the locale set with
  59. * {@link org.eclipse.jgit.nls.NLS#setLocale(Locale)}. However, the
  60. * {@link org.eclipse.jgit.nls.NLS#setLocale(Locale)} method defines only
  61. * prefered locale which will be honored only if it is supported by the provided
  62. * resource bundle property files. Basically, this class will use
  63. * {@link java.util.ResourceBundle#getBundle(String, Locale)} method to load a
  64. * resource bundle. See the documentation of this method for a detailed
  65. * explanation of resource bundle loading strategy. After a bundle is created
  66. * the {@link #effectiveLocale()} method can be used to determine whether the
  67. * bundle really corresponds to the requested locale or is a fallback.
  68. *
  69. * <p>
  70. * To load a String from a resource bundle property file this class uses the
  71. * {@link java.util.ResourceBundle#getString(String)}. This method can throw the
  72. * {@link java.util.MissingResourceException} and this class is not making any
  73. * effort to catch and/or translate this exception.
  74. *
  75. * <p>
  76. * To define a concrete translation bundle one has to:
  77. * <ul>
  78. * <li>extend this class
  79. * <li>define a public static get() method like in the example above
  80. * <li>define public static String fields for each text message
  81. * <li>make sure the translation bundle class provide public no arg constructor
  82. * <li>provide one or more resource bundle property files in the same package
  83. * where the translation bundle class resides
  84. * </ul>
  85. */
  86. public abstract class TranslationBundle {
  87. private Locale effectiveLocale;
  88. private ResourceBundle resourceBundle;
  89. /**
  90. * Get the locale used for loading the resource bundle from which the field
  91. * values were taken.
  92. *
  93. * @return the locale used for loading the resource bundle from which the
  94. * field values were taken.
  95. */
  96. public Locale effectiveLocale() {
  97. return effectiveLocale;
  98. }
  99. /**
  100. * Get the resource bundle on which this translation bundle is based.
  101. *
  102. * @return the resource bundle on which this translation bundle is based.
  103. */
  104. public ResourceBundle resourceBundle() {
  105. return resourceBundle;
  106. }
  107. /**
  108. * Injects locale specific text in all instance fields of this instance.
  109. * Only public instance fields of type <code>String</code> are considered.
  110. * <p>
  111. * The name of this (sub)class plus the given <code>locale</code> parameter
  112. * define the resource bundle to be loaded. In other words the
  113. * <code>this.getClass().getName()</code> is used as the
  114. * <code>baseName</code> parameter in the
  115. * {@link ResourceBundle#getBundle(String, Locale)} parameter to load the
  116. * resource bundle.
  117. * <p>
  118. *
  119. * @param locale
  120. * defines the locale to be used when loading the resource bundle
  121. * @exception TranslationBundleLoadingException
  122. * see {@link TranslationBundleLoadingException}
  123. * @exception TranslationStringMissingException
  124. * see {@link TranslationStringMissingException}
  125. */
  126. void load(Locale locale)
  127. throws TranslationBundleLoadingException {
  128. Class bundleClass = getClass();
  129. try {
  130. resourceBundle = ResourceBundle.getBundle(bundleClass.getName(),
  131. locale, bundleClass.getClassLoader());
  132. } catch (MissingResourceException e) {
  133. throw new TranslationBundleLoadingException(bundleClass, locale, e);
  134. }
  135. this.effectiveLocale = resourceBundle.getLocale();
  136. for (Field field : bundleClass.getFields()) {
  137. if (field.getType().equals(String.class)) {
  138. try {
  139. String translatedText = resourceBundle.getString(field.getName());
  140. field.set(this, translatedText);
  141. } catch (MissingResourceException e) {
  142. throw new TranslationStringMissingException(bundleClass, locale, field.getName(), e);
  143. } catch (IllegalArgumentException | IllegalAccessException e) {
  144. throw new Error(e);
  145. }
  146. }
  147. }
  148. }
  149. }