Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

.NET String Resources

, 19 Apr 2012 CPOL
Concepts and patterns for the handling of strings in multilingual applications.
StringResources_SourceCode.zip
docu
StringResources_en.pdf
Pub
Desktop.Release
de
Itenso.Community.StringResources.ResStrings.resources.dll
Itenso.Community.StringResources.SampleLibrary.resources.dll
Itenso.Community.StringResources.ResStrings.dll
Itenso.Community.StringResources.SampleLibrary.dll
Release
de
Itenso.Community.StringResources.ResStrings.resources.dll
Itenso.Community.StringResources.SampleLibrary.resources.dll
Itenso.Community.StringResources.ResStrings.dll
Itenso.Community.StringResources.SampleLibrary.dll
Silverlight.Release
de
Itenso.Community.StringResources.ResStrings.resources.dll
Itenso.Community.StringResources.SampleLibrary.resources.dll
Itenso.Community.StringResources.ResStrings.dll
Itenso.Community.StringResources.SampleLibrary.dll
Web.Release
de
Itenso.Community.StringResources.ResStrings.resources.dll
Itenso.Community.StringResources.SampleLibrary.resources.dll
Itenso.Community.StringResources.ResStrings.dll
Itenso.Community.StringResources.SampleLibrary.dll
WindowsPhone.Release
de
Itenso.Community.StringResources.ResStrings.resources.dll
Itenso.Community.StringResources.SampleLibrary.resources.dll
Itenso.Community.StringResources.ResStrings.dll
Itenso.Community.StringResources.SampleLibrary.dll
ResStrings
Properties
SampleApplication.Web
App_Data
App_GlobalResources
App_LocalResources
Global.asax
Properties
SampleApplication.WinForms
Properties
Settings.settings
SampleApplication.Xaml
ApplicationIcon.png
Background.png
Properties
SplashScreenImage.jpg
SampleLibrary
Images
Status.Error.png
Status.Info.png
Status.Ok.png
Status.Warning.png
Properties
StringResources.suo
StringResources.Desktop.suo
StringResources.Silverlight.suo
StringResources.Web.suo
StringResources.WindowsPhone.suo
StringResources.WinForms.suo
// -- FILE ------------------------------------------------------------------
// name       : StringsResourceManager.cs
// project    : Itenso String Resources
// created    : Jani Giannoudis - 2011.09.09
// language   : c#
// environment: .NET 4.0
// copyright  : (c) 2004-2012 by Itenso GmbH, Switzerland
// --------------------------------------------------------------------------
using System;
using System.Globalization;
using System.Resources;

namespace Itenso.Community.StringResources.ResStrings
{

	// ------------------------------------------------------------------------
	/// <summary>
	/// Provides some helper functionality to keep resource handling in a
	/// namespace as simple and uniform as possible.
	/// </summary>
	/// <remarks>
	/// intended to be used by a singleton class per namespace, which should
	/// commonly be named <c>Strings</c>. This singleton instance should
	/// provide access to resources via strongly typed properties, thus
	/// avoiding coding errors with misspelled resource identifiers.
	/// </remarks>
	public partial class StringsResourceManager : ResourceManager
	{

		// ----------------------------------------------------------------------
		/// <summary>
		/// Creates a <c>ResourceManager</c> instance for the given type, loading its resources
		/// from the type's full name, suffixed with '<c>.resx</c>'.
		/// </summary>
		/// <param name="resourceSource">the .type containing the resources</param>
		public StringsResourceManager( Type resourceSource ) :
			base( resourceSource )
		{
		} // StringsResourceManager

		// ----------------------------------------------------------------------
		protected StringsResourceManager() :
			this( typeof( StringsResourceManager ) )
		{
		} // StringsResourceManager

		// ----------------------------------------------------------------------
		public bool UseStringIndicators
		{
			get { return useStringIndicators; }
			set { useStringIndicators = value; }
		} // UseStringIndicators

		// ----------------------------------------------------------------------
		public string UnknowResFormat
		{
			get { return unknowResFormat; }
			set { unknowResFormat = value; }
		} // UseStringIndicators

