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