2
This code is derived from jgit (http://eclipse.org/jgit).
3
Copyright owners are documented in jgit's IP log.
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
12
Redistribution and use in source and binary forms, with or
13
without modification, are permitted provided that the following
16
- Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
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.
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
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.
45
using System.Globalization;
46
using System.Reflection;
54
/// Base class for all translation bundles that provides injection of translated
55
/// texts into public String fields.
58
/// Base class for all translation bundles that provides injection of translated
59
/// texts into public String fields.
61
/// The usage pattern is shown with the following example. First define a new
62
/// translation bundle:
64
/// public class TransportText extends TranslationBundle {
65
/// public static TransportText get() {
66
/// return NLS.getBundleFor(TransportText.class);
68
/// public String repositoryNotFound;
69
/// public String transportError;
72
/// Second, define one or more resource bundle property files.
74
/// TransportText_en_US.properties:
75
/// repositoryNotFound=repository {0} not found
76
/// transportError=unknown error talking to {0}
77
/// TransportText_de.properties:
78
/// repositoryNotFound=repository {0} nicht gefunden
79
/// transportError=unbekannter Fehler während der Kommunikation mit {0}
82
/// Then make use of it:
84
/// NLS.setLocale(Locale.GERMAN); // or skip this call to stick to the JVM default locale
86
/// throw new TransportException(uri, TransportText.get().transportError);
88
/// The translated text is automatically injected into the public String fields
89
/// according to the locale set with
90
/// <see cref="NLS.SetLocale(System.Globalization.CultureInfo)">NLS.SetLocale(System.Globalization.CultureInfo)
93
/// <see cref="NLS.SetLocale(System.Globalization.CultureInfo)">NLS.SetLocale(System.Globalization.CultureInfo)
95
/// method defines only prefered locale which will
96
/// be honored only if it is supported by the provided resource bundle property
97
/// files. Basically, this class will use
98
/// <see cref="Sharpen.ResourceBundle.GetBundle(string, System.Globalization.CultureInfo)
99
/// ">Sharpen.ResourceBundle.GetBundle(string, System.Globalization.CultureInfo)</see>
100
/// method to load a resource
101
/// bundle. See the documentation of this method for a detailed explanation of
102
/// resource bundle loading strategy. After a bundle is created the
103
/// <see cref="EffectiveLocale()">EffectiveLocale()</see>
104
/// method can be used to determine whether the
105
/// bundle really corresponds to the requested locale or is a fallback.
107
/// To load a String from a resource bundle property file this class uses the
108
/// <see cref="Sharpen.ResourceBundle.GetString(string)">Sharpen.ResourceBundle.GetString(string)
110
/// . This method can throw the
111
/// <see cref="Sharpen.MissingResourceException">Sharpen.MissingResourceException</see>
112
/// and this class is not making any effort to
113
/// catch and/or translate this exception.
115
/// To define a concrete translation bundle one has to:
117
/// <li>extend this class
118
/// <li>define a public static get() method like in the example above
119
/// <li>define public static String fields for each text message
120
/// <li>make sure the translation bundle class provide public no arg constructor
121
/// <li>provide one or more resource bundle property files in the same package
122
/// where the translation bundle class resides
125
public abstract class TranslationBundle
127
private CultureInfo effectiveLocale;
129
private Sharpen.ResourceBundle resourceBundle;
132
/// the locale locale used for loading the resource bundle from which
133
/// the field values were taken
135
public virtual CultureInfo EffectiveLocale()
137
return effectiveLocale;
140
/// <returns>the resource bundle on which this translation bundle is based</returns>
141
public virtual Sharpen.ResourceBundle ResourceBundle()
143
return resourceBundle;
146
/// <summary>Injects locale specific text in all instance fields of this instance.</summary>
148
/// Injects locale specific text in all instance fields of this instance.
149
/// Only public instance fields of type <code>String</code> are considered.
151
/// The name of this (sub)class plus the given <code>locale</code> parameter
152
/// define the resource bundle to be loaded. In other words the
153
/// <code>this.getClass().getName()</code> is used as the
154
/// <code>baseName</code> parameter in the
155
/// <see cref="Sharpen.ResourceBundle.GetBundle(string, System.Globalization.CultureInfo)
156
/// ">Sharpen.ResourceBundle.GetBundle(string, System.Globalization.CultureInfo)</see>
157
/// parameter to load the
161
/// <param name="locale">defines the locale to be used when loading the resource bundle
164
/// TranslationBundleLoadingException
166
/// <see cref="NGit.Errors.TranslationBundleLoadingException">NGit.Errors.TranslationBundleLoadingException
170
/// TranslationStringMissingException
172
/// <see cref="NGit.Errors.TranslationStringMissingException">NGit.Errors.TranslationStringMissingException
175
/// <exception cref="NGit.Errors.TranslationBundleLoadingException"></exception>
176
internal virtual void Load(CultureInfo locale)
178
Type bundleClass = GetType();
181
resourceBundle = Sharpen.ResourceBundle.GetBundle(bundleClass.FullName, locale);
183
catch (MissingResourceException e)
185
throw new TranslationBundleLoadingException(bundleClass, locale, e);
187
this.effectiveLocale = resourceBundle.GetLocale();
188
foreach (FieldInfo field in bundleClass.GetFields())
190
if (field.FieldType.Equals(typeof(string)))
194
string translatedText = resourceBundle.GetString(field.Name);
195
field.SetValue(this, translatedText);
197
catch (MissingResourceException e)
199
throw new TranslationStringMissingException(bundleClass, locale, field.Name, e);
201
catch (ArgumentException e)
205
catch (MemberAccessException e)