		// ----------------------------------------------------------------------
		public string MissingResFormat
		{
			get { return missingResFormat; }
			set { missingResFormat = value; }
		} // MissingResFormat

		// ----------------------------------------------------------------------
		public string InvalidResFormat
		{
			get { return invalidResFormat; }
			set { invalidResFormat = value; }
		} // InvalidResFormat

		// ----------------------------------------------------------------------
		internal string GetEnumValue<T>( T enumValue ) where T : struct
		{
			return GetString( typeof( T ).Name + EnumTool.EnumNameSeparator + EnumTool.Format( enumValue ) );
		} // GetEnumValue

		// ----------------------------------------------------------------------
		public EnumItemCollection<T> GetEnumItems<T>() where T : struct
		{
			return new EnumItemCollection<T>( this );
		} // GetEnumItems

		// ----------------------------------------------------------------------
		public override string GetString( string name )
		{
			string value = base.GetString( name );
			if ( value == null )
			{
				return GetUnknownResName( name );
			}
			if ( string.IsNullOrEmpty( value ) )
			{
				value = GetMissingResName( name );
			}
			return value;
		} // GetString

		// ----------------------------------------------------------------------
		public override string GetString( string name, CultureInfo culture )
		{
			string value = base.GetString( name, culture );
			if ( value == null )
			{
				return GetUnknownResName( name );
			}
			if ( string.IsNullOrEmpty( value ) )
			{
				value = GetMissingResName( name );
			}
			return value;
		} // GetString

		// ----------------------------------------------------------------------
		public string GetLanguageString<T>( string name, T language ) where T : struct
		{
			if ( name == null )
			{
				throw new ArgumentNullException( "name" );
			}
			return GetString( name + EnumTool.EnumNameSeparator + EnumTool.Format( language ) );
		} // GetLanguageString

		// ----------------------------------------------------------------------
		/// <summary>
		/// Formats the given format-string with the invariant culture and the
		/// given arguments.
		/// </summary>
		/// <param name="name">the name of the resource</param>
		/// <param name="args">the arguments to fill in</param>
		/// <returns>the formatted string</returns>
		public string Format( string name, params object[] args )
		{
			return Format( CultureInfo.InvariantCulture, name, args );
		} // Format

		// ----------------------------------------------------------------------
		/// <summary>
		/// Formats the given format-string with the invariant culture and the
		/// given arguments.
		/// </summary>
		/// <param name="provider">an object that supplies culture-specific formatting information. </param>
		/// <param name="name">the name of the resource</param>
		/// <param name="args">the arguments to fill in</param>
		/// <returns>the formatted string</returns>
		public string Format( IFormatProvider provider, string name, params object[] args )
		{
			string value = GetString( name );
			if ( value == null )
			{
				return GetUnknownResName( name );
			}
			if ( string.IsNullOrEmpty( value ) )
			{
				value = GetMissingResName( name );
			}
			try
			{
				return string.Format( provider, value, args );
			}
			catch ( FormatException )
			{
				return GetInvalidResName( name );
			}
		} // Format

		// ----------------------------------------------------------------------
		protected string GetUnknownResName( string name )
		{
			return UseStringIndicators ? string.Format( unknowResFormat, name ) : name;
		} // GetUnknownResName

		// ----------------------------------------------------------------------
		protected string GetMissingResName( string name )
		{
			return UseStringIndicators ? string.Format( missingResFormat, name ) : name;
		} // GetMissingResName

		// ----------------------------------------------------------------------
		protected string GetInvalidResName( string name )
		{
			return UseStringIndicators ? string.Format( invalidResFormat, name ) : name;
		} // GetUnknownResName

		// ----------------------------------------------------------------------
		// members
		private bool useStringIndicators = true;
		private string unknowResFormat = "?{0}?";
		private string missingResFormat = "*{0}*";
		private string invalidResFormat = "!{0}!";

	} // class StringsResourceManager

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

Share

About the Author

Jani Giannoudis
Software Developer (Senior)
Switzerland Switzerland
Jani is Co-founder of Meerazo.com, a free service which allows to share resources like locations, things, persons and their services in a cooperating group of people.

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 20 Apr 2012
Article Copyright 2011 by Jani Giannoudis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid