Click here to Skip to main content
15,893,814 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 143K   2.2K   143  
Concepts and patterns for the handling of strings in multilingual applications.
// -- FILE ------------------------------------------------------------------
// name       : Localization.Web.cs
// project    : Itenso String Resources
// created    : Jani Giannoudis - 2012.03.21
// language   : c#
// environment: .NET 4.0
// copyright  : (c) 2004-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.ComponentModel;
using System.Reflection;
using System.Web.UI;

namespace Itenso.Community.StringResources.ResStrings
{

	// ------------------------------------------------------------------------
	[Designer( typeof( LocalizationDesigner ) )]
	public class Localization : Control
	{

		// ----------------------------------------------------------------------
		public enum LocalizationSetup
		{
			Init,
			Load,
			PreRender,
		} // enum LocalizationSetup

		// ----------------------------------------------------------------------
		private class ObjectProperty
		{

			// --------------------------------------------------------------------
			public object Object { get; set; }

			// --------------------------------------------------------------------
			public PropertyInfo Property { get; set; }

		} // ObjectProperty

		// ----------------------------------------------------------------------
		public Localization()
		{
			ApplyOn = LocalizationSetup.Load;
		} // Localization

		// ----------------------------------------------------------------------
		[Themeable( false )]
		[Description( "Set the target control" )]
		public string TargetControl { get; set; }

		// ----------------------------------------------------------------------
		[Themeable( false )]
		[Description( "Set the target value" )]
		public string TargetValue { get; set; }

		// ----------------------------------------------------------------------
		[Themeable( false )]
		[Description( "Set the localization source type" )]
		public string SourceType { get; set; }

		// ----------------------------------------------------------------------
		[Themeable( false )]
		[Description( "Set the localization source value" )]
		public string SourceValue { get; set; }

		// ----------------------------------------------------------------------
		[Themeable( false )]
		[Description( "Set the localization setup phase" )]
		[DefaultValue( LocalizationSetup.Load )]
		public LocalizationSetup ApplyOn { get; set; }

		// ----------------------------------------------------------------------
		protected override void OnInit( EventArgs e )
		{
			base.OnInit( e );
			if ( ApplyOn == LocalizationSetup.Init )
			{
				Localize();
			}
		} // OnInit

		// ----------------------------------------------------------------------
		protected override void OnLoad( EventArgs e )
		{
			base.OnLoad( e );
			if ( ApplyOn == LocalizationSetup.Load )
			{
				Localize();
			}
		} // OnLoad

		// ----------------------------------------------------------------------
		protected override void OnPreRender( EventArgs e )
		{
			base.OnPreRender( e );
			if ( ApplyOn == LocalizationSetup.PreRender )
			{
				Localize();
			}
		} // OnPreRender

		// ----------------------------------------------------------------------
		private void Localize()
		{
			if ( !DesignMode && !string.IsNullOrEmpty( TargetControl ) && !string.IsNullOrEmpty( TargetValue ) &&
				!string.IsNullOrEmpty( SourceType ) && !string.IsNullOrEmpty( SourceValue ) )
			{
				Control control = Parent.FindControl( TargetControl );
				if ( control != null )
				{
					ObjectProperty objectProperty = FindObjectProperty( control, TargetValue );
					if ( objectProperty != null )
					{
						object localizedValue = StringsExpressionParser.Parse( SourceType, SourceValue );
						if ( localizedValue != null && objectProperty.Property.PropertyType.IsAssignableFrom( localizedValue.GetType() ) )
						{
							objectProperty.Property.SetValue( objectProperty.Object, localizedValue, null );
						}
					}
				}
			}
		} // Localize

		// ----------------------------------------------------------------------
		private static ObjectProperty FindObjectProperty( object source, string propertyName )
		{
			if ( source == null || string.IsNullOrEmpty( propertyName ) )
			{
				return null;
			}

			int childIndex = propertyName.IndexOf( '.' );
			if ( childIndex > 0 )
			{
				PropertyInfo childProperty = source.GetType().GetProperty( propertyName.Substring( 0, childIndex ), BindingFlags.Public | BindingFlags.Instance );
				if ( childProperty == null )
				{
					return null;
				}
				object childObject = childProperty.GetValue( source, null );
				return childObject == null ? null : FindObjectProperty( childObject, propertyName.Substring( childIndex + 1 ) );
			}

			PropertyInfo property = source.GetType().GetProperty( propertyName, BindingFlags.Public | BindingFlags.Instance );
			return property == null ? null : new ObjectProperty { Object = source, Property = property };
		} // FindObjectProperty

	} // class Localization

} // 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