~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/NGit/NGit.Nls/TranslationBundle.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
Import upstream version 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
This code is derived from jgit (http://eclipse.org/jgit).
 
3
Copyright owners are documented in jgit'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
using System;
 
45
using System.Globalization;
 
46
using System.Reflection;
 
47
using NGit.Errors;
 
48
using NGit.Nls;
 
49
using Sharpen;
 
50
 
 
51
namespace NGit.Nls
 
52
{
 
53
        /// <summary>
 
54
        /// Base class for all translation bundles that provides injection of translated
 
55
        /// texts into public String fields.
 
56
        /// </summary>
 
57
        /// <remarks>
 
58
        /// Base class for all translation bundles that provides injection of translated
 
59
        /// texts into public String fields.
 
60
        /// <p>
 
61
        /// The usage pattern is shown with the following example. First define a new
 
62
        /// translation bundle:
 
63
        /// <pre>
 
64
        /// public class TransportText extends TranslationBundle {
 
65
        /// public static TransportText get() {
 
66
        /// return NLS.getBundleFor(TransportText.class);
 
67
        /// }
 
68
        /// public String repositoryNotFound;
 
69
        /// public String transportError;
 
70
        /// }
 
71
        /// </pre>
 
72
        /// Second, define one or more resource bundle property files.
 
73
        /// <pre>
 
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}
 
80
        /// ...
 
81
        /// </pre>
 
82
        /// Then make use of it:
 
83
        /// <pre>
 
84
        /// NLS.setLocale(Locale.GERMAN); // or skip this call to stick to the JVM default locale
 
85
        /// ...
 
86
        /// throw new TransportException(uri, TransportText.get().transportError);
 
87
        /// </pre>
 
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)
 
91
        ///     </see>
 
92
        /// . However, the
 
93
        /// <see cref="NLS.SetLocale(System.Globalization.CultureInfo)">NLS.SetLocale(System.Globalization.CultureInfo)
 
94
        ///     </see>
 
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.
 
106
        /// <p>
 
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)
 
109
        ///     </see>
 
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.
 
114
        /// <p>
 
115
        /// To define a concrete translation bundle one has to:
 
116
        /// <ul>
 
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
 
123
        /// </ul>
 
124
        /// </remarks>
 
125
        public abstract class TranslationBundle
 
126
        {
 
127
                private CultureInfo effectiveLocale;
 
128
 
 
129
                private Sharpen.ResourceBundle resourceBundle;
 
130
 
 
131
                /// <returns>
 
132
                /// the locale locale used for loading the resource bundle from which
 
133
                /// the field values were taken
 
134
                /// </returns>
 
135
                public virtual CultureInfo EffectiveLocale()
 
136
                {
 
137
                        return effectiveLocale;
 
138
                }
 
139
 
 
140
                /// <returns>the resource bundle on which this translation bundle is based</returns>
 
141
                public virtual Sharpen.ResourceBundle ResourceBundle()
 
142
                {
 
143
                        return resourceBundle;
 
144
                }
 
145
 
 
146
                /// <summary>Injects locale specific text in all instance fields of this instance.</summary>
 
147
                /// <remarks>
 
148
                /// Injects locale specific text in all instance fields of this instance.
 
149
                /// Only public instance fields of type <code>String</code> are considered.
 
150
                /// <p>
 
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
 
158
                /// resource bundle.
 
159
                /// <p>
 
160
                /// </remarks>
 
161
                /// <param name="locale">defines the locale to be used when loading the resource bundle
 
162
                ///     </param>
 
163
                /// <exception>
 
164
                /// TranslationBundleLoadingException
 
165
                /// see
 
166
                /// <see cref="NGit.Errors.TranslationBundleLoadingException">NGit.Errors.TranslationBundleLoadingException
 
167
                ///     </see>
 
168
                /// </exception>
 
169
                /// <exception>
 
170
                /// TranslationStringMissingException
 
171
                /// see
 
172
                /// <see cref="NGit.Errors.TranslationStringMissingException">NGit.Errors.TranslationStringMissingException
 
173
                ///     </see>
 
174
                /// </exception>
 
175
                /// <exception cref="NGit.Errors.TranslationBundleLoadingException"></exception>
 
176
                internal virtual void Load(CultureInfo locale)
 
177
                {
 
178
                        Type bundleClass = GetType();
 
179
                        try
 
180
                        {
 
181
                                resourceBundle = Sharpen.ResourceBundle.GetBundle(bundleClass.FullName, locale);
 
182
                        }
 
183
                        catch (MissingResourceException e)
 
184
                        {
 
185
                                throw new TranslationBundleLoadingException(bundleClass, locale, e);
 
186
                        }
 
187
                        this.effectiveLocale = resourceBundle.GetLocale();
 
188
                        foreach (FieldInfo field in bundleClass.GetFields())
 
189
                        {
 
190
                                if (field.FieldType.Equals(typeof(string)))
 
191
                                {
 
192
                                        try
 
193
                                        {
 
194
                                                string translatedText = resourceBundle.GetString(field.Name);
 
195
                                                field.SetValue(this, translatedText);
 
196
                                        }
 
197
                                        catch (MissingResourceException e)
 
198
                                        {
 
199
                                                throw new TranslationStringMissingException(bundleClass, locale, field.Name, e);
 
200
                                        }
 
201
                                        catch (ArgumentException e)
 
202
                                        {
 
203
                                                throw new Error(e);
 
204
                                        }
 
205
                                        catch (MemberAccessException e)
 
206
                                        {
 
207
                                                throw new Error(e);
 
208
                                        }
 
209
                                }
 
210
                        }
 
211
                }
 
212
        }
 
213
}