AssemblyResourceManager.cs :  » Development » devAdvantage » AnticipatingMinds » PlatformServices » Globalization » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Development » devAdvantage 
devAdvantage » AnticipatingMinds » PlatformServices » Globalization » AssemblyResourceManager.cs
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;
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.