Click here to Skip to main content
15,885,881 members
Articles / Web Development / ASP.NET

.NET String Resources

Rate me:
Please Sign up or sign in to vote.
4.85/5 (37 votes)
19 Apr 2012CPOL7 min read 142K   2.2K   143  
Concepts and patterns for the handling of strings in multilingual applications.
// -- FILE ------------------------------------------------------------------
// name       : EnumTool.Windows.cs
// project    : Itenso String Resources
// created    : Jani Giannoudis - 2011.09.09
// language   : c#
// environment: .NET 4.0
// copyright  : (c) 2004-2011 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.IO;
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace Itenso.Community.StringResources.ResStrings
{

	// ------------------------------------------------------------------------
	internal static partial class EnumTool
	{

		// ----------------------------------------------------------------------
		internal static EnumImageItem<T>[] GetEnumImageItems<T>( StringsResourceManager resourceManager ) where T : struct
		{
			if ( resourceManager == null )
			{
				throw new ArgumentNullException( "resourceManager" );
			}
			CheckEnumType( typeof( T ) );

			FieldInfo[] fields = GetEnumFields<T>();
			int count = fields != null ? fields.Length : 0;
			EnumImageItem<T>[] enumItems = new EnumImageItem<T>[ count ];
			if ( fields != null )
			{
				for ( int i = 0; i < count; i++ )
				{
					FieldInfo field = fields[ i ];
					T value = (T)field.GetValue( null );

					string itemDescription = resourceManager.GetEnumValue( value );
					if ( string.IsNullOrEmpty( itemDescription ) )
					{
						itemDescription = field.Name;
					}
					Image image = LoadEnumImage( value, resourceManager );
					enumItems[ i ] = new EnumImageItem<T>( value, itemDescription, image );
				}
			}
			return enumItems;
		} // GetEnumImageItems

		// ----------------------------------------------------------------------
		private static Image LoadEnumImage<T>( T value, StringsResourceManager resourceManager ) where T : struct
		{
			Type type = typeof( T );
			string resourceKey = type.Name + EnumNameSeparator + Format( value );
			string imageResKey = type.Namespace + "." + resourceManager.ImagesPath + "." + resourceKey + resourceManager.ImageExtension;

			Stream stream = type.Assembly.GetManifestResourceStream( imageResKey );
			BitmapImage bitmapImage = null;
			if ( stream != null )
			{
				bitmapImage = new BitmapImage();
#if SILVERLIGHT
				bitmapImage.SetSource( stream );
#else
				MemoryStream memmoryStream = new MemoryStream();
				stream.CopyTo( memmoryStream );
				stream.Close();

				bitmapImage.BeginInit();
				bitmapImage.StreamSource = memmoryStream;
				bitmapImage.EndInit();
#endif
			}

			return new Image
			{
				Source = bitmapImage
			};
		} // LoadEnumImage

	} // class EnumTool

} // namespace Itenso.Community.StringResources.ResStrings
// -- EOF -------------------------------------------------------------------

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions