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

Zeta Enterprise Library

Rate me:
Please Sign up or sign in to vote.
4.97/5 (14 votes)
16 Jan 2010CPOL3 min read 50.2K   2.3K   48  
A small set of general-purpose classes for using in .NET applications (2.0 or higher)
namespace Zeta.EnterpriseLibrary.Tools
{
	using System;
	using System.ComponentModel;
	using System.Globalization;
	using Properties;
	using Common;

	/// <summary>
	/// BooleanLocalizedConverter uses local string for TRUE and FALSE.
	/// See also http://netcode.ru/dotnet/?lang=&katID=30&skatID=264&artID=7319.
	/// 
	/// Usage: 
	///		Apply [TypeConverter( typeof( BooleanLocalizedConverter ) )]
	///		to a property.
	/// </summary>
	public class BooleanLocalizedConverter : BooleanConverter
	{
		public override object ConvertTo(
			ITypeDescriptorContext context,
			CultureInfo culture,
			object value,
			Type destinationType )
		{
			if ( value is bool && destinationType == typeof( string ) )
			{
				return GetLocalized( ConvertHelper.ToBoolean( value ) );
			}
			else if ( value is string && destinationType == typeof( string ) )
			{
				return GetLocalized( ConvertHelper.ToBoolean( value ) );
			}
			else
			{
				return base.ConvertTo( context, culture, value, destinationType );
			}
		}

		public override object ConvertFrom(
			ITypeDescriptorContext context,
			CultureInfo culture,
			object value )
		{
			if ( value is string )
			{
				var v = (string)value;

				return v == Resources.SR_BooleanLocalizedConverter_True;
			}
			else if ( value is bool )
			{
				return GetLocalized( ConvertHelper.ToBoolean( value ) );
			}
			else
			{
				return base.ConvertFrom( context, culture, value );
			}
		}

		private static object GetLocalized( bool b )
		{
			return b
				? Resources.SR_BooleanLocalizedConverter_True
				: Resources.SR_BooleanLocalizedConverter_False;
		}
	}
}

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
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions