using System;
using System.IO;
using System.Resources;
using System.Reflection;
using System.Collections;
namespace AnticipatingMinds.PlatformServices.Globalization{
/// <summary>
/// Provides access to assembly based resources.
/// </summary>
/// <remarks>
/// AssemblyResourceManager is a wrapper arounf ResourceManager class. The main differentiator though that
/// it organizes all the code to instatiate and managed Resourcemanager in one place.
/// AssemblyResourceManager also caches instances of ResourceManager class per assembly/resourcebase basis.
/// Though accessing cache requires some reflection code (aquiring calling assembly) it still
/// speeds up creation of ResourceManager.
/// Organizing all the code for managing Resourcemanager in one place also facilitates better code reuse
/// and enables single point of failure.
/// </remarks>
public class AssemblyResourceManager
{
/// <summary>
/// Initializes new instance of AssemblyResourceManager.
/// </summary>
/// <remarks>This constaractor is private because AssemblyResourceManager is a singleton and
/// it cannot be instantiated from the outside world.</remarks>
/// <param name="resourceBaseName">The root name of the resources. For example, the root name for the resource file named "MyResource.en-US.resources" is "MyResource".</param>
/// <param name="resourceAssembly">The main Assembly for the resources.</param>
private AssemblyResourceManager(string resourceBaseName, Assembly resourceAssembly)
{
resourceManager = new ResourceManager(resourceBaseName,resourceAssembly);
this.resourceAssembly = resourceAssembly;
}
/// <summary>
/// Instantiates ResourceManager base on resource name. Assembly is assumed to
/// be calling assembly.
/// </summary>
/// <param name="resourceBaseName">The root name of the resources. For example, the root name for the resource file named "MyResource.en-US.resources" is "MyResource".</param>
/// <returns>AssemblyResourceManager instance.</returns>
public static AssemblyResourceManager GetInstance(string resourceBaseName)
{
return GetInstance(resourceBaseName,Assembly.GetCallingAssembly());
}
/// <summary>
/// Instantiates ResourceManager base on resource name and main resource assembly.
/// </summary>
/// <param name="resourceBaseName">The root name of the resources. For example, the root name for the resource file named "MyResource.en-US.resources" is "MyResource".</param>
/// <param name="mainResourceAssembly">The main Assembly for the resources.</param>
/// <returns>AssemblyResourceManager instance.</returns>
public static AssemblyResourceManager GetInstance(string resourceBaseName, Assembly mainResourceAssembly)
{
string key = resourceBaseName + mainResourceAssembly.FullName;
AssemblyResourceManager resourceManagerInstance = (AssemblyResourceManager) resourceManagersCache[key];
if(resourceManagerInstance == null)
{
resourceManagerInstance = new AssemblyResourceManager(resourceBaseName,mainResourceAssembly);
resourceManagersCache.Add(key,resourceManagerInstance);
}
return resourceManagerInstance;
}
/// <summary>
/// Gets the value of the specified String resource for the current culture.
/// </summary>
/// <param name="stringId">Id of string resource.</param>
/// <returns>Returns the value of the specified String resource.</returns>
public string GetString(string stringId)
{
string translatedString = resourceManager.GetString(stringId);
if(translatedString == null)
return "!"+stringId + "!"; //indicate that translation is missing
else
return translatedString;
}
/// <summary>
/// Gets formatted value of the specified String resource for the current culture.
/// </summary>
/// <param name="stringId">Id of string resource.</param>
/// <param name="arguments">Arguments for formatting destenation string.</param>
/// <returns>Returns the value of the specified String resource formatted with provided arguments.</returns>
public string GetFormatedString(string stringId,params object[] arguments)
{
string resourceString = resourceManager.GetString(stringId);
if(resourceString == null)
resourceString = stringId;
if(arguments.Length == 0)
return resourceString;
else
{
return String.Format(resourceString,arguments);
}
}
/// <summary>
/// Gets the value of the specified Object resource for the current culture.
/// </summary>
/// <param name="resourceId">Resource id.</param>
/// <returns>Returns the value of the specified Object resource.</returns>
public object GetObject(string resourceId)
{
return resourceManager.GetObject(resourceId);
}
/// <summary>
/// Gets the resourse stream from assembly manifest.
/// </summary>
/// <param name="resourceName">Resource name.</param>
/// <returns>Resource stream.</returns>
public Stream GetManifestResourceStream(string resourceName)
{
return resourceAssembly.GetManifestResourceStream(resourceName);
}
private static Hashtable resourceManagersCache = new Hashtable();
private ResourceManager resourceManager = null;
private Assembly resourceAssembly;
}
}
|