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
44 package org.eclipse.jgit.nls;
45
46 import java.lang.reflect.Field;
47 import java.util.Locale;
48 import java.util.MissingResourceException;
49 import java.util.ResourceBundle;
50
51 import org.eclipse.jgit.errors.TranslationBundleLoadingException;
52 import org.eclipse.jgit.errors.TranslationStringMissingException;
53
54 /**
55 * Base class for all translation bundles that provides injection of translated
56 * texts into public String fields.
57 *
58 * <p>
59 * The usage pattern is shown with the following example. First define a new
60 * translation bundle:
61 *
62 * <pre>
63 * public class TransportText extends TranslationBundle {
64 * public static TransportText get() {
65 * return NLS.getBundleFor(TransportText.class);
66 * }
67 *
68 * public String repositoryNotFound;
69 *
70 * public String transportError;
71 * }
72 * </pre>
73 *
74 * Second, define one or more resource bundle property files.
75 *
76 * <pre>
77 * TransportText_en_US.properties:
78 * repositoryNotFound=repository {0} not found
79 * transportError=unknown error talking to {0}
80 * TransportText_de.properties:
81 * repositoryNotFound=repository {0} nicht gefunden
82 * transportError=unbekannter Fehler während der Kommunikation mit {0}
83 * ...
84 * </pre>
85 *
86 * Then make use of it:
87 *
88 * <pre>
89 * NLS.setLocale(Locale.GERMAN); // or skip this call to stick to the JVM default locale
90 * ...
91 * throw new TransportException(uri, TransportText.get().transportError);
92 * </pre>
93 *
94 * The translated text is automatically injected into the public String fields
95 * according to the locale set with
96 * {@link org.eclipse.jgit.nls.NLS#setLocale(Locale)}. However, the
97 * {@link org.eclipse.jgit.nls.NLS#setLocale(Locale)} method defines only
98 * prefered locale which will be honored only if it is supported by the provided
99 * resource bundle property files. Basically, this class will use
100 * {@link java.util.ResourceBundle#getBundle(String, Locale)} method to load a
101 * resource bundle. See the documentation of this method for a detailed
102 * explanation of resource bundle loading strategy. After a bundle is created
103 * the {@link #effectiveLocale()} method can be used to determine whether the
104 * bundle really corresponds to the requested locale or is a fallback.
105 *
106 * <p>
107 * To load a String from a resource bundle property file this class uses the
108 * {@link java.util.ResourceBundle#getString(String)}. This method can throw the
109 * {@link java.util.MissingResourceException} and this class is not making any
110 * effort to catch and/or translate this exception.
111 *
112 * <p>
113 * To define a concrete translation bundle one has to:
114 * <ul>
115 * <li>extend this class
116 * <li>define a public static get() method like in the example above
117 * <li>define public static String fields for each text message
118 * <li>make sure the translation bundle class provide public no arg constructor
119 * <li>provide one or more resource bundle property files in the same package
120 * where the translation bundle class resides
121 * </ul>
122 */
123 public abstract class TranslationBundle {
124
125 private Locale effectiveLocale;
126 private ResourceBundle resourceBundle;
127
128 /**
129 * Get the locale used for loading the resource bundle from which the field
130 * values were taken.
131 *
132 * @return the locale used for loading the resource bundle from which the
133 * field values were taken.
134 */
135 public Locale effectiveLocale() {
136 return effectiveLocale;
137 }
138
139 /**
140 * Get the resource bundle on which this translation bundle is based.
141 *
142 * @return the resource bundle on which this translation bundle is based.
143 */
144 public ResourceBundle resourceBundle() {
145 return resourceBundle;
146 }
147
148 /**
149 * Injects locale specific text in all instance fields of this instance.
150 * Only public instance fields of type <code>String</code> are considered.
151 * <p>
152 * The name of this (sub)class plus the given <code>locale</code> parameter
153 * define the resource bundle to be loaded. In other words the
154 * <code>this.getClass().getName()</code> is used as the
155 * <code>baseName</code> parameter in the
156 * {@link ResourceBundle#getBundle(String, Locale)} parameter to load the
157 * resource bundle.
158 * <p>
159 *
160 * @param locale
161 * defines the locale to be used when loading the resource bundle
162 * @exception TranslationBundleLoadingException
163 * see {@link TranslationBundleLoadingException}
164 * @exception TranslationStringMissingException
165 * see {@link TranslationStringMissingException}
166 */
167 void load(Locale locale)
168 throws TranslationBundleLoadingException {
169 Class bundleClass = getClass();
170 try {
171 resourceBundle = ResourceBundle.getBundle(bundleClass.getName(),
172 locale, bundleClass.getClassLoader());
173 } catch (MissingResourceException e) {
174 throw new TranslationBundleLoadingException(bundleClass, locale, e);
175 }
176 this.effectiveLocale = resourceBundle.getLocale();
177
178 for (Field field : bundleClass.getFields()) {
179 if (field.getType().equals(String.class)) {
180 try {
181 String translatedText = resourceBundle.getString(field.getName());
182 field.set(this, translatedText);
183 } catch (MissingResourceException e) {
184 throw new TranslationStringMissingException(bundleClass, locale, field.getName(), e);
185 } catch (IllegalArgumentException e) {
186 throw new Error(e);
187 } catch (IllegalAccessException e) {
188 throw new Error(e);
189 }
190 }
191 }
192 }
193 }