public static ResourceBundle GetBundle (string bundleClass, CultureInfo culture)
 {
     Assembly asm = null;
     foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies ()) {
         if (a.GetType (bundleClass) != null) {
             asm = a;
             break;
         }
     }
     if (asm == null)
         throw new MissingResourceException ();
     Stream manifestResourceStream;
     manifestResourceStream = asm.GetManifestResourceStream (bundleClass + "_" + culture.ToString().Replace ('-','_') + ".properties");
     if (manifestResourceStream == null)
         manifestResourceStream = asm.GetManifestResourceStream (bundleClass + "_" + culture.TwoLetterISOLanguageName + ".properties");
     if (manifestResourceStream == null)
         manifestResourceStream = asm.GetManifestResourceStream (bundleClass + ".properties");
     if (manifestResourceStream != null) {
         ResourceBundle bundle = new ResourceBundle ();
         bundle.culture = culture;
         bundle.Load (manifestResourceStream);
         return bundle;
     } else
         throw new MissingResourceException ();
 }
Ejemplo n.º 2
0
        public static ResourceBundle GetBundle(string bundleClass, string name, CultureInfo culture)
        {
            Assembly asm = null;

            foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (a.GetType(bundleClass) != null)
                {
                    asm = a;
                    break;
                }
            }
            if (asm == null)
            {
                throw new MissingResourceException();
            }
            Stream manifestResourceStream;

            manifestResourceStream = asm.GetManifestResourceStream(bundleClass + "_" + culture.ToString().Replace('-', '_') + ".properties");
            if (manifestResourceStream == null)
            {
                manifestResourceStream = asm.GetManifestResourceStream(bundleClass + "_" + culture.TwoLetterISOLanguageName + ".properties");
            }
            if (manifestResourceStream == null)
            {
                manifestResourceStream = asm.GetManifestResourceStream(name + ".properties");
            }
            if (manifestResourceStream != null)
            {
                ResourceBundle bundle = new ResourceBundle();
                bundle.culture = culture;
                bundle.Load(manifestResourceStream);
                return(bundle);
            }
            else
            {
                throw new MissingResourceException();
            }
        }
        /// <summary>Injects locale specific text in all instance fields of this instance.</summary>
        /// <remarks>
        /// Injects locale specific text in all instance fields of this instance.
        /// Only public instance fields of type <code>String</code> are considered.
        /// <p>
        /// The name of this (sub)class plus the given <code>locale</code> parameter
        /// define the resource bundle to be loaded. In other words the
        /// <code>this.getClass().getName()</code> is used as the
        /// <code>baseName</code> parameter in the
        /// <see cref="Sharpen.ResourceBundle.GetBundle(string, System.Globalization.CultureInfo)
        ///     ">Sharpen.ResourceBundle.GetBundle(string, System.Globalization.CultureInfo)</see>
        /// parameter to load the
        /// resource bundle.
        /// <p>
        /// </remarks>
        /// <param name="locale">defines the locale to be used when loading the resource bundle
        ///     </param>
        /// <exception>
        /// TranslationBundleLoadingException
        /// see
        /// <see cref="NGit.Errors.TranslationBundleLoadingException">NGit.Errors.TranslationBundleLoadingException
        ///     </see>
        /// </exception>
        /// <exception>
        /// TranslationStringMissingException
        /// see
        /// <see cref="NGit.Errors.TranslationStringMissingException">NGit.Errors.TranslationStringMissingException
        ///     </see>
        /// </exception>
        /// <exception cref="NGit.Errors.TranslationBundleLoadingException"></exception>
        internal virtual void Load(CultureInfo locale)
        {
            Type bundleClass = GetType();

            try
            {
                // NGit.NGit.resources.JGitText
                resourceBundle = Sharpen.ResourceBundle.GetBundle(bundleClass.FullName, "NGit.NGit.resources.JGitText", locale);
                //resourceBundle = Sharpen.ResourceBundle.GetBundle(bundleClass.FullName, locale);
            }
            catch (MissingResourceException e)
            {
                throw new TranslationBundleLoadingException(bundleClass, locale, e);
            }
            this.effectiveLocale = resourceBundle.GetLocale();
            foreach (FieldInfo field in bundleClass.GetFields())
            {
                if (field.FieldType.Equals(typeof(string)))
                {
                    try
                    {
                        string translatedText = resourceBundle.GetString(field.Name);
                        field.SetValue(this, translatedText);
                    }
                    catch (MissingResourceException e)
                    {
                        throw new TranslationStringMissingException(bundleClass, locale, field.Name, e);
                    }
                    catch (ArgumentException e)
                    {
                        throw new Error(e);
                    }
                    catch (MemberAccessException e)
                    {
                        throw new Error(e);
                    }
                }
            }
        }
Ejemplo n.º 4
0
		/// <summary>Injects locale specific text in all instance fields of this instance.</summary>
		/// <remarks>
		/// Injects locale specific text in all instance fields of this instance.
		/// Only public instance fields of type <code>String</code> are considered.
		/// <p>
		/// The name of this (sub)class plus the given <code>locale</code> parameter
		/// define the resource bundle to be loaded. In other words the
		/// <code>this.getClass().getName()</code> is used as the
		/// <code>baseName</code> parameter in the
		/// <see cref="Sharpen.ResourceBundle.GetBundle(string, System.Globalization.CultureInfo)
		/// 	">Sharpen.ResourceBundle.GetBundle(string, System.Globalization.CultureInfo)</see>
		/// parameter to load the
		/// resource bundle.
		/// <p>
		/// </remarks>
		/// <param name="locale">defines the locale to be used when loading the resource bundle
		/// 	</param>
		/// <exception>
		/// TranslationBundleLoadingException
		/// see
		/// <see cref="NGit.Errors.TranslationBundleLoadingException">NGit.Errors.TranslationBundleLoadingException
		/// 	</see>
		/// </exception>
		/// <exception>
		/// TranslationStringMissingException
		/// see
		/// <see cref="NGit.Errors.TranslationStringMissingException">NGit.Errors.TranslationStringMissingException
		/// 	</see>
		/// </exception>
		/// <exception cref="NGit.Errors.TranslationBundleLoadingException"></exception>
		internal virtual void Load(CultureInfo locale)
		{
			Type bundleClass = GetType();
			try
			{
				resourceBundle = Sharpen.ResourceBundle.GetBundle(bundleClass.FullName, locale);
			}
			catch (MissingResourceException e)
			{
				throw new TranslationBundleLoadingException(bundleClass, locale, e);
			}
			this.effectiveLocale = resourceBundle.GetLocale();
			foreach (FieldInfo field in bundleClass.GetFields())
			{
				if (field.FieldType.Equals(typeof(string)))
				{
					try
					{
						string translatedText = resourceBundle.GetString(field.Name);
						field.SetValue(this, translatedText);
					}
					catch (MissingResourceException e)
					{
						throw new TranslationStringMissingException(bundleClass, locale, field.Name, e);
					}
					catch (ArgumentException e)
					{
						throw new Error(e);
					}
					catch (MemberAccessException e)
					{
						throw new Error(e);
					}
				}
			}
		